2019-09-04 17:46:06 +08:00
|
|
|
//===--- ParsedAST.cpp -------------------------------------------*- C++-*-===//
|
2017-05-16 17:38:59 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-05-16 17:38:59 +08:00
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2019-09-04 17:46:06 +08:00
|
|
|
#include "ParsedAST.h"
|
2018-11-16 16:32:23 +08:00
|
|
|
#include "../clang-tidy/ClangTidyDiagnosticConsumer.h"
|
|
|
|
#include "../clang-tidy/ClangTidyModuleRegistry.h"
|
2019-08-08 15:21:06 +08:00
|
|
|
#include "AST.h"
|
2017-12-04 21:49:59 +08:00
|
|
|
#include "Compiler.h"
|
2018-03-12 23:28:22 +08:00
|
|
|
#include "Diagnostics.h"
|
2019-01-28 22:01:55 +08:00
|
|
|
#include "Headers.h"
|
|
|
|
#include "IncludeFixer.h"
|
2017-09-20 18:46:58 +08:00
|
|
|
#include "Logger.h"
|
2018-03-12 23:28:22 +08:00
|
|
|
#include "SourceCode.h"
|
2017-11-02 17:21:51 +08:00
|
|
|
#include "Trace.h"
|
2019-02-05 00:19:57 +08:00
|
|
|
#include "index/CanonicalIncludes.h"
|
2019-01-28 22:01:55 +08:00
|
|
|
#include "index/Index.h"
|
2018-05-24 23:50:15 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2019-08-08 15:21:06 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2018-05-24 23:50:15 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2019-08-30 17:33:27 +08:00
|
|
|
#include "clang/Basic/SourceLocation.h"
|
2019-05-03 00:12:36 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/TokenKinds.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
2017-07-21 21:29:29 +08:00
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
2017-05-26 20:26:51 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2017-06-29 00:12:10 +08:00
|
|
|
#include "clang/Index/IndexDataConsumer.h"
|
2017-07-21 21:29:29 +08:00
|
|
|
#include "clang/Index/IndexingAction.h"
|
2017-06-29 00:12:10 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
#include "clang/Lex/MacroInfo.h"
|
2019-08-30 17:33:27 +08:00
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
2017-06-29 00:12:10 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2018-07-09 19:33:31 +08:00
|
|
|
#include "clang/Lex/PreprocessorOptions.h"
|
2017-07-21 21:29:29 +08:00
|
|
|
#include "clang/Sema/Sema.h"
|
|
|
|
#include "clang/Serialization/ASTWriter.h"
|
2019-04-04 20:56:03 +08:00
|
|
|
#include "clang/Serialization/PCHContainerOperations.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
2019-06-19 22:03:19 +08:00
|
|
|
#include "clang/Tooling/Syntax/Tokens.h"
|
2017-07-21 21:29:29 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2019-01-28 22:01:55 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2018-10-09 16:27:31 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2017-07-21 21:29:29 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2018-02-02 03:06:45 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-06-29 00:12:10 +08:00
|
|
|
#include <algorithm>
|
2019-01-28 22:01:55 +08:00
|
|
|
#include <memory>
|
2017-06-29 00:12:10 +08:00
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2017-07-21 21:29:29 +08:00
|
|
|
namespace {
|
|
|
|
|
2018-01-25 22:32:21 +08:00
|
|
|
template <class T> std::size_t getUsedBytes(const std::vector<T> &Vec) {
|
|
|
|
return Vec.capacity() * sizeof(T);
|
|
|
|
}
|
|
|
|
|
2017-07-21 21:29:29 +08:00
|
|
|
class DeclTrackingASTConsumer : public ASTConsumer {
|
|
|
|
public:
|
2018-06-06 00:30:25 +08:00
|
|
|
DeclTrackingASTConsumer(std::vector<Decl *> &TopLevelDecls)
|
2017-07-21 21:29:29 +08:00
|
|
|
: TopLevelDecls(TopLevelDecls) {}
|
|
|
|
|
|
|
|
bool HandleTopLevelDecl(DeclGroupRef DG) override {
|
2018-06-06 00:30:25 +08:00
|
|
|
for (Decl *D : DG) {
|
2019-07-01 19:49:01 +08:00
|
|
|
auto &SM = D->getASTContext().getSourceManager();
|
2019-07-19 16:33:39 +08:00
|
|
|
if (!isInsideMainFile(D->getLocation(), SM))
|
2018-11-09 23:35:00 +08:00
|
|
|
continue;
|
2019-08-08 15:21:06 +08:00
|
|
|
if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
|
|
|
|
if (isImplicitTemplateInstantiation(ND))
|
|
|
|
continue;
|
2018-11-09 23:35:00 +08:00
|
|
|
|
2017-07-21 21:29:29 +08:00
|
|
|
// ObjCMethodDecl are not actually top-level decls.
|
|
|
|
if (isa<ObjCMethodDecl>(D))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
TopLevelDecls.push_back(D);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-06-06 00:30:25 +08:00
|
|
|
std::vector<Decl *> &TopLevelDecls;
|
2017-07-21 21:29:29 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class ClangdFrontendAction : public SyntaxOnlyAction {
|
|
|
|
public:
|
2018-06-06 00:30:25 +08:00
|
|
|
std::vector<Decl *> takeTopLevelDecls() { return std::move(TopLevelDecls); }
|
2017-07-21 21:29:29 +08:00
|
|
|
|
|
|
|
protected:
|
2019-01-07 23:45:19 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
CreateASTConsumer(CompilerInstance &CI, llvm::StringRef InFile) override {
|
2019-08-15 07:52:23 +08:00
|
|
|
return std::make_unique<DeclTrackingASTConsumer>(/*ref*/ TopLevelDecls);
|
2017-07-21 21:29:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-06-06 00:30:25 +08:00
|
|
|
std::vector<Decl *> TopLevelDecls;
|
2017-07-21 21:29:29 +08:00
|
|
|
};
|
|
|
|
|
2019-09-04 15:35:00 +08:00
|
|
|
// This collects macro expansions in the main file.
|
|
|
|
// (Contrast with CollectMainFileMacros in Preamble.cpp, which collects macro
|
|
|
|
// *definitions* in the preamble region of the main file).
|
2019-08-30 17:33:27 +08:00
|
|
|
class CollectMainFileMacroExpansions : public PPCallbacks {
|
|
|
|
const SourceManager &SM;
|
|
|
|
std::vector<SourceLocation> &MainFileMacroLocs;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CollectMainFileMacroExpansions(const SourceManager &SM,
|
|
|
|
std::vector<SourceLocation> &MainFileMacroLocs)
|
|
|
|
: SM(SM), MainFileMacroLocs(MainFileMacroLocs) {}
|
|
|
|
|
2019-09-06 18:24:39 +08:00
|
|
|
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
|
|
|
|
SourceRange Range, const MacroArgs *Args) override {
|
2019-08-30 17:33:27 +08:00
|
|
|
SourceLocation L = MacroNameTok.getLocation();
|
|
|
|
if (!L.isMacroID() && isInsideMainFile(L, SM))
|
|
|
|
MainFileMacroLocs.push_back(L);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-20 18:58:48 +08:00
|
|
|
// When using a preamble, only preprocessor events outside its bounds are seen.
|
|
|
|
// This is almost what we want: replaying transitive preprocessing wastes time.
|
|
|
|
// However this confuses clang-tidy checks: they don't see any #includes!
|
|
|
|
// So we replay the *non-transitive* #includes that appear in the main-file.
|
|
|
|
// It would be nice to replay other events (macro definitions, ifdefs etc) but
|
|
|
|
// this addresses the most common cases fairly cheaply.
|
|
|
|
class ReplayPreamble : private PPCallbacks {
|
|
|
|
public:
|
|
|
|
// Attach preprocessor hooks such that preamble events will be injected at
|
|
|
|
// the appropriate time.
|
|
|
|
// Events will be delivered to the *currently registered* PP callbacks.
|
|
|
|
static void attach(const IncludeStructure &Includes,
|
|
|
|
CompilerInstance &Clang) {
|
|
|
|
auto &PP = Clang.getPreprocessor();
|
|
|
|
auto *ExistingCallbacks = PP.getPPCallbacks();
|
2019-01-22 17:39:05 +08:00
|
|
|
// No need to replay events if nobody is listening.
|
|
|
|
if (!ExistingCallbacks)
|
|
|
|
return;
|
2018-11-20 18:58:48 +08:00
|
|
|
PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(
|
|
|
|
new ReplayPreamble(Includes, ExistingCallbacks,
|
|
|
|
Clang.getSourceManager(), PP, Clang.getLangOpts())));
|
|
|
|
// We're relying on the fact that addPPCallbacks keeps the old PPCallbacks
|
|
|
|
// around, creating a chaining wrapper. Guard against other implementations.
|
|
|
|
assert(PP.getPPCallbacks() != ExistingCallbacks &&
|
|
|
|
"Expected chaining implementation");
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ReplayPreamble(const IncludeStructure &Includes, PPCallbacks *Delegate,
|
|
|
|
const SourceManager &SM, Preprocessor &PP,
|
|
|
|
const LangOptions &LangOpts)
|
|
|
|
: Includes(Includes), Delegate(Delegate), SM(SM), PP(PP),
|
|
|
|
LangOpts(LangOpts) {}
|
|
|
|
|
|
|
|
// In a normal compile, the preamble traverses the following structure:
|
|
|
|
//
|
|
|
|
// mainfile.cpp
|
|
|
|
// <built-in>
|
|
|
|
// ... macro definitions like __cplusplus ...
|
|
|
|
// <command-line>
|
|
|
|
// ... macro definitions for args like -Dfoo=bar ...
|
|
|
|
// "header1.h"
|
|
|
|
// ... header file contents ...
|
|
|
|
// "header2.h"
|
|
|
|
// ... header file contents ...
|
|
|
|
// ... main file contents ...
|
|
|
|
//
|
|
|
|
// When using a preamble, the "header1" and "header2" subtrees get skipped.
|
|
|
|
// We insert them right after the built-in header, which still appears.
|
|
|
|
void FileChanged(SourceLocation Loc, FileChangeReason Reason,
|
|
|
|
SrcMgr::CharacteristicKind Kind, FileID PrevFID) override {
|
|
|
|
// It'd be nice if there was a better way to identify built-in headers...
|
|
|
|
if (Reason == FileChangeReason::ExitFile &&
|
|
|
|
SM.getBuffer(PrevFID)->getBufferIdentifier() == "<built-in>")
|
|
|
|
replay();
|
|
|
|
}
|
|
|
|
|
|
|
|
void replay() {
|
|
|
|
for (const auto &Inc : Includes.MainFileIncludes) {
|
|
|
|
const FileEntry *File = nullptr;
|
|
|
|
if (Inc.Resolved != "")
|
2019-08-02 05:32:01 +08:00
|
|
|
if (auto FE = SM.getFileManager().getFile(Inc.Resolved))
|
|
|
|
File = *FE;
|
2018-11-20 18:58:48 +08:00
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringRef WrittenFilename =
|
|
|
|
llvm::StringRef(Inc.Written).drop_front().drop_back();
|
|
|
|
bool Angled = llvm::StringRef(Inc.Written).startswith("<");
|
2018-11-20 18:58:48 +08:00
|
|
|
|
|
|
|
// Re-lex the #include directive to find its interesting parts.
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringRef Src = SM.getBufferData(SM.getMainFileID());
|
2018-11-20 18:58:48 +08:00
|
|
|
Lexer RawLexer(SM.getLocForStartOfFile(SM.getMainFileID()), LangOpts,
|
|
|
|
Src.begin(), Src.begin() + Inc.HashOffset, Src.end());
|
|
|
|
Token HashTok, IncludeTok, FilenameTok;
|
|
|
|
RawLexer.LexFromRawLexer(HashTok);
|
|
|
|
assert(HashTok.getKind() == tok::hash);
|
|
|
|
RawLexer.setParsingPreprocessorDirective(true);
|
|
|
|
RawLexer.LexFromRawLexer(IncludeTok);
|
|
|
|
IdentifierInfo *II = PP.getIdentifierInfo(IncludeTok.getRawIdentifier());
|
|
|
|
IncludeTok.setIdentifierInfo(II);
|
|
|
|
IncludeTok.setKind(II->getTokenID());
|
|
|
|
RawLexer.LexIncludeFilename(FilenameTok);
|
|
|
|
|
|
|
|
Delegate->InclusionDirective(
|
|
|
|
HashTok.getLocation(), IncludeTok, WrittenFilename, Angled,
|
|
|
|
CharSourceRange::getCharRange(FilenameTok.getLocation(),
|
|
|
|
FilenameTok.getEndLoc()),
|
|
|
|
File, "SearchPath", "RelPath", /*Imported=*/nullptr, Inc.FileKind);
|
|
|
|
if (File)
|
2019-08-27 09:03:25 +08:00
|
|
|
// FIXME: Use correctly named FileEntryRef.
|
|
|
|
Delegate->FileSkipped(FileEntryRef(File->getName(), *File), FilenameTok,
|
|
|
|
Inc.FileKind);
|
2018-11-20 18:58:48 +08:00
|
|
|
else {
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::SmallString<1> UnusedRecovery;
|
2018-11-20 18:58:48 +08:00
|
|
|
Delegate->FileNotFound(WrittenFilename, UnusedRecovery);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const IncludeStructure &Includes;
|
|
|
|
PPCallbacks *Delegate;
|
|
|
|
const SourceManager &SM;
|
|
|
|
Preprocessor &PP;
|
|
|
|
const LangOptions &LangOpts;
|
|
|
|
};
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
} // namespace
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
void dumpAST(ParsedAST &AST, llvm::raw_ostream &OS) {
|
2017-08-01 23:51:38 +08:00
|
|
|
AST.getASTContext().getTranslationUnitDecl()->dump(OS, true);
|
2017-05-16 17:38:59 +08:00
|
|
|
}
|
2017-05-23 21:42:59 +08:00
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<ParsedAST>
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
ParsedAST::build(std::unique_ptr<clang::CompilerInvocation> CI,
|
|
|
|
llvm::ArrayRef<Diag> CompilerInvocationDiags,
|
2017-11-24 21:04:21 +08:00
|
|
|
std::shared_ptr<const PreambleData> Preamble,
|
2019-01-07 23:45:19 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buffer,
|
2019-01-22 17:39:05 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
|
2019-01-28 22:01:55 +08:00
|
|
|
const SymbolIndex *Index, const ParseOptions &Opts) {
|
2018-05-28 20:11:37 +08:00
|
|
|
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;
|
2017-11-24 21:04:21 +08:00
|
|
|
const PrecompiledPreamble *PreamblePCH =
|
|
|
|
Preamble ? &Preamble->Preamble : nullptr;
|
2018-03-12 23:28:22 +08:00
|
|
|
|
|
|
|
StoreDiags ASTDiags;
|
2019-01-28 22:01:55 +08:00
|
|
|
std::string Content = Buffer->getBuffer();
|
2019-08-10 04:45:24 +08:00
|
|
|
std::string Filename = Buffer->getBufferIdentifier(); // Absolute.
|
2019-01-28 22:01:55 +08:00
|
|
|
|
2019-04-04 20:56:03 +08:00
|
|
|
auto Clang = prepareCompilerInstance(std::move(CI), PreamblePCH,
|
|
|
|
std::move(Buffer), VFS, ASTDiags);
|
2018-01-29 22:30:28 +08:00
|
|
|
if (!Clang)
|
2018-10-20 23:30:37 +08:00
|
|
|
return None;
|
2017-07-21 21:29:29 +08:00
|
|
|
|
2019-08-15 07:52:23 +08:00
|
|
|
auto Action = std::make_unique<ClangdFrontendAction>();
|
2017-09-20 15:24:15 +08:00
|
|
|
const FrontendInputFile &MainInput = Clang->getFrontendOpts().Inputs[0];
|
|
|
|
if (!Action->BeginSourceFile(*Clang, MainInput)) {
|
[clangd] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
log("BeginSourceFile() failed when building AST for {0}",
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
MainInput.getFile());
|
2018-10-20 23:30:37 +08:00
|
|
|
return None;
|
2017-07-21 21:29:29 +08:00
|
|
|
}
|
2018-02-21 10:39:08 +08:00
|
|
|
|
2018-11-16 16:32:23 +08:00
|
|
|
// Set up ClangTidy. Must happen after BeginSourceFile() so ASTContext exists.
|
|
|
|
// Clang-tidy has some limitiations to ensure reasonable performance:
|
|
|
|
// - checks don't see all preprocessor events in the preamble
|
|
|
|
// - matchers run only over the main-file top-level decls (and can't see
|
|
|
|
// ancestors outside this scope).
|
|
|
|
// In practice almost all checks work well without modifications.
|
|
|
|
std::vector<std::unique_ptr<tidy::ClangTidyCheck>> CTChecks;
|
|
|
|
ast_matchers::MatchFinder CTFinder;
|
|
|
|
llvm::Optional<tidy::ClangTidyContext> CTContext;
|
|
|
|
{
|
|
|
|
trace::Span Tracer("ClangTidyInit");
|
2019-08-10 04:45:24 +08:00
|
|
|
dlog("ClangTidy configuration for file {0}: {1}", Filename,
|
2019-01-28 22:01:55 +08:00
|
|
|
tidy::configurationAsText(Opts.ClangTidyOpts));
|
2018-11-16 16:32:23 +08:00
|
|
|
tidy::ClangTidyCheckFactories CTFactories;
|
|
|
|
for (const auto &E : tidy::ClangTidyModuleRegistry::entries())
|
|
|
|
E.instantiate()->addCheckFactories(CTFactories);
|
2019-08-15 07:52:23 +08:00
|
|
|
CTContext.emplace(std::make_unique<tidy::DefaultOptionsProvider>(
|
2019-01-28 22:01:55 +08:00
|
|
|
tidy::ClangTidyGlobalOptions(), Opts.ClangTidyOpts));
|
2018-11-16 16:32:23 +08:00
|
|
|
CTContext->setDiagnosticsEngine(&Clang->getDiagnostics());
|
|
|
|
CTContext->setASTContext(&Clang->getASTContext());
|
2019-08-10 04:45:24 +08:00
|
|
|
CTContext->setCurrentFile(Filename);
|
2018-11-16 16:32:23 +08:00
|
|
|
CTFactories.createChecks(CTContext.getPointer(), CTChecks);
|
2019-05-19 12:19:14 +08:00
|
|
|
ASTDiags.setLevelAdjuster([&CTContext](DiagnosticsEngine::Level DiagLevel,
|
|
|
|
const clang::Diagnostic &Info) {
|
2019-05-19 12:06:52 +08:00
|
|
|
if (CTContext) {
|
2019-05-19 12:19:14 +08:00
|
|
|
std::string CheckName = CTContext->getCheckName(Info.getID());
|
|
|
|
bool IsClangTidyDiag = !CheckName.empty();
|
2019-05-19 12:06:52 +08:00
|
|
|
if (IsClangTidyDiag) {
|
2019-05-19 12:19:14 +08:00
|
|
|
// Check for warning-as-error.
|
|
|
|
// We deliberately let this take precedence over suppression comments
|
|
|
|
// to match clang-tidy's behaviour.
|
|
|
|
if (DiagLevel == DiagnosticsEngine::Warning &&
|
|
|
|
CTContext->treatAsError(CheckName)) {
|
|
|
|
return DiagnosticsEngine::Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for suppression comment. Skip the check for diagnostics not
|
|
|
|
// in the main file, because we don't want that function to query the
|
2019-05-19 12:06:52 +08:00
|
|
|
// source buffer for preamble files. For the same reason, we ask
|
|
|
|
// ShouldSuppressDiagnostic not to follow macro expansions, since
|
|
|
|
// those might take us into a preamble file as well.
|
|
|
|
bool IsInsideMainFile =
|
|
|
|
Info.hasSourceManager() &&
|
2019-07-19 16:33:39 +08:00
|
|
|
isInsideMainFile(Info.getLocation(), Info.getSourceManager());
|
2019-05-19 12:06:52 +08:00
|
|
|
if (IsInsideMainFile && tidy::ShouldSuppressDiagnostic(
|
|
|
|
DiagLevel, Info, *CTContext,
|
|
|
|
/* CheckMacroExpansion = */ false)) {
|
2019-05-19 12:19:14 +08:00
|
|
|
return DiagnosticsEngine::Ignored;
|
2019-05-19 12:06:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-19 12:19:14 +08:00
|
|
|
return DiagLevel;
|
2019-05-19 12:06:52 +08:00
|
|
|
});
|
2019-03-23 02:16:51 +08:00
|
|
|
Preprocessor *PP = &Clang->getPreprocessor();
|
2018-11-16 16:32:23 +08:00
|
|
|
for (const auto &Check : CTChecks) {
|
|
|
|
// FIXME: the PP callbacks skip the entire preamble.
|
|
|
|
// Checks that want to see #includes in the main file do not see them.
|
2019-03-23 02:16:51 +08:00
|
|
|
Check->registerPPCallbacks(Clang->getSourceManager(), PP, PP);
|
2018-11-16 16:32:23 +08:00
|
|
|
Check->registerMatchers(&CTFinder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 06:18:04 +08:00
|
|
|
// Add IncludeFixer which can recover diagnostics caused by missing includes
|
2019-01-28 22:01:55 +08:00
|
|
|
// (e.g. incomplete type) and attach include insertion fixes to diagnostics.
|
|
|
|
llvm::Optional<IncludeFixer> FixIncludes;
|
|
|
|
auto BuildDir = VFS->getCurrentWorkingDirectory();
|
|
|
|
if (Opts.SuggestMissingIncludes && Index && !BuildDir.getError()) {
|
2019-08-10 04:45:24 +08:00
|
|
|
auto Style = getFormatStyleForFile(Filename, Content, VFS.get());
|
2019-01-28 22:01:55 +08:00
|
|
|
auto Inserter = std::make_shared<IncludeInserter>(
|
2019-08-10 04:45:24 +08:00
|
|
|
Filename, Content, Style, BuildDir.get(),
|
2019-04-11 17:36:36 +08:00
|
|
|
&Clang->getPreprocessor().getHeaderSearchInfo());
|
2019-01-28 22:01:55 +08:00
|
|
|
if (Preamble) {
|
|
|
|
for (const auto &Inc : Preamble->Includes.MainFileIncludes)
|
|
|
|
Inserter->addExisting(Inc);
|
|
|
|
}
|
2019-08-10 04:45:24 +08:00
|
|
|
FixIncludes.emplace(Filename, Inserter, *Index,
|
2019-01-28 22:01:55 +08:00
|
|
|
/*IndexRequestLimit=*/5);
|
|
|
|
ASTDiags.contributeFixes([&FixIncludes](DiagnosticsEngine::Level DiagLevl,
|
|
|
|
const clang::Diagnostic &Info) {
|
|
|
|
return FixIncludes->fix(DiagLevl, Info);
|
|
|
|
});
|
2019-02-07 17:23:22 +08:00
|
|
|
Clang->setExternalSemaSource(FixIncludes->unresolvedNameRecorder());
|
2019-01-28 22:01:55 +08:00
|
|
|
}
|
|
|
|
|
2018-02-21 10:39:08 +08:00
|
|
|
// Copy over the includes from the preamble, then combine with the
|
|
|
|
// non-preamble includes below.
|
2018-07-03 16:09:29 +08:00
|
|
|
auto Includes = Preamble ? Preamble->Includes : IncludeStructure{};
|
2018-11-20 18:58:48 +08:00
|
|
|
// Replay the preamble includes so that clang-tidy checks can see them.
|
|
|
|
if (Preamble)
|
|
|
|
ReplayPreamble::attach(Includes, *Clang);
|
|
|
|
// Important: collectIncludeStructure is registered *after* ReplayPreamble!
|
|
|
|
// Otherwise we would collect the replayed includes again...
|
|
|
|
// (We can't *just* use the replayed includes, they don't have Resolved path).
|
2018-07-03 16:09:29 +08:00
|
|
|
Clang->getPreprocessor().addPPCallbacks(
|
|
|
|
collectIncludeStructureCallback(Clang->getSourceManager(), &Includes));
|
2019-08-30 17:33:27 +08:00
|
|
|
// Collect the macro expansions in the main file.
|
|
|
|
std::vector<SourceLocation> MainFileMacroExpLocs;
|
|
|
|
Clang->getPreprocessor().addPPCallbacks(
|
|
|
|
std::make_unique<CollectMainFileMacroExpansions>(
|
|
|
|
Clang->getSourceManager(), MainFileMacroExpLocs));
|
2018-02-21 10:39:08 +08:00
|
|
|
|
2019-02-05 00:19:57 +08:00
|
|
|
// Copy over the includes from the preamble, then combine with the
|
|
|
|
// non-preamble includes below.
|
|
|
|
CanonicalIncludes CanonIncludes;
|
|
|
|
if (Preamble)
|
|
|
|
CanonIncludes = Preamble->CanonIncludes;
|
|
|
|
else
|
2019-09-09 23:32:51 +08:00
|
|
|
CanonIncludes.addSystemHeadersMapping(Clang->getLangOpts());
|
2019-02-05 00:19:57 +08:00
|
|
|
std::unique_ptr<CommentHandler> IWYUHandler =
|
|
|
|
collectIWYUHeaderMaps(&CanonIncludes);
|
|
|
|
Clang->getPreprocessor().addCommentHandler(IWYUHandler.get());
|
|
|
|
|
2019-06-19 22:03:19 +08:00
|
|
|
// Collect tokens of the main file.
|
2019-07-10 17:28:35 +08:00
|
|
|
syntax::TokenCollector CollectTokens(Clang->getPreprocessor());
|
2019-06-19 22:03:19 +08:00
|
|
|
|
2019-06-27 03:50:12 +08:00
|
|
|
if (llvm::Error Err = Action->Execute())
|
|
|
|
log("Execute() failed when building AST for {0}: {1}", MainInput.getFile(),
|
|
|
|
toString(std::move(Err)));
|
2017-07-21 21:29:29 +08:00
|
|
|
|
2019-07-10 17:28:35 +08:00
|
|
|
// We have to consume the tokens before running clang-tidy to avoid collecting
|
|
|
|
// tokens from running the preprocessor inside the checks (only
|
|
|
|
// modernize-use-trailing-return-type does that today).
|
|
|
|
syntax::TokenBuffer Tokens = std::move(CollectTokens).consume();
|
2018-11-16 16:32:23 +08:00
|
|
|
std::vector<Decl *> ParsedDecls = Action->takeTopLevelDecls();
|
|
|
|
// AST traversals should exclude the preamble, to avoid performance cliffs.
|
|
|
|
Clang->getASTContext().setTraversalScope(ParsedDecls);
|
|
|
|
{
|
|
|
|
// Run the AST-dependent part of the clang-tidy checks.
|
|
|
|
// (The preprocessor part ran already, via PPCallbacks).
|
|
|
|
trace::Span Tracer("ClangTidyMatch");
|
|
|
|
CTFinder.matchAST(Clang->getASTContext());
|
|
|
|
}
|
|
|
|
|
2017-07-21 21:29:29 +08:00
|
|
|
// UnitDiagsConsumer is local, we can not store it in CompilerInstance that
|
|
|
|
// has a longer lifetime.
|
2017-12-04 21:49:59 +08:00
|
|
|
Clang->getDiagnostics().setClient(new IgnoreDiagnostics);
|
2018-03-12 23:28:22 +08:00
|
|
|
// CompilerInstance won't run this callback, do it directly.
|
|
|
|
ASTDiags.EndSourceFile();
|
2018-11-16 16:32:23 +08:00
|
|
|
// XXX: This is messy: clang-tidy checks flush some diagnostics at EOF.
|
|
|
|
// However Action->EndSourceFile() would destroy the ASTContext!
|
|
|
|
// So just inform the preprocessor of EOF, while keeping everything alive.
|
|
|
|
Clang->getPreprocessor().EndSourceFile();
|
2017-07-21 21:29:29 +08:00
|
|
|
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
std::vector<Diag> Diags = CompilerInvocationDiags;
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
// Add diagnostics from the preamble, if any.
|
|
|
|
if (Preamble)
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
Diags.insert(Diags.end(), Preamble->Diags.begin(), Preamble->Diags.end());
|
|
|
|
// Finally, add diagnostics coming from the AST.
|
|
|
|
{
|
|
|
|
std::vector<Diag> D = ASTDiags.take(CTContext.getPointer());
|
|
|
|
Diags.insert(Diags.end(), D.begin(), D.end());
|
|
|
|
}
|
2017-11-24 21:04:21 +08:00
|
|
|
return ParsedAST(std::move(Preamble), std::move(Clang), std::move(Action),
|
2019-08-30 17:33:27 +08:00
|
|
|
std::move(Tokens), std::move(MainFileMacroExpLocs),
|
|
|
|
std::move(ParsedDecls), std::move(Diags),
|
2019-07-10 17:28:35 +08:00
|
|
|
std::move(Includes), std::move(CanonIncludes));
|
2017-07-21 21:29:29 +08:00
|
|
|
}
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
ParsedAST::ParsedAST(ParsedAST &&Other) = default;
|
2017-07-21 21:29:29 +08:00
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
ParsedAST &ParsedAST::operator=(ParsedAST &&Other) = default;
|
2017-07-21 21:29:29 +08:00
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
ParsedAST::~ParsedAST() {
|
2017-07-21 21:29:29 +08:00
|
|
|
if (Action) {
|
2018-11-16 16:32:23 +08:00
|
|
|
// We already notified the PP of end-of-file earlier, so detach it first.
|
|
|
|
// We must keep it alive until after EndSourceFile(), Sema relies on this.
|
|
|
|
auto PP = Clang->getPreprocessorPtr(); // Keep PP alive for now.
|
|
|
|
Clang->setPreprocessor(nullptr); // Detach so we don't send EOF again.
|
|
|
|
Action->EndSourceFile(); // Destroy ASTContext and Sema.
|
|
|
|
// Now Sema is gone, it's safe for PP to go out of scope.
|
2017-07-21 21:29:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
ASTContext &ParsedAST::getASTContext() { return Clang->getASTContext(); }
|
2017-07-21 21:29:29 +08:00
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
const ASTContext &ParsedAST::getASTContext() const {
|
2017-07-21 21:29:29 +08:00
|
|
|
return Clang->getASTContext();
|
|
|
|
}
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
Preprocessor &ParsedAST::getPreprocessor() { return Clang->getPreprocessor(); }
|
2017-07-21 21:29:29 +08:00
|
|
|
|
2018-01-10 01:32:00 +08:00
|
|
|
std::shared_ptr<Preprocessor> ParsedAST::getPreprocessorPtr() {
|
|
|
|
return Clang->getPreprocessorPtr();
|
|
|
|
}
|
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
const Preprocessor &ParsedAST::getPreprocessor() const {
|
2017-07-21 21:29:29 +08:00
|
|
|
return Clang->getPreprocessor();
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::ArrayRef<Decl *> ParsedAST::getLocalTopLevelDecls() {
|
2018-05-28 20:23:17 +08:00
|
|
|
return LocalTopLevelDecls;
|
2017-07-21 21:29:29 +08:00
|
|
|
}
|
|
|
|
|
2019-08-30 17:33:27 +08:00
|
|
|
llvm::ArrayRef<SourceLocation> ParsedAST::getMainFileExpansions() const {
|
|
|
|
return MainFileMacroExpLocs;
|
|
|
|
}
|
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
const std::vector<Diag> &ParsedAST::getDiagnostics() const { return Diags; }
|
2017-07-21 21:29:29 +08:00
|
|
|
|
2018-01-25 22:32:21 +08:00
|
|
|
std::size_t ParsedAST::getUsedBytes() const {
|
|
|
|
auto &AST = getASTContext();
|
|
|
|
// FIXME(ibiryukov): we do not account for the dynamically allocated part of
|
2018-03-12 23:28:22 +08:00
|
|
|
// Message and Fixes inside each diagnostic.
|
2018-06-01 22:44:57 +08:00
|
|
|
std::size_t Total =
|
2018-10-20 23:30:37 +08:00
|
|
|
clangd::getUsedBytes(LocalTopLevelDecls) + clangd::getUsedBytes(Diags);
|
2018-06-01 22:44:57 +08:00
|
|
|
|
|
|
|
// FIXME: the rest of the function is almost a direct copy-paste from
|
|
|
|
// libclang's clang_getCXTUResourceUsage. We could share the implementation.
|
|
|
|
|
|
|
|
// Sum up variaous allocators inside the ast context and the preprocessor.
|
|
|
|
Total += AST.getASTAllocatedMemory();
|
|
|
|
Total += AST.getSideTableAllocatedMemory();
|
|
|
|
Total += AST.Idents.getAllocator().getTotalMemory();
|
|
|
|
Total += AST.Selectors.getTotalMemory();
|
|
|
|
|
|
|
|
Total += AST.getSourceManager().getContentCacheSize();
|
|
|
|
Total += AST.getSourceManager().getDataStructureSizes();
|
|
|
|
Total += AST.getSourceManager().getMemoryBufferSizes().malloc_bytes;
|
|
|
|
|
|
|
|
if (ExternalASTSource *Ext = AST.getExternalSource())
|
|
|
|
Total += Ext->getMemoryBufferSizes().malloc_bytes;
|
|
|
|
|
|
|
|
const Preprocessor &PP = getPreprocessor();
|
|
|
|
Total += PP.getTotalMemory();
|
|
|
|
if (PreprocessingRecord *PRec = PP.getPreprocessingRecord())
|
|
|
|
Total += PRec->getTotalMemory();
|
|
|
|
Total += PP.getHeaderSearchInfo().getTotalMemory();
|
|
|
|
|
|
|
|
return Total;
|
2018-01-25 22:32:21 +08:00
|
|
|
}
|
|
|
|
|
2018-07-03 16:09:29 +08:00
|
|
|
const IncludeStructure &ParsedAST::getIncludeStructure() const {
|
|
|
|
return Includes;
|
2018-02-21 10:39:08 +08:00
|
|
|
}
|
|
|
|
|
2019-02-05 00:19:57 +08:00
|
|
|
const CanonicalIncludes &ParsedAST::getCanonicalIncludes() const {
|
|
|
|
return CanonIncludes;
|
|
|
|
}
|
|
|
|
|
2017-11-24 21:04:21 +08:00
|
|
|
ParsedAST::ParsedAST(std::shared_ptr<const PreambleData> Preamble,
|
|
|
|
std::unique_ptr<CompilerInstance> Clang,
|
2017-08-01 23:51:38 +08:00
|
|
|
std::unique_ptr<FrontendAction> Action,
|
2019-06-19 22:03:19 +08:00
|
|
|
syntax::TokenBuffer Tokens,
|
2019-08-30 17:33:27 +08:00
|
|
|
std::vector<SourceLocation> MainFileMacroExpLocs,
|
2018-06-06 00:30:25 +08:00
|
|
|
std::vector<Decl *> LocalTopLevelDecls,
|
2019-02-05 00:19:57 +08:00
|
|
|
std::vector<Diag> Diags, IncludeStructure Includes,
|
|
|
|
CanonicalIncludes CanonIncludes)
|
2017-11-24 21:04:21 +08:00
|
|
|
: Preamble(std::move(Preamble)), Clang(std::move(Clang)),
|
2019-06-19 22:03:19 +08:00
|
|
|
Action(std::move(Action)), Tokens(std::move(Tokens)),
|
2019-08-30 17:33:27 +08:00
|
|
|
MainFileMacroExpLocs(std::move(MainFileMacroExpLocs)),
|
2019-06-19 22:03:19 +08:00
|
|
|
Diags(std::move(Diags)),
|
2018-05-28 20:23:17 +08:00
|
|
|
LocalTopLevelDecls(std::move(LocalTopLevelDecls)),
|
2019-02-05 00:19:57 +08:00
|
|
|
Includes(std::move(Includes)), CanonIncludes(std::move(CanonIncludes)) {
|
2017-07-21 21:29:29 +08:00
|
|
|
assert(this->Clang);
|
|
|
|
assert(this->Action);
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<ParsedAST>
|
|
|
|
buildAST(PathRef FileName, std::unique_ptr<CompilerInvocation> Invocation,
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
llvm::ArrayRef<Diag> CompilerInvocationDiags,
|
2019-01-07 23:45:19 +08:00
|
|
|
const ParseInputs &Inputs,
|
2019-04-04 20:56:03 +08:00
|
|
|
std::shared_ptr<const PreambleData> Preamble) {
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
trace::Span Tracer("BuildAST");
|
|
|
|
SPAN_ATTACH(Tracer, "File", FileName);
|
|
|
|
|
2018-10-02 18:43:55 +08:00
|
|
|
auto VFS = Inputs.FS;
|
|
|
|
if (Preamble && Preamble->StatCache)
|
|
|
|
VFS = Preamble->StatCache->getConsumingFS(std::move(VFS));
|
|
|
|
if (VFS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory)) {
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
log("Couldn't set working directory when building the preamble.");
|
|
|
|
// We proceed anyway, our lit-tests rely on results for non-existing working
|
|
|
|
// dirs.
|
|
|
|
}
|
|
|
|
|
2019-07-22 19:12:16 +08:00
|
|
|
return ParsedAST::build(
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
std::make_unique<CompilerInvocation>(*Invocation),
|
|
|
|
CompilerInvocationDiags, Preamble,
|
2019-07-22 19:12:16 +08:00
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, FileName),
|
|
|
|
std::move(VFS), Inputs.Index, Inputs.Opts);
|
[clangd] Keep only a limited number of idle ASTs in memory
Summary:
After this commit, clangd will only keep the last 3 accessed ASTs in
memory. Preambles for each of the opened files are still kept in
memory to make completion and AST rebuilds fast.
AST rebuilds are usually fast enough, but having the last ASTs in
memory still considerably improves latency of operations like
findDefinition and documeneHighlight, which are often sent multiple
times a second when moving around the code. So keeping some of the last
accessed ASTs in memory seems like a reasonable tradeoff.
Reviewers: sammccall
Reviewed By: sammccall
Subscribers: malaperle, arphaman, klimek, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47063
llvm-svn: 333737
2018-06-01 18:08:43 +08:00
|
|
|
}
|
|
|
|
|
2018-10-20 23:30:37 +08:00
|
|
|
} // namespace clangd
|
2018-11-16 16:32:23 +08:00
|
|
|
namespace tidy {
|
|
|
|
// Force the linker to link in Clang-tidy modules.
|
|
|
|
#define LINK_TIDY_MODULE(X) \
|
|
|
|
extern volatile int X##ModuleAnchorSource; \
|
|
|
|
static int LLVM_ATTRIBUTE_UNUSED X##ModuleAnchorDestination = \
|
|
|
|
X##ModuleAnchorSource
|
|
|
|
LINK_TIDY_MODULE(Abseil);
|
2019-08-23 17:13:23 +08:00
|
|
|
LINK_TIDY_MODULE(Android);
|
2018-11-16 16:32:23 +08:00
|
|
|
LINK_TIDY_MODULE(Boost);
|
|
|
|
LINK_TIDY_MODULE(Bugprone);
|
2019-08-23 17:13:23 +08:00
|
|
|
LINK_TIDY_MODULE(CERT);
|
2018-11-16 16:32:23 +08:00
|
|
|
LINK_TIDY_MODULE(CppCoreGuidelines);
|
|
|
|
LINK_TIDY_MODULE(Fuchsia);
|
|
|
|
LINK_TIDY_MODULE(Google);
|
2019-08-23 17:13:23 +08:00
|
|
|
LINK_TIDY_MODULE(HICPP);
|
|
|
|
LINK_TIDY_MODULE(LinuxKernel);
|
|
|
|
LINK_TIDY_MODULE(LLVM);
|
2018-11-16 16:32:23 +08:00
|
|
|
LINK_TIDY_MODULE(Misc);
|
|
|
|
LINK_TIDY_MODULE(Modernize);
|
2019-08-23 17:13:23 +08:00
|
|
|
// LINK_TIDY_MODULE(MPI); // clangd doesn't support static analyzer.
|
|
|
|
LINK_TIDY_MODULE(ObjC);
|
|
|
|
LINK_TIDY_MODULE(OpenMP);
|
2018-11-16 16:32:23 +08:00
|
|
|
LINK_TIDY_MODULE(Performance);
|
|
|
|
LINK_TIDY_MODULE(Portability);
|
|
|
|
LINK_TIDY_MODULE(Readability);
|
|
|
|
LINK_TIDY_MODULE(Zircon);
|
|
|
|
#undef LINK_TIDY_MODULE
|
|
|
|
} // namespace tidy
|
2018-10-20 23:30:37 +08:00
|
|
|
} // namespace clang
|