diff options
| author | Haojian Wu <hokein@google.com> | 2019-01-22 09:39:05 +0000 |
|---|---|---|
| committer | Haojian Wu <hokein@google.com> | 2019-01-22 09:39:05 +0000 |
| commit | 1ca0c58c8152ecfb2a47f91080b64eae7710dadb (patch) | |
| tree | cf967fe3f5caa6c4745dbf4a404a55b015d85cc5 /clang-tools-extra/clangd | |
| parent | bd374b27cc33567b0a0522ee511fc1ea9f0bb1df (diff) | |
| download | bcm5719-llvm-1ca0c58c8152ecfb2a47f91080b64eae7710dadb.tar.gz bcm5719-llvm-1ca0c58c8152ecfb2a47f91080b64eae7710dadb.zip | |
[clangd] Support clang-tidy configuration in clangd.
Summary:
This patch adds some basic supports for clang-tidy configurations in clangd:
- clangd will respect .clang-tidy configurations for each file
- we don't aim to support all clang-tidy options in clangd, only a
small subset of condfigurations (options related to which checks will be
enabled) are supported.
- add a `clang-tidy-checks` CLI option that can override options from
.clang-tidy file
Reviewers: ilya-biryukov, sammccall
Reviewed By: sammccall
Subscribers: javed.absar, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Differential Revision: https://reviews.llvm.org/D55256
llvm-svn: 351792
Diffstat (limited to 'clang-tools-extra/clangd')
| -rw-r--r-- | clang-tools-extra/clangd/ClangdLSPServer.cpp | 4 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/ClangdLSPServer.h | 5 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/ClangdServer.cpp | 10 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/ClangdServer.h | 11 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/ClangdUnit.cpp | 17 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/ClangdUnit.h | 5 | ||||
| -rw-r--r-- | clang-tools-extra/clangd/tool/ClangdMain.cpp | 17 |
7 files changed, 51 insertions, 18 deletions
diff --git a/clang-tools-extra/clangd/ClangdLSPServer.cpp b/clang-tools-extra/clangd/ClangdLSPServer.cpp index d3ee6aa5ef6..e90ee916184 100644 --- a/clang-tools-extra/clangd/ClangdLSPServer.cpp +++ b/clang-tools-extra/clangd/ClangdLSPServer.cpp @@ -720,11 +720,13 @@ void ClangdLSPServer::onSymbolInfo(const TextDocumentPositionParams &Params, } ClangdLSPServer::ClangdLSPServer(class Transport &Transp, + const FileSystemProvider &FSProvider, const clangd::CodeCompleteOptions &CCOpts, llvm::Optional<Path> CompileCommandsDir, bool UseDirBasedCDB, const ClangdServer::Options &Opts) - : Transp(Transp), MsgHandler(new MessageHandler(*this)), CCOpts(CCOpts), + : Transp(Transp), MsgHandler(new MessageHandler(*this)), + FSProvider(FSProvider), CCOpts(CCOpts), SupportedSymbolKinds(defaultSymbolKinds()), SupportedCompletionItemKinds(defaultCompletionItemKinds()), UseDirBasedCDB(UseDirBasedCDB), diff --git a/clang-tools-extra/clangd/ClangdLSPServer.h b/clang-tools-extra/clangd/ClangdLSPServer.h index d84c298e4c9..40b9fc40e90 100644 --- a/clang-tools-extra/clangd/ClangdLSPServer.h +++ b/clang-tools-extra/clangd/ClangdLSPServer.h @@ -37,7 +37,8 @@ public: /// for compile_commands.json in all parent directories of each file. /// If UseDirBasedCDB is false, compile commands are not read from disk. // FIXME: Clean up signature around CDBs. - ClangdLSPServer(Transport &Transp, const clangd::CodeCompleteOptions &CCOpts, + ClangdLSPServer(Transport &Transp, const FileSystemProvider &FSProvider, + const clangd::CodeCompleteOptions &CCOpts, llvm::Optional<Path> CompileCommandsDir, bool UseDirBasedCDB, const ClangdServer::Options &Opts); ~ClangdLSPServer(); @@ -128,7 +129,7 @@ private: void call(StringRef Method, llvm::json::Value Params); void notify(StringRef Method, llvm::json::Value Params); - RealFileSystemProvider FSProvider; + const FileSystemProvider &FSProvider; /// Options used for code completion clangd::CodeCompleteOptions CCOpts; /// Options used for diagnostics. diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp index d70321da15c..5f494210ac3 100644 --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/clang-tools-extra/clangd/ClangdServer.cpp @@ -105,6 +105,7 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB, DynamicIdx(Opts.BuildDynamicSymbolIndex ? new FileIndex(Opts.HeavyweightDynamicSymbolIndex) : nullptr), + ClangTidyOptProvider(Opts.ClangTidyOptProvider), WorkspaceRoot(Opts.WorkspaceRoot), PCHs(std::make_shared<PCHContainerOperations>()), // Pass a callback into `WorkScheduler` to extract symbols from a newly @@ -140,13 +141,16 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB, void ClangdServer::addDocument(PathRef File, llvm::StringRef Contents, WantDiagnostics WantDiags) { + tidy::ClangTidyOptions Options = tidy::ClangTidyOptions::getDefaults(); + if (ClangTidyOptProvider) + Options = ClangTidyOptProvider->getOptions(File); // FIXME: some build systems like Bazel will take time to preparing // environment to build the file, it would be nice if we could emit a // "PreparingBuild" status to inform users, it is non-trivial given the // current implementation. - WorkScheduler.update(File, - ParseInputs{getCompileCommand(File), - FSProvider.getFileSystem(), Contents.str()}, + WorkScheduler.update(File, ParseInputs{getCompileCommand(File), + FSProvider.getFileSystem(), + Contents.str(), Options}, WantDiags); } diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h index 20874c4bc0b..dc9581b504d 100644 --- a/clang-tools-extra/clangd/ClangdServer.h +++ b/clang-tools-extra/clangd/ClangdServer.h @@ -9,6 +9,7 @@ #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDSERVER_H #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDSERVER_H +#include "../clang-tidy/ClangTidyOptions.h" #include "Cancellation.h" #include "ClangdUnit.h" #include "CodeComplete.h" @@ -92,6 +93,13 @@ public: /// If set, use this index to augment code completion results. SymbolIndex *StaticIndex = nullptr; + /// If set, enable clang-tidy in clangd, used to get clang-tidy + /// configurations for a particular file. + /// Clangd supports only a small subset of ClangTidyOptions, these options + /// (Checks, CheckOptions) are about which clang-tidy checks will be + /// enabled. + tidy::ClangTidyOptionsProvider *ClangTidyOptProvider = nullptr; + /// Clangd's workspace root. Relevant for "workspace" operations not bound /// to a particular file. /// FIXME: If not set, should use the current working directory. @@ -257,6 +265,9 @@ private: // Storage for merged views of the various indexes. std::vector<std::unique_ptr<SymbolIndex>> MergedIdx; + // The provider used to provide a clang-tidy option for a specific file. + tidy::ClangTidyOptionsProvider *ClangTidyOptProvider = nullptr; + // GUARDED_BY(CachedCompletionFuzzyFindRequestMutex) llvm::StringMap<llvm::Optional<FuzzyFindRequest>> CachedCompletionFuzzyFindRequestByFile; diff --git a/clang-tools-extra/clangd/ClangdUnit.cpp b/clang-tools-extra/clangd/ClangdUnit.cpp index f7b8b731e12..268f7d1bbb4 100644 --- a/clang-tools-extra/clangd/ClangdUnit.cpp +++ b/clang-tools-extra/clangd/ClangdUnit.cpp @@ -132,6 +132,9 @@ public: CompilerInstance &Clang) { auto &PP = Clang.getPreprocessor(); auto *ExistingCallbacks = PP.getPPCallbacks(); + // No need to replay events if nobody is listening. + if (!ExistingCallbacks) + return; PP.addPPCallbacks(std::unique_ptr<PPCallbacks>( new ReplayPreamble(Includes, ExistingCallbacks, Clang.getSourceManager(), PP, Clang.getLangOpts()))); @@ -227,7 +230,8 @@ ParsedAST::build(std::unique_ptr<CompilerInvocation> CI, std::shared_ptr<const PreambleData> Preamble, std::unique_ptr<llvm::MemoryBuffer> Buffer, std::shared_ptr<PCHContainerOperations> PCHs, - llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) { + llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, + const tidy::ClangTidyOptions &ClangTidyOpts) { assert(CI); // Command-line parsing sets DisableFree to true by default, but we don't want // to leak memory in clangd. @@ -264,15 +268,8 @@ ParsedAST::build(std::unique_ptr<CompilerInvocation> CI, tidy::ClangTidyCheckFactories CTFactories; for (const auto &E : tidy::ClangTidyModuleRegistry::entries()) E.instantiate()->addCheckFactories(CTFactories); - auto CTOpts = tidy::ClangTidyOptions::getDefaults(); - // FIXME: this needs to be configurable, and we need to support .clang-tidy - // files and other options providers. - // These checks exercise the matcher- and preprocessor-based hooks. - CTOpts.Checks = "bugprone-sizeof-expression," - "bugprone-macro-repeated-side-effects," - "modernize-deprecated-headers"; CTContext.emplace(llvm::make_unique<tidy::DefaultOptionsProvider>( - tidy::ClangTidyGlobalOptions(), CTOpts)); + tidy::ClangTidyGlobalOptions(), ClangTidyOpts)); CTContext->setDiagnosticsEngine(&Clang->getDiagnostics()); CTContext->setASTContext(&Clang->getASTContext()); CTContext->setCurrentFile(MainInput.getFile()); @@ -538,7 +535,7 @@ buildAST(PathRef FileName, std::unique_ptr<CompilerInvocation> Invocation, return ParsedAST::build(llvm::make_unique<CompilerInvocation>(*Invocation), Preamble, llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents), - PCHs, std::move(VFS)); + PCHs, std::move(VFS), Inputs.ClangTidyOpts); } SourceLocation getBeginningOfIdentifier(ParsedAST &Unit, const Position &Pos, diff --git a/clang-tools-extra/clangd/ClangdUnit.h b/clang-tools-extra/clangd/ClangdUnit.h index abcefdb6bf6..3a4841d7080 100644 --- a/clang-tools-extra/clangd/ClangdUnit.h +++ b/clang-tools-extra/clangd/ClangdUnit.h @@ -9,6 +9,7 @@ #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNIT_H #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNIT_H +#include "../clang-tidy/ClangTidyOptions.h" #include "Diagnostics.h" #include "FS.h" #include "Function.h" @@ -64,6 +65,7 @@ struct ParseInputs { tooling::CompileCommand CompileCommand; IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS; std::string Contents; + tidy::ClangTidyOptions ClangTidyOpts; }; /// Stores and provides access to parsed AST. @@ -76,7 +78,8 @@ public: std::shared_ptr<const PreambleData> Preamble, std::unique_ptr<llvm::MemoryBuffer> Buffer, std::shared_ptr<PCHContainerOperations> PCHs, - IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS); + IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, + const tidy::ClangTidyOptions &ClangTidyOpts); ParsedAST(ParsedAST &&Other); ParsedAST &operator=(ParsedAST &&Other); diff --git a/clang-tools-extra/clangd/tool/ClangdMain.cpp b/clang-tools-extra/clangd/tool/ClangdMain.cpp index 80f3f45e672..c969a5b7cfb 100644 --- a/clang-tools-extra/clangd/tool/ClangdMain.cpp +++ b/clang-tools-extra/clangd/tool/ClangdMain.cpp @@ -201,6 +201,12 @@ static llvm::cl::opt<bool> EnableFunctionArgSnippets( "placeholders for method parameters."), llvm::cl::init(CodeCompleteOptions().EnableFunctionArgSnippets)); +static llvm::cl::opt<std::string> ClangTidyChecks( + "clang-tidy-checks", + llvm::cl::desc("List of clang-tidy checks to run (this will overrides " + ".clang-tidy files)"), + llvm::cl::init(""), llvm::cl::Hidden); + namespace { /// \brief Supports a test URI scheme with relaxed constraints for lit tests. @@ -408,6 +414,7 @@ int main(int argc, char *argv[]) { CCOpts.EnableFunctionArgSnippets = EnableFunctionArgSnippets; CCOpts.AllScopes = AllScopesCompletion; + RealFileSystemProvider FSProvider; // Initialize and run ClangdLSPServer. // Change stdin to binary to not lose \r\n on windows. llvm::sys::ChangeStdinToBinary(); @@ -427,8 +434,16 @@ int main(int argc, char *argv[]) { PrettyPrint, InputStyle); } + // Create an empty clang-tidy option. + auto OverrideClangTidyOptions = tidy::ClangTidyOptions::getDefaults(); + OverrideClangTidyOptions.Checks = ClangTidyChecks; + tidy::FileOptionsProvider ClangTidyOptProvider( + tidy::ClangTidyGlobalOptions(), + /* Default */ tidy::ClangTidyOptions::getDefaults(), + /* Override */ OverrideClangTidyOptions, FSProvider.getFileSystem()); + Opts.ClangTidyOptProvider = &ClangTidyOptProvider; ClangdLSPServer LSPServer( - *TransportLayer, CCOpts, CompileCommandsDirPath, + *TransportLayer, FSProvider, CCOpts, CompileCommandsDirPath, /*UseDirBasedCDB=*/CompileArgsFrom == FilesystemCompileArgs, Opts); llvm::set_thread_name("clangd.main"); return LSPServer.run() ? 0 |

