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"
|
2020-06-29 23:05:51 +08:00
|
|
|
#include "../clang-tidy/ClangTidyCheck.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"
|
2020-04-02 16:53:45 +08:00
|
|
|
#include "Preamble.h"
|
2018-03-12 23:28:22 +08:00
|
|
|
#include "SourceCode.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"
|
[clangd] Move non-clang base pieces into separate support/ lib. NFCI
Summary:
This enforces layering, reduces a sprawling clangd/ directory, and makes life
easier for embedders.
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79014
2020-04-28 23:49:17 +08:00
|
|
|
#include "support/Logger.h"
|
|
|
|
#include "support/Trace.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"
|
2020-06-16 18:16:24 +08:00
|
|
|
#include "llvm/ADT/StringRef.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>
|
2020-04-02 16:53:45 +08:00
|
|
|
#include <vector>
|
2017-06-29 00:12:10 +08:00
|
|
|
|
2019-09-27 20:56:14 +08:00
|
|
|
// Force the linker to link in Clang-tidy modules.
|
|
|
|
// clangd doesn't support the static analyzer.
|
|
|
|
#define CLANG_TIDY_DISABLE_STATIC_ANALYZER_CHECKS
|
|
|
|
#include "../clang-tidy/ClangTidyForceLinker.h"
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
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.
|
2020-06-02 17:52:55 +08:00
|
|
|
static void attach(std::vector<Inclusion> Includes, CompilerInstance &Clang,
|
2020-02-20 00:36:41 +08:00
|
|
|
const PreambleBounds &PB) {
|
2018-11-20 18:58:48 +08:00
|
|
|
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;
|
2020-02-20 00:36:41 +08:00
|
|
|
PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(new ReplayPreamble(
|
2020-06-02 17:52:55 +08:00
|
|
|
std::move(Includes), ExistingCallbacks, Clang.getSourceManager(), PP,
|
2020-02-20 00:36:41 +08:00
|
|
|
Clang.getLangOpts(), PB)));
|
2018-11-20 18:58:48 +08:00
|
|
|
// 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:
|
2020-06-02 17:52:55 +08:00
|
|
|
ReplayPreamble(std::vector<Inclusion> Includes, PPCallbacks *Delegate,
|
2018-11-20 18:58:48 +08:00
|
|
|
const SourceManager &SM, Preprocessor &PP,
|
2020-02-20 00:36:41 +08:00
|
|
|
const LangOptions &LangOpts, const PreambleBounds &PB)
|
2020-06-02 17:52:55 +08:00
|
|
|
: Includes(std::move(Includes)), Delegate(Delegate), SM(SM), PP(PP) {
|
2020-02-20 00:36:41 +08:00
|
|
|
// Only tokenize the preamble section of the main file, as we are not
|
|
|
|
// interested in the rest of the tokens.
|
|
|
|
MainFileTokens = syntax::tokenize(
|
|
|
|
syntax::FileRange(SM.getMainFileID(), 0, PB.Size), SM, LangOpts);
|
|
|
|
}
|
2018-11-20 18:58:48 +08:00
|
|
|
|
|
|
|
// 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() {
|
2020-06-02 17:52:55 +08:00
|
|
|
for (const auto &Inc : Includes) {
|
2018-11-20 18:58:48 +08:00
|
|
|
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
|
|
|
|
2020-02-20 00:36:41 +08:00
|
|
|
// Re-lex the #include directive to find its interesting parts.
|
|
|
|
auto HashLoc = SM.getComposedLoc(SM.getMainFileID(), Inc.HashOffset);
|
|
|
|
auto HashTok = llvm::partition_point(MainFileTokens,
|
|
|
|
[&HashLoc](const syntax::Token &T) {
|
|
|
|
return T.location() < HashLoc;
|
|
|
|
});
|
|
|
|
assert(HashTok != MainFileTokens.end() && HashTok->kind() == tok::hash);
|
|
|
|
|
|
|
|
auto IncludeTok = std::next(HashTok);
|
|
|
|
assert(IncludeTok != MainFileTokens.end());
|
|
|
|
|
|
|
|
auto FileTok = std::next(IncludeTok);
|
|
|
|
assert(FileTok != MainFileTokens.end());
|
|
|
|
|
|
|
|
// Create a fake import/include token, none of the callers seem to care
|
|
|
|
// about clang::Token::Flags.
|
|
|
|
Token SynthesizedIncludeTok;
|
|
|
|
SynthesizedIncludeTok.startToken();
|
|
|
|
SynthesizedIncludeTok.setLocation(IncludeTok->location());
|
|
|
|
SynthesizedIncludeTok.setLength(IncludeTok->length());
|
|
|
|
SynthesizedIncludeTok.setKind(tok::raw_identifier);
|
|
|
|
SynthesizedIncludeTok.setRawIdentifierData(IncludeTok->text(SM).data());
|
|
|
|
PP.LookUpIdentifierInfo(SynthesizedIncludeTok);
|
|
|
|
|
|
|
|
// Same here, create a fake one for Filename, including angles or quotes.
|
|
|
|
Token SynthesizedFilenameTok;
|
|
|
|
SynthesizedFilenameTok.startToken();
|
|
|
|
SynthesizedFilenameTok.setLocation(FileTok->location());
|
|
|
|
// Note that we can't make use of FileTok->length/text in here as in the
|
|
|
|
// case of angled includes this will contain tok::less instead of
|
|
|
|
// filename. Whereas Inc.Written contains the full header name including
|
|
|
|
// quotes/angles.
|
|
|
|
SynthesizedFilenameTok.setLength(Inc.Written.length());
|
|
|
|
SynthesizedFilenameTok.setKind(tok::header_name);
|
|
|
|
SynthesizedFilenameTok.setLiteralData(Inc.Written.data());
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringRef WrittenFilename =
|
|
|
|
llvm::StringRef(Inc.Written).drop_front().drop_back();
|
2020-02-20 00:36:41 +08:00
|
|
|
Delegate->InclusionDirective(HashTok->location(), SynthesizedIncludeTok,
|
|
|
|
WrittenFilename, Inc.Written.front() == '<',
|
|
|
|
FileTok->range(SM).toCharRange(SM), File,
|
|
|
|
"SearchPath", "RelPath",
|
|
|
|
/*Imported=*/nullptr, Inc.FileKind);
|
2018-11-20 18:58:48 +08:00
|
|
|
if (File)
|
2019-08-27 09:03:25 +08:00
|
|
|
// FIXME: Use correctly named FileEntryRef.
|
2020-02-20 00:36:41 +08:00
|
|
|
Delegate->FileSkipped(FileEntryRef(File->getName(), *File),
|
|
|
|
SynthesizedFilenameTok, 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 17:52:55 +08:00
|
|
|
const std::vector<Inclusion> Includes;
|
2018-11-20 18:58:48 +08:00
|
|
|
PPCallbacks *Delegate;
|
|
|
|
const SourceManager &SM;
|
|
|
|
Preprocessor &PP;
|
2020-02-20 00:36:41 +08:00
|
|
|
std::vector<syntax::Token> MainFileTokens;
|
2018-11-20 18:58:48 +08:00
|
|
|
};
|
|
|
|
|
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>
|
2020-04-27 06:13:56 +08:00
|
|
|
ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
|
[clangd] Track document versions, include them with diags, enhance logs
Summary:
This ties to an LSP feature (diagnostic versioning) but really a lot
of the value is in being able to log what's happening with file versions
and queues more descriptively and clearly.
As such it's fairly invasive, for a logging patch :-\
Key decisions:
- at the LSP layer, we don't reqire the client to provide versions (LSP
makes it mandatory but we never enforced it). If not provided,
versions start at 0 and increment. DraftStore handles this.
- don't propagate magically using contexts, but rather manually:
addDocument -> ParseInputs -> (ParsedAST, Preamble, various callbacks)
Context-propagation would hide the versions from ClangdServer, which
would make producing good log messages hard
- within ClangdServer, treat versions as opaque and unordered.
std::string is a convenient type for this, and allows richer versions
for embedders. They're "mandatory" but "null" is a reasonable default.
Subscribers: ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75582
2020-03-04 07:33:29 +08:00
|
|
|
std::unique_ptr<clang::CompilerInvocation> CI,
|
[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,
|
2020-04-27 06:13:56 +08:00
|
|
|
std::shared_ptr<const PreambleData> Preamble) {
|
|
|
|
trace::Span Tracer("BuildAST");
|
|
|
|
SPAN_ATTACH(Tracer, "File", Filename);
|
|
|
|
|
2020-06-18 00:09:54 +08:00
|
|
|
auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
|
2020-04-27 06:13:56 +08:00
|
|
|
if (Preamble && Preamble->StatCache)
|
|
|
|
VFS = Preamble->StatCache->getConsumingFS(std::move(VFS));
|
|
|
|
|
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
|
|
|
|
2020-04-25 09:03:01 +08:00
|
|
|
// This is on-by-default in windows to allow parsing SDK headers, but it
|
|
|
|
// breaks many features. Disable it for the main-file (not preamble).
|
|
|
|
CI->getLangOpts()->DelayedTemplateParsing = false;
|
2020-03-31 22:09:49 +08:00
|
|
|
|
2018-03-12 23:28:22 +08:00
|
|
|
StoreDiags ASTDiags;
|
2019-01-28 22:01:55 +08:00
|
|
|
|
2020-04-02 16:53:45 +08:00
|
|
|
llvm::Optional<PreamblePatch> Patch;
|
|
|
|
if (Preamble) {
|
|
|
|
Patch = PreamblePatch::create(Filename, Inputs, *Preamble);
|
|
|
|
Patch->apply(*CI);
|
|
|
|
}
|
2020-04-27 06:13:56 +08:00
|
|
|
auto Clang = prepareCompilerInstance(
|
|
|
|
std::move(CI), PreamblePCH,
|
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(Inputs.Contents, Filename), 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.
|
2020-04-05 14:28:11 +08:00
|
|
|
// Clang-tidy has some limitations to ensure reasonable performance:
|
2018-11-16 16:32:23 +08:00
|
|
|
// - 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,
|
2020-04-27 06:13:56 +08:00
|
|
|
tidy::configurationAsText(Inputs.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>(
|
2020-04-27 06:13:56 +08:00
|
|
|
tidy::ClangTidyGlobalOptions(), Inputs.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);
|
2019-09-26 21:55:01 +08:00
|
|
|
CTChecks = CTFactories.createChecks(CTContext.getPointer());
|
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 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
|
2020-03-30 03:13:13 +08:00
|
|
|
// shouldSuppressDiagnostic to avoid I/O.
|
2020-05-11 03:08:18 +08:00
|
|
|
// We let suppression comments take precedence over warning-as-error
|
|
|
|
// to match clang-tidy's behaviour.
|
2019-05-19 12:06:52 +08:00
|
|
|
bool IsInsideMainFile =
|
|
|
|
Info.hasSourceManager() &&
|
2019-07-19 16:33:39 +08:00
|
|
|
isInsideMainFile(Info.getLocation(), Info.getSourceManager());
|
2020-03-30 03:13:13 +08:00
|
|
|
if (IsInsideMainFile &&
|
|
|
|
tidy::shouldSuppressDiagnostic(DiagLevel, Info, *CTContext,
|
|
|
|
/*AllowIO=*/false)) {
|
2019-05-19 12:19:14 +08:00
|
|
|
return DiagnosticsEngine::Ignored;
|
2019-05-19 12:06:52 +08:00
|
|
|
}
|
2020-05-11 03:08:18 +08:00
|
|
|
|
|
|
|
// Check for warning-as-error.
|
|
|
|
if (DiagLevel == DiagnosticsEngine::Warning &&
|
|
|
|
CTContext->treatAsError(CheckName)) {
|
|
|
|
return DiagnosticsEngine::Error;
|
|
|
|
}
|
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) {
|
[clang-tidy] Added virtual isLanguageVersionSupported to ClangTidyCheck
Summary:
Motivated by [[ https://bugs.llvm.org/show_bug.cgi?id=45045 | Tune inspections to a specific C++ standard. ]]
Moves the isLanguageVersionSupported virtual function from `MakeSmartPtrCheck` to the base `ClangTidyCheck` class.
This will disable registering matchers or pp callbacks on unsupported language versions for a check.
Having it as a standalone function is cleaner than manually disabling the check in the register function and should hopefully
encourage check developers to actually restrict the check based on language version.
As an added bonus this could enable automatic detection of what language version a check runs on for the purpose of documentation generation
Reviewers: aaron.ballman, gribozavr2, Eugene.Zelenko, JonasToth, alexfh, hokein
Reviewed By: gribozavr2
Subscribers: xazax.hun, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75289
2020-02-28 21:03:30 +08:00
|
|
|
if (!Check->isLanguageVersionSupported(CTContext->getLangOpts()))
|
|
|
|
continue;
|
2018-11-16 16:32:23 +08:00
|
|
|
// 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();
|
2020-04-27 06:13:56 +08:00
|
|
|
if (Inputs.Opts.SuggestMissingIncludes && Inputs.Index &&
|
|
|
|
!BuildDir.getError()) {
|
2020-06-18 00:09:54 +08:00
|
|
|
auto Style = getFormatStyleForFile(Filename, Inputs.Contents, *Inputs.TFS);
|
2019-01-28 22:01:55 +08:00
|
|
|
auto Inserter = std::make_shared<IncludeInserter>(
|
2020-04-27 06:13:56 +08:00
|
|
|
Filename, Inputs.Contents, 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);
|
|
|
|
}
|
2020-04-27 06:13:56 +08:00
|
|
|
FixIncludes.emplace(Filename, Inserter, *Inputs.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
|
|
|
}
|
|
|
|
|
2020-04-02 16:53:45 +08:00
|
|
|
IncludeStructure Includes;
|
|
|
|
// If we are using a preamble, copy existing includes.
|
|
|
|
if (Preamble) {
|
|
|
|
Includes = Preamble->Includes;
|
|
|
|
Includes.MainFileIncludes = Patch->preambleIncludes();
|
|
|
|
// Replay the preamble includes so that clang-tidy checks can see them.
|
2020-06-02 17:52:55 +08:00
|
|
|
ReplayPreamble::attach(Patch->preambleIncludes(), *Clang,
|
2020-06-17 03:21:45 +08:00
|
|
|
Patch->modifiedBounds());
|
2020-04-02 16:53:45 +08:00
|
|
|
}
|
2018-11-20 18:58:48 +08:00
|
|
|
// 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-09-24 19:14:06 +08:00
|
|
|
// Copy over the macros in the preamble region of the main file, and combine
|
|
|
|
// with non-preamble macros below.
|
|
|
|
MainFileMacros Macros;
|
|
|
|
if (Preamble)
|
|
|
|
Macros = Preamble->Macros;
|
2019-08-30 17:33:27 +08:00
|
|
|
Clang->getPreprocessor().addPPCallbacks(
|
2019-09-10 18:10:36 +08:00
|
|
|
std::make_unique<CollectMainFileMacros>(Clang->getSourceManager(),
|
2020-03-01 23:05:12 +08:00
|
|
|
Macros));
|
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());
|
|
|
|
}
|
2020-04-27 06:13:56 +08:00
|
|
|
return ParsedAST(Inputs.Version, std::move(Preamble), std::move(Clang),
|
[clangd] Track document versions, include them with diags, enhance logs
Summary:
This ties to an LSP feature (diagnostic versioning) but really a lot
of the value is in being able to log what's happening with file versions
and queues more descriptively and clearly.
As such it's fairly invasive, for a logging patch :-\
Key decisions:
- at the LSP layer, we don't reqire the client to provide versions (LSP
makes it mandatory but we never enforced it). If not provided,
versions start at 0 and increment. DraftStore handles this.
- don't propagate magically using contexts, but rather manually:
addDocument -> ParseInputs -> (ParsedAST, Preamble, various callbacks)
Context-propagation would hide the versions from ClangdServer, which
would make producing good log messages hard
- within ClangdServer, treat versions as opaque and unordered.
std::string is a convenient type for this, and allows richer versions
for embedders. They're "mandatory" but "null" is a reasonable default.
Subscribers: ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75582
2020-03-04 07:33:29 +08:00
|
|
|
std::move(Action), std::move(Tokens), std::move(Macros),
|
|
|
|
std::move(ParsedDecls), std::move(Diags),
|
|
|
|
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-09-24 19:14:06 +08:00
|
|
|
const MainFileMacros &ParsedAST::getMacros() const { return Macros; }
|
2019-08-30 17:33:27 +08:00
|
|
|
|
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.
|
|
|
|
|
2020-04-05 14:28:11 +08:00
|
|
|
// Sum up various allocators inside the ast context and the preprocessor.
|
2018-06-01 22:44:57 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
[clangd] Track document versions, include them with diags, enhance logs
Summary:
This ties to an LSP feature (diagnostic versioning) but really a lot
of the value is in being able to log what's happening with file versions
and queues more descriptively and clearly.
As such it's fairly invasive, for a logging patch :-\
Key decisions:
- at the LSP layer, we don't reqire the client to provide versions (LSP
makes it mandatory but we never enforced it). If not provided,
versions start at 0 and increment. DraftStore handles this.
- don't propagate magically using contexts, but rather manually:
addDocument -> ParseInputs -> (ParsedAST, Preamble, various callbacks)
Context-propagation would hide the versions from ClangdServer, which
would make producing good log messages hard
- within ClangdServer, treat versions as opaque and unordered.
std::string is a convenient type for this, and allows richer versions
for embedders. They're "mandatory" but "null" is a reasonable default.
Subscribers: ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75582
2020-03-04 07:33:29 +08:00
|
|
|
ParsedAST::ParsedAST(llvm::StringRef Version,
|
|
|
|
std::shared_ptr<const PreambleData> Preamble,
|
2017-11-24 21:04:21 +08:00
|
|
|
std::unique_ptr<CompilerInstance> Clang,
|
2017-08-01 23:51:38 +08:00
|
|
|
std::unique_ptr<FrontendAction> Action,
|
2019-09-24 19:14:06 +08:00
|
|
|
syntax::TokenBuffer Tokens, MainFileMacros Macros,
|
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)
|
[clangd] Track document versions, include them with diags, enhance logs
Summary:
This ties to an LSP feature (diagnostic versioning) but really a lot
of the value is in being able to log what's happening with file versions
and queues more descriptively and clearly.
As such it's fairly invasive, for a logging patch :-\
Key decisions:
- at the LSP layer, we don't reqire the client to provide versions (LSP
makes it mandatory but we never enforced it). If not provided,
versions start at 0 and increment. DraftStore handles this.
- don't propagate magically using contexts, but rather manually:
addDocument -> ParseInputs -> (ParsedAST, Preamble, various callbacks)
Context-propagation would hide the versions from ClangdServer, which
would make producing good log messages hard
- within ClangdServer, treat versions as opaque and unordered.
std::string is a convenient type for this, and allows richer versions
for embedders. They're "mandatory" but "null" is a reasonable default.
Subscribers: ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75582
2020-03-04 07:33:29 +08:00
|
|
|
: Version(Version), 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-09-24 19:14:06 +08:00
|
|
|
Macros(std::move(Macros)), 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);
|
|
|
|
}
|
|
|
|
|
2020-04-08 03:18:00 +08:00
|
|
|
llvm::Optional<llvm::StringRef> ParsedAST::preambleVersion() const {
|
|
|
|
if (!Preamble)
|
|
|
|
return llvm::None;
|
|
|
|
return llvm::StringRef(Preamble->Version);
|
|
|
|
}
|
2018-10-20 23:30:37 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|