Adds compileCommands command line argument to specify an absolute path directly to the requested compile_commands.json for flags.
Details
Diff Detail
- Build Status
Buildable 9908 Build 9908: arc lint + arc unit
Event Timeline
clangd/GlobalCompilationDatabase.cpp | ||
---|---|---|
29 | Please place this flag in ClangdMain.cpp. | |
29 | Following naming convention for flags, this should be compile-commands. | |
30 | A description is somewhat vague. What is the intention of this flag?
It looks like the code also looks at the parent directories of compile-commands. Is that the original intention? Maybe consider making it look only inside the directory itself and not its parents? | |
49 | Incidental whitespace change? | |
93 | Please check command arguments for validity in main(). | |
101 | Naming: local variables should be UpperCamelCase | |
103 | Maybe rewrite to avoid extra nesting? For example, if (!badPath) { auto result = CDB.get(); CompilationDatabases.insert(std::make_pair(File, std::move(CDB))); return result; } // Proceed as before... | |
103 | Maybe extract a function from the for loop body instead of copying it here? | |
115 | Please consider removing this code duplication by extracting a function from the loop body (also see my previous comment). |
BTW, if you're only looking for providing extra compilation flags for some files, you could use an extension to LSP that is already in clangd source tree. (See tests from https://reviews.llvm.org/D34947 for more details).
But your change is also useful, as it allows to override the whole compilation database. Would be interested in this change to land regardless of whether the extra flags thing suits you well.
See my comments.
Could you also clang-format the code please?
clangd/ClangdServer.cpp | ||
---|---|---|
150 | Could you pass llvm::Optional<Path> instead (Path is just a descriptive typedef that we use in place of std::string) and check for empty string in main()? | |
clangd/GlobalCompilationDatabase.cpp | ||
14 | There are definitely unneeded includes here. Remove most of them and leave only the required ones? | |
30 | How about chaning semantics of the flag to only look inside "--compile-commands-dir" and not its parent paths? The flag that you propose seems to have a different purpose of providing clangd with custom compilation flags. | |
93 | Let's simply lookup only inside CompileCommandsDir if it was set. | |
clangd/GlobalCompilationDatabase.h | ||
38 | GlobalCompilationDatabase is an interface class and is designed specifically to not contain any fields.
| |
clangd/tool/ClangdMain.cpp | ||
29 | Could you use descriptive Path typedef instead of std::string here and in other places? | |
30 | Maybe rename this (and other occurences) to CompileCommandsDir? | |
43 |
| |
44 | Maybe rephrase the message a little bit, mentioning the name of the flag. Something like: |
Modified CompileCommandsDir to only look in the specified path if the value is set.
Moved CompileCommandsDir field to DirectoryBasedGlobalCompilationDatabase class.
Minor refactoring.
Could you also clang-format the code please?
clangd/GlobalCompilationDatabase.cpp | ||
---|---|---|
95 | Why do we use parent_path of CompilerCommandsDir, not CompilerCommandsDir itself? Maybe simply do | |
100 | Code style: don't use else after return. | |
clangd/tool/ClangdMain.cpp | ||
41 | Replace --CompileCommandsDir with --compile-commands-dir. |
clangd/ClangdServer.cpp | ||
---|---|---|
150 | Remove CompileCommandsDir from ClangdServer parameters, it is hanlded by DirectoryBasedGlobalCompilationDatabase. | |
clangd/GlobalCompilationDatabase.cpp | ||
70 | Restore this assertion, move it to tryLoadDatabaseFromPath. | |
92–101 | Maybe move this local variable into tryLoadDatabaseFromPath and remove Error parameter from tryLoadDatabaseFromPath? | |
clangd/GlobalCompilationDatabase.h | ||
49 | Rename parameter to CompileCommandsDir, change type to llvm::Optional<Path>. | |
53 | Make this function private. | |
clangd/tool/ClangdMain.cpp | ||
46 | Use Out.log() instead of Logs << for consistency with other logging code. | |
52 | Maybe also rephrase to mention the name of the flag? Something like: | |
52 | Accept llvm::Optional in ClangdLSPServer, pass llvm::None if CompileCommandsDir == "" here. |
clangd/GlobalCompilationDatabase.cpp | ||
---|---|---|
128–130 | Isn't that the same as "return CDB"? |
clangd/GlobalCompilationDatabase.cpp | ||
---|---|---|
104 | Parentheses seem redundant. | |
107 | Code style: local variable are UpperCamelCase | |
126 | Even better: return tryLoadDatabaseFromPath(CompileCommandsDir); | |
clangd/GlobalCompilationDatabase.h | ||
51 | getValue() will fail if NewCompileCommandsDir does not have a value. | |
57 | Could you please move this to other fields? Mixing functions and fields is a bit confusing. | |
clangd/tool/ClangdMain.cpp | ||
50 | Could you please move this out of the logic that validates CompileCommandsDir parameter? | |
52 | Message does not mention that path does not exist. Probably incidental. |
All lit test for clangd are currently failing if I apply your change.
Generally, please run make check-clang-tools to make sure your changes do not introduce any regressions.
The tests are failing because when --compile-commands-dir is not specified clangd now shows the cmd arg validation errors ("path not absolute", "path does not exist").
clangd/tool/ClangdMain.cpp | ||
---|---|---|
57 | The code doing command line argument validation shows errors using llvm::errs() and not Out.log(). Could you do the same for consistency? Also, could you put your new code that does argument validation right after the previous checks (to line 65)? So that we have a clear structure in the code: /// Validate command line arguments. ... /// Initialize and run ClangdLSPServer. ... |
More consistent logging in clangdmain.
Restructured argument checking in ClangdMain
Fixed empty compile-commands-dir triggering error messages.
Fixed failing standard tests.
Could you please run clang-format on every submission?
clangd/GlobalCompilationDatabase.cpp | ||
---|---|---|
127 | Is this some kind of accidental change? Why do we need to assign "/" to CompileCommandsDir? | |
clangd/tool/ClangdMain.cpp | ||
20 | We certainly don't need that include. | |
57 | This should be something like: if (CompileCommandsDir.empty()) { //... CompileCommandsDirPath = llvm::None; } else if (!is_absolute(...)) { //.... CompileCommandsDirPath = llvm::None; } else if (!exists(...)) { // .... CompileCommandsDirPath = llvm::None; } else { CompileCommandsDirPath = CompileCommandsDir; }
|
Fixed inverted compile_commands check logic that made tests fail.
More readable command arg checks.
clangd/tool/ClangdMain.cpp | ||
---|---|---|
20 | William, did you perhaps miss this comment? I don't think unistd.h makes sense here. |
clangd/tool/ClangdMain.cpp | ||
---|---|---|
20 | I indeed missed it. |
It seems some changes were be lost while merging with head.
clangd/GlobalCompilationDatabase.cpp | ||
---|---|---|
93 | Maybe move Error closer to its usage (line 84: auto CDB = tooling::CompilationDatabase::loadFromDirectory ...)? | |
97–99 | Please restore this logging statement. | |
112 | Please remove this FIXME, it is already deleted in head. |
clangd/GlobalCompilationDatabase.cpp | ||
---|---|---|
105 | We should also log if we can't find compilation database in this code path. | |
113 | This logging statement is misplaced, in the current HEAD it's outside of the loop. Otherwise the output of clangd gets really spammy. |
Thanks. LGTM modulo minor NIT (see other inline comment).
Do you need help to land this?
clangd/GlobalCompilationDatabase.cpp | ||
---|---|---|
112 | Message should probably mention that's clangd was looking for compile commands in an overridden directory. |
Improved logging message when unable to find compilation database in specified overriden directory.
Thanks for fixing the last comment.
Do you want me to land this for you?
clangd/GlobalCompilationDatabase.cpp | ||
---|---|---|
113 | NIT: missing a space at the start of "in overriden...". Otherwise filename will be concatenated with "in" |
Did a minor rename and added a few std::moves before submitting. Was not worth another round of code review.
Could you pass llvm::Optional<Path> instead (Path is just a descriptive typedef that we use in place of std::string) and check for empty string in main()?