15 #include "clang/AST/RecursiveASTVisitor.h" 16 #include "clang/Frontend/CompilerInvocation.h" 17 #include "clang/Frontend/Utils.h" 25 ImportThunk =
testPath(
"import_thunk.h");
29 std::string ThunkContents =
"#import \"" + FullHeaderName +
"\"\n";
32 Files[FullFilename] =
Code;
34 Files[ImportThunk] = ThunkContents;
36 std::vector<const char *> Cmd = {
"clang"};
40 Cmd.push_back(
"-include");
42 : FullHeaderName.c_str());
46 Cmd.push_back(
"-fno-ms-compatibility");
51 Cmd.push_back(FullFilename.c_str());
66 assert(CI &&
"Failed to build compilation invocation.");
74 if (!
AST.hasValue()) {
75 ADD_FAILURE() <<
"Failed to build code:\n" <<
Code;
76 llvm_unreachable(
"Failed to build TestTU!");
78 return std::move(*
AST);
84 AST.getPreprocessorPtr(),
85 AST.getCanonicalIncludes()));
90 auto Idx = std::make_unique<FileIndex>(
true);
91 Idx->updatePreamble(
Filename,
AST.getASTContext(),
AST.getPreprocessorPtr(),
92 AST.getCanonicalIncludes());
94 return std::move(Idx);
98 const Symbol *Result =
nullptr;
99 for (
const Symbol &S : Slab) {
100 if (QName != (S.Scope + S.Name).str())
103 ADD_FAILURE() <<
"Multiple symbols named " << QName <<
":\n" 104 << *Result <<
"\n---\n" 106 assert(
false &&
"QName is not unique");
111 ADD_FAILURE() <<
"No symbol named " << QName <<
" in " 112 << ::testing::PrintToString(Slab);
113 assert(
false &&
"No symbol with QName");
119 llvm::SmallVector<llvm::StringRef, 4> Components;
120 QName.split(Components,
"::");
123 auto LookupDecl = [&
Ctx](
const DeclContext &Scope,
124 llvm::StringRef
Name) ->
const NamedDecl & {
125 auto LookupRes = Scope.lookup(DeclarationName(&
Ctx.Idents.get(
Name)));
126 assert(!LookupRes.empty() &&
"Lookup failed");
127 assert(LookupRes.size() == 1 &&
"Lookup returned multiple results");
128 return *LookupRes.front();
131 const DeclContext *Scope =
Ctx.getTranslationUnitDecl();
132 for (
auto NameIt = Components.begin(), End = Components.end() - 1;
133 NameIt != End; ++NameIt) {
134 Scope = &cast<DeclContext>(LookupDecl(*Scope, *NameIt));
136 return LookupDecl(*Scope, Components.back());
140 std::function<
bool(
const NamedDecl &)> Filter) {
141 struct Visitor : RecursiveASTVisitor<Visitor> {
143 llvm::SmallVector<const NamedDecl *, 1>
Decls;
144 bool VisitNamedDecl(
const NamedDecl *ND) {
151 Visitor.TraverseDecl(AST.
getASTContext().getTranslationUnitDecl());
152 if (Visitor.Decls.size() != 1) {
153 ADD_FAILURE() << Visitor.Decls.size() <<
" symbols matched.";
154 assert(Visitor.Decls.size() == 1);
156 return *Visitor.Decls.front();
160 return findDecl(AST, [Name](
const NamedDecl &ND) {
161 if (
auto *ID = ND.getIdentifier())
162 if (ID->getName() ==
Name)
llvm::Optional< std::string > Checks
Checks filter.
An immutable symbol container that stores a set of symbols.
llvm::Optional< std::string > ClangTidyChecks
StoreDiags collects the diagnostics that can later be reported by clangd.
llvm::Optional< ParsedAST > buildAST(PathRef FileName, std::unique_ptr< CompilerInvocation > Invocation, llvm::ArrayRef< Diag > CompilerInvocationDiags, const ParseInputs &Inputs, std::shared_ptr< const PreambleData > Preamble)
Build an AST from provided user inputs.
bool SuggestMissingIncludes
tidy::ClangTidyOptions ClangTidyOpts
const NamedDecl & findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name)
ASTContext & getASTContext()
Note that the returned ast will not contain decls from the preamble that were not deserialized during...
std::shared_ptr< const PreambleData > buildPreamble(PathRef FileName, CompilerInvocation &CI, std::shared_ptr< const PreambleData > OldPreamble, const tooling::CompileCommand &OldCompileCommand, const ParseInputs &Inputs, bool StoreInMemory, PreambleParsedCallback PreambleCallback)
Build a preamble for the new inputs unless an old one can be reused.
std::vector< const char * > ExtraArgs
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > buildTestFS(llvm::StringMap< std::string > const &Files, llvm::StringMap< time_t > const &Timestamps)
const SymbolIndex * ExternalIndex
std::string testPath(PathRef File)
llvm::Optional< std::string > ClangTidyWarningsAsErrors
static constexpr llvm::StringLiteral Name
llvm::Optional< std::string > WarningsAsErrors
WarningsAsErrors filter.
Stores and provides access to parsed AST.
SymbolSlab headerSymbols() const
The class presents a C++ symbol, e.g.
const PreambleData * Preamble
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
const Symbol & findSymbol(const SymbolSlab &Slab, llvm::StringRef QName)
std::unique_ptr< CompilerInvocation > buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D, std::vector< std::string > *CC1Args)
Builds compiler invocation that could be used to build AST or preamble.
std::unique_ptr< SymbolIndex > index() const
llvm::SmallDenseMap< const NamedDecl *, RelSet > Decls
std::string HeaderFilename
llvm::StringMap< std::string > Files
llvm::StringMap< std::string > AdditionalFiles
std::vector< Diag > take(const clang::tidy::ClangTidyContext *Tidy=nullptr)
SlabTuple indexHeaderSymbols(ASTContext &AST, std::shared_ptr< Preprocessor > PP, const CanonicalIncludes &Includes)
Index declarations from AST and macros from PP that are declared in included headers.
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)