diff options
author | Ilya Biryukov <ibiryukov@google.com> | 2018-05-28 12:11:37 +0000 |
---|---|---|
committer | Ilya Biryukov <ibiryukov@google.com> | 2018-05-28 12:11:37 +0000 |
commit | 194406e244d05dab41de3936ec7f39ad41102586 (patch) | |
tree | 441b32845264a3c12263cf4b0da7337f5ccaeabd /clang-tools-extra/clangd | |
parent | 7d272111e0d0900bfbabe33431f8f9ff0c4748f5 (diff) |
[clangd] Fix leak sanitizers warnings in clangd
The commit includes two changes:
1. Set DisableFree to false when building the ParsedAST.
This is sane default, since clangd never wants to leak the AST.
2. Make sure CompilerInstance created in code completion is passed to
the FrontendAction::BeginSourceFile call.
We have to do this to make sure the memory buffers of remapped
files are properly freed.
Our tests do not produce any warnings under asan anymore.
The changes are mostly trivial, just moving the code around. So
sending without review.
Diffstat (limited to 'clang-tools-extra/clangd')
-rw-r--r-- | clang-tools-extra/clangd/ClangdUnit.cpp | 4 | ||||
-rw-r--r-- | clang-tools-extra/clangd/CodeComplete.cpp | 35 |
2 files changed, 21 insertions, 18 deletions
diff --git a/clang-tools-extra/clangd/ClangdUnit.cpp b/clang-tools-extra/clangd/ClangdUnit.cpp index 41f2df3669e..105bdb1fe8e 100644 --- a/clang-tools-extra/clangd/ClangdUnit.cpp +++ b/clang-tools-extra/clangd/ClangdUnit.cpp @@ -153,6 +153,10 @@ ParsedAST::Build(std::unique_ptr<clang::CompilerInvocation> CI, std::unique_ptr<llvm::MemoryBuffer> Buffer, std::shared_ptr<PCHContainerOperations> PCHs, IntrusiveRefCntPtr<vfs::FileSystem> VFS) { + assert(CI); + // Command-line parsing sets DisableFree to true by default, but we don't want + // to leak memory in clangd. + CI->getFrontendOpts().DisableFree = false; const PrecompiledPreamble *PreamblePCH = Preamble ? &Preamble->Preamble : nullptr; diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp index 4adb80c8470..cfebb3e3c94 100644 --- a/clang-tools-extra/clangd/CodeComplete.cpp +++ b/clang-tools-extra/clangd/CodeComplete.cpp @@ -699,26 +699,13 @@ bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer, log("Couldn't create CompilerInvocation");; return false; } - CI->getFrontendOpts().DisableFree = false; + auto &FrontendOpts = CI->getFrontendOpts(); + FrontendOpts.DisableFree = false; + FrontendOpts.SkipFunctionBodies = true; CI->getLangOpts()->CommentOpts.ParseAllComments = true; - - std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer = - llvm::MemoryBuffer::getMemBufferCopy(Input.Contents, Input.FileName); - - // The diagnostic options must be set before creating a CompilerInstance. - CI->getDiagnosticOpts().IgnoreWarnings = true; - // We reuse the preamble whether it's valid or not. This is a - // correctness/performance tradeoff: building without a preamble is slow, and - // completion is latency-sensitive. - auto Clang = prepareCompilerInstance( - std::move(CI), Input.Preamble, std::move(ContentsBuffer), - std::move(Input.PCHs), std::move(Input.VFS), DummyDiagsConsumer); - // Disable typo correction in Sema. - Clang->getLangOpts().SpellChecking = false; - - auto &FrontendOpts = Clang->getFrontendOpts(); - FrontendOpts.SkipFunctionBodies = true; + CI->getLangOpts()->SpellChecking = false; + // Setup code completion. FrontendOpts.CodeCompleteOpts = Options; FrontendOpts.CodeCompletionAt.FileName = Input.FileName; auto Offset = positionToOffset(Input.Contents, Input.Pos); @@ -731,6 +718,18 @@ bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer, FrontendOpts.CodeCompletionAt.Column) = offsetToClangLineColumn(Input.Contents, *Offset); + std::unique_ptr<llvm::MemoryBuffer> ContentsBuffer = + llvm::MemoryBuffer::getMemBufferCopy(Input.Contents, Input.FileName); + // The diagnostic options must be set before creating a CompilerInstance. + CI->getDiagnosticOpts().IgnoreWarnings = true; + // We reuse the preamble whether it's valid or not. This is a + // correctness/performance tradeoff: building without a preamble is slow, and + // completion is latency-sensitive. + // NOTE: we must call BeginSourceFile after prepareCompilerInstance. Otherwise + // the remapped buffers do not get freed. + auto Clang = prepareCompilerInstance( + std::move(CI), Input.Preamble, std::move(ContentsBuffer), + std::move(Input.PCHs), std::move(Input.VFS), DummyDiagsConsumer); Clang->setCodeCompletionConsumer(Consumer.release()); SyntaxOnlyAction Action; |