Skip to content

Commit

Permalink
Merge master:d14cf45735b into amd-gfx
Browse files Browse the repository at this point in the history
Change-Id: I68dd6da83b37e651d0f3441919295daa177fab5b
  • Loading branch information
jayfoad committed Aug 21, 2020
2 parents a77b6e9 + d14cf45 commit 58d7da9
Show file tree
Hide file tree
Showing 494 changed files with 13,679 additions and 7,823 deletions.
20 changes: 12 additions & 8 deletions clang-tools-extra/clang-tidy/abseil/AbseilMatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ AST_POLYMORPHIC_MATCHER(
if (PrefixPosition == StringRef::npos)
return false;
Path = Path.drop_front(PrefixPosition + AbslPrefix.size());
static const char *AbseilLibraries[] = {
"algorithm", "base", "container", "debugging", "flags",
"hash", "iterator", "memory", "meta", "numeric",
"random", "strings", "synchronization", "status", "time",
"types", "utility"};
return std::any_of(
std::begin(AbseilLibraries), std::end(AbseilLibraries),
[&](const char *Library) { return Path.startswith(Library); });
static const char *AbseilLibraries[] = {"algorithm", "base",
"container", "debugging",
"flags", "hash",
"iterator", "memory",
"meta", "numeric",
"random", "status",
"strings", "synchronization",
"time", "types",
"utility"};
return llvm::any_of(AbseilLibraries, [&](const char *Library) {
return Path.startswith(Library);
});
}

} // namespace ast_matchers
Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/clangd/ClangdServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
Callbacks *Callbacks)
: ConfigProvider(Opts.ConfigProvider), TFS(TFS),
DynamicIdx(Opts.BuildDynamicSymbolIndex
? new FileIndex(Opts.HeavyweightDynamicSymbolIndex)
? new FileIndex(Opts.HeavyweightDynamicSymbolIndex,
Opts.CollectMainFileRefs)
: nullptr),
GetClangTidyOptions(Opts.GetClangTidyOptions),
SuggestMissingIncludes(Opts.SuggestMissingIncludes),
Expand Down
3 changes: 3 additions & 0 deletions clang-tools-extra/clangd/ClangdServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ class ClangdServer {
/// on background threads. The index is stored in the project root.
bool BackgroundIndex = false;

/// Store refs to main-file symbols in the index.
bool CollectMainFileRefs = false;

/// If set, use this index to augment code completion results.
SymbolIndex *StaticIndex = nullptr;

Expand Down
38 changes: 25 additions & 13 deletions clang-tools-extra/clangd/FindTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ CXXRecordDecl *resolveTypeToRecordDecl(const Type *T) {
std::vector<const NamedDecl *> getMembersReferencedViaDependentName(
const Type *T,
llvm::function_ref<DeclarationName(ASTContext &)> NameFactory,
bool IsNonstaticMember) {
llvm::function_ref<bool(const NamedDecl *ND)> Filter) {
if (!T)
return {};
if (auto *ET = T->getAs<EnumType>()) {
Expand All @@ -113,17 +113,22 @@ std::vector<const NamedDecl *> getMembersReferencedViaDependentName(
return {};
RD = RD->getDefinition();
DeclarationName Name = NameFactory(RD->getASTContext());
return RD->lookupDependentName(Name, [=](const NamedDecl *D) {
return IsNonstaticMember ? D->isCXXInstanceMember()
: !D->isCXXInstanceMember();
});
return RD->lookupDependentName(Name, Filter);
}
return {};
}

// Given the type T of a dependent expression that appears of the LHS of a "->",
// heuristically find a corresponding pointee type in whose scope we could look
// up the name appearing on the RHS.
const auto NonStaticFilter = [](const NamedDecl *D) {
return D->isCXXInstanceMember();
};
const auto StaticFilter = [](const NamedDecl *D) {
return !D->isCXXInstanceMember();
};
const auto ValueFilter = [](const NamedDecl *D) { return isa<ValueDecl>(D); };

// Given the type T of a dependent expression that appears of the LHS of a
// "->", heuristically find a corresponding pointee type in whose scope we
// could look up the name appearing on the RHS.
const Type *getPointeeType(const Type *T) {
if (!T)
return nullptr;
Expand All @@ -141,7 +146,7 @@ const Type *getPointeeType(const Type *T) {
[](ASTContext &Ctx) {
return Ctx.DeclarationNames.getCXXOperatorName(OO_Arrow);
},
/*IsNonStaticMember=*/true);
NonStaticFilter);
if (ArrowOps.empty())
return nullptr;

Expand Down Expand Up @@ -187,13 +192,12 @@ std::vector<const NamedDecl *> resolveExprToDecls(const Expr *E) {
}
return getMembersReferencedViaDependentName(
BaseType, [ME](ASTContext &) { return ME->getMember(); },
/*IsNonstaticMember=*/true);
NonStaticFilter);
}
if (const auto *RE = dyn_cast<DependentScopeDeclRefExpr>(E)) {
return getMembersReferencedViaDependentName(
RE->getQualifier()->getAsType(),
[RE](ASTContext &) { return RE->getDeclName(); },
/*IsNonstaticMember=*/false);
[RE](ASTContext &) { return RE->getDeclName(); }, StaticFilter);
}
if (const auto *CE = dyn_cast<CallExpr>(E)) {
const auto *CalleeType = resolveExprToType(CE->getCallee());
Expand Down Expand Up @@ -291,7 +295,6 @@ const NamedDecl *getTemplatePattern(const NamedDecl *D) {
// CXXDependentScopeMemberExpr, but some other constructs remain to be handled:
// - DependentTemplateSpecializationType,
// - DependentNameType
// - UnresolvedUsingValueDecl
// - UnresolvedUsingTypenameDecl
struct TargetFinder {
using RelSet = DeclRelationSet;
Expand Down Expand Up @@ -345,6 +348,15 @@ struct TargetFinder {
} else if (const auto *NAD = dyn_cast<NamespaceAliasDecl>(D)) {
add(NAD->getUnderlyingDecl(), Flags | Rel::Underlying);
Flags |= Rel::Alias; // continue with the alias
} else if (const UnresolvedUsingValueDecl *UUVD =
dyn_cast<UnresolvedUsingValueDecl>(D)) {
for (const NamedDecl *Target : getMembersReferencedViaDependentName(
UUVD->getQualifier()->getAsType(),
[UUVD](ASTContext &) { return UUVD->getNameInfo().getName(); },
ValueFilter)) {
add(Target, Flags | Rel::Underlying);
}
Flags |= Rel::Alias; // continue with the alias
} else if (const UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D)) {
// Include the using decl, but don't traverse it. This may end up
// including *all* shadows, which we don't want.
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/clangd/XRefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ locateASTReferent(SourceLocation CurLoc, const syntax::Token *TouchedIdentifier,

// Give the underlying decl if navigation is triggered on a non-renaming
// alias.
if (llvm::isa<UsingDecl>(D)) {
if (llvm::isa<UsingDecl>(D) || llvm::isa<UnresolvedUsingValueDecl>(D)) {
// FIXME: address more complicated cases. TargetDecl(... Underlying) gives
// all overload candidates, we only want the targeted one if the cursor is
// on an using-alias usage, workround it with getDeclAtPosition.
Expand Down
2 changes: 2 additions & 0 deletions clang-tools-extra/clangd/index/Background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ BackgroundIndex::BackgroundIndex(
BackgroundIndexStorage::Factory IndexStorageFactory, Options Opts)
: SwapIndex(std::make_unique<MemIndex>()), TFS(TFS), CDB(CDB),
ContextProvider(std::move(Opts.ContextProvider)),
CollectMainFileRefs(Opts.CollectMainFileRefs),
Rebuilder(this, &IndexedSymbols, Opts.ThreadPoolSize),
IndexStorageFactory(std::move(IndexStorageFactory)),
Queue(std::move(Opts.OnProgress)),
Expand Down Expand Up @@ -301,6 +302,7 @@ llvm::Error BackgroundIndex::index(tooling::CompileCommand Cmd) {
return false; // Skip files that haven't changed, without errors.
return true;
};
IndexOpts.CollectMainFileRefs = CollectMainFileRefs;

IndexFileIn Index;
auto Action = createStaticIndexingAction(
Expand Down
3 changes: 3 additions & 0 deletions clang-tools-extra/clangd/index/Background.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ class BackgroundIndex : public SwapIndex {
// file. Called with the empty string for other tasks.
// (When called, the context from BackgroundIndex construction is active).
std::function<Context(PathRef)> ContextProvider = nullptr;
// Whether to collect references to main-file-only symbols.
bool CollectMainFileRefs = false;
};

/// Creates a new background index and starts its threads.
Expand Down Expand Up @@ -188,6 +190,7 @@ class BackgroundIndex : public SwapIndex {
const ThreadsafeFS &TFS;
const GlobalCompilationDatabase &CDB;
std::function<Context(PathRef)> ContextProvider;
bool CollectMainFileRefs;

llvm::Error index(tooling::CompileCommand);

Expand Down
21 changes: 12 additions & 9 deletions clang-tools-extra/clangd/index/FileIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ SlabTuple indexSymbols(ASTContext &AST, std::shared_ptr<Preprocessor> PP,
llvm::ArrayRef<Decl *> DeclsToIndex,
const MainFileMacros *MacroRefsToIndex,
const CanonicalIncludes &Includes, bool IsIndexMainAST,
llvm::StringRef Version) {
llvm::StringRef Version, bool CollectMainFileRefs) {
SymbolCollector::Options CollectorOpts;
CollectorOpts.CollectIncludePath = true;
CollectorOpts.Includes = &Includes;
CollectorOpts.CountReferences = false;
CollectorOpts.Origin = SymbolOrigin::Dynamic;
CollectorOpts.CollectMainFileRefs = CollectMainFileRefs;

index::IndexingOptions IndexOpts;
// We only need declarations, because we don't count references.
Expand Down Expand Up @@ -205,11 +206,11 @@ FileShardedIndex::getShard(llvm::StringRef Uri) const {
return std::move(IF);
}

SlabTuple indexMainDecls(ParsedAST &AST) {
return indexSymbols(AST.getASTContext(), AST.getPreprocessorPtr(),
AST.getLocalTopLevelDecls(), &AST.getMacros(),
AST.getCanonicalIncludes(),
/*IsIndexMainAST=*/true, AST.version());
SlabTuple indexMainDecls(ParsedAST &AST, bool CollectMainFileRefs) {
return indexSymbols(
AST.getASTContext(), AST.getPreprocessorPtr(),
AST.getLocalTopLevelDecls(), &AST.getMacros(), AST.getCanonicalIncludes(),
/*IsIndexMainAST=*/true, AST.version(), CollectMainFileRefs);
}

SlabTuple indexHeaderSymbols(llvm::StringRef Version, ASTContext &AST,
Expand All @@ -220,7 +221,8 @@ SlabTuple indexHeaderSymbols(llvm::StringRef Version, ASTContext &AST,
AST.getTranslationUnitDecl()->decls().end());
return indexSymbols(AST, std::move(PP), DeclsToIndex,
/*MainFileMacros=*/nullptr, Includes,
/*IsIndexMainAST=*/false, Version);
/*IsIndexMainAST=*/false, Version,
/*CollectMainFileRefs=*/false);
}

void FileSymbols::update(llvm::StringRef Key,
Expand Down Expand Up @@ -371,8 +373,9 @@ FileSymbols::buildIndex(IndexType Type, DuplicateHandling DuplicateHandle,
llvm_unreachable("Unknown clangd::IndexType");
}

FileIndex::FileIndex(bool UseDex)
FileIndex::FileIndex(bool UseDex, bool CollectMainFileRefs)
: MergedIndex(&MainFileIndex, &PreambleIndex), UseDex(UseDex),
CollectMainFileRefs(CollectMainFileRefs),
PreambleIndex(std::make_unique<MemIndex>()),
MainFileIndex(std::make_unique<MemIndex>()) {}

Expand Down Expand Up @@ -415,7 +418,7 @@ void FileIndex::updatePreamble(PathRef Path, llvm::StringRef Version,
}

void FileIndex::updateMain(PathRef Path, ParsedAST &AST) {
auto Contents = indexMainDecls(AST);
auto Contents = indexMainDecls(AST, CollectMainFileRefs);
MainFileSymbols.update(
Path, std::make_unique<SymbolSlab>(std::move(std::get<0>(Contents))),
std::make_unique<RefSlab>(std::move(std::get<1>(Contents))),
Expand Down
5 changes: 3 additions & 2 deletions clang-tools-extra/clangd/index/FileIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class FileSymbols {
/// FIXME: Expose an interface to remove files that are closed.
class FileIndex : public MergedIndex {
public:
FileIndex(bool UseDex = true);
FileIndex(bool UseDex = true, bool CollectMainFileRefs = false);

/// Update preamble symbols of file \p Path with all declarations in \p AST
/// and macros in \p PP.
Expand All @@ -118,6 +118,7 @@ class FileIndex : public MergedIndex {

private:
bool UseDex; // FIXME: this should be always on.
bool CollectMainFileRefs;

// Contains information from each file's preamble only. Symbols and relations
// are sharded per declaration file to deduplicate multiple symbols and reduce
Expand Down Expand Up @@ -152,7 +153,7 @@ using SlabTuple = std::tuple<SymbolSlab, RefSlab, RelationSlab>;
/// Retrieves symbols and refs of local top level decls in \p AST (i.e.
/// `AST.getLocalTopLevelDecls()`).
/// Exposed to assist in unit tests.
SlabTuple indexMainDecls(ParsedAST &AST);
SlabTuple indexMainDecls(ParsedAST &AST, bool CollectMainFileRefs = false);

/// Index declarations from \p AST and macros from \p PP that are declared in
/// included headers.
Expand Down
5 changes: 3 additions & 2 deletions clang-tools-extra/clangd/index/SymbolCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,13 @@ bool SymbolCollector::handleDeclOccurrence(
if (IsOnlyRef && !CollectRef)
return true;

// Do not store references to main-file symbols.
// Unlike other fields, e.g. Symbols (which use spelling locations), we use
// file locations for references (as it aligns the behavior of clangd's
// AST-based xref).
// FIXME: we should try to use the file locations for other fields.
if (CollectRef && (!IsMainFileOnly || ND->isExternallyVisible()) &&
if (CollectRef &&
(!IsMainFileOnly || Opts.CollectMainFileRefs ||
ND->isExternallyVisible()) &&
!isa<NamespaceDecl>(ND) &&
(Opts.RefsInHeaders ||
SM.getFileID(SM.getFileLoc(Loc)) == SM.getMainFileID()))
Expand Down
2 changes: 2 additions & 0 deletions clang-tools-extra/clangd/index/SymbolCollector.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class SymbolCollector : public index::IndexDataConsumer {
/// Collect symbols local to main-files, such as static functions
/// and symbols inside an anonymous namespace.
bool CollectMainFileSymbols = true;
/// Collect references to main-file symbols.
bool CollectMainFileRefs = false;
/// If set to true, SymbolCollector will collect doc for all symbols.
/// Note that documents of symbols being indexed for completion will always
/// be collected regardless of this option.
Expand Down
8 changes: 8 additions & 0 deletions clang-tools-extra/clangd/tool/ClangdMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,13 @@ opt<bool> EnableConfig{
init(true),
};

opt<bool> CollectMainFileRefs{
"collect-main-file-refs",
cat(Misc),
desc("Store references to main-file-only symbols in the index"),
init(false),
};

#if CLANGD_ENABLE_REMOTE
opt<std::string> RemoteIndexAddress{
"remote-index-address",
Expand Down Expand Up @@ -682,6 +689,7 @@ clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment var
if (!ResourceDir.empty())
Opts.ResourceDir = ResourceDir;
Opts.BuildDynamicSymbolIndex = EnableIndex;
Opts.CollectMainFileRefs = CollectMainFileRefs;
std::unique_ptr<SymbolIndex> StaticIdx;
std::future<void> AsyncIndexLoad; // Block exit while loading the index.
if (EnableIndex && !IndexFile.empty()) {
Expand Down
55 changes: 55 additions & 0 deletions clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,61 @@ TEST_F(BackgroundIndexTest, IndexTwoFiles) {
FileURI("unittest:///root/B.cc")}));
}

TEST_F(BackgroundIndexTest, MainFileRefs) {
MockFS FS;
FS.Files[testPath("root/A.h")] = R"cpp(
void header_sym();
)cpp";
FS.Files[testPath("root/A.cc")] =
"#include \"A.h\"\nstatic void main_sym() { (void)header_sym; }";

// Check the behaviour with CollectMainFileRefs = false (the default).
{
llvm::StringMap<std::string> Storage;
size_t CacheHits = 0;
MemoryShardStorage MSS(Storage, CacheHits);
OverlayCDB CDB(/*Base=*/nullptr);
BackgroundIndex Idx(FS, CDB, [&](llvm::StringRef) { return &MSS; },
/*Opts=*/{});

tooling::CompileCommand Cmd;
Cmd.Filename = testPath("root/A.cc");
Cmd.Directory = testPath("root");
Cmd.CommandLine = {"clang++", testPath("root/A.cc")};
CDB.setCompileCommand(testPath("root/A.cc"), Cmd);

ASSERT_TRUE(Idx.blockUntilIdleForTest());
EXPECT_THAT(
runFuzzyFind(Idx, ""),
UnorderedElementsAre(AllOf(Named("header_sym"), NumReferences(1U)),
AllOf(Named("main_sym"), NumReferences(0U))));
}

// Check the behaviour with CollectMainFileRefs = true.
{
llvm::StringMap<std::string> Storage;
size_t CacheHits = 0;
MemoryShardStorage MSS(Storage, CacheHits);
OverlayCDB CDB(/*Base=*/nullptr);
BackgroundIndex::Options Opts;
Opts.CollectMainFileRefs = true;
BackgroundIndex Idx(
FS, CDB, [&](llvm::StringRef) { return &MSS; }, Opts);

tooling::CompileCommand Cmd;
Cmd.Filename = testPath("root/A.cc");
Cmd.Directory = testPath("root");
Cmd.CommandLine = {"clang++", testPath("root/A.cc")};
CDB.setCompileCommand(testPath("root/A.cc"), Cmd);

ASSERT_TRUE(Idx.blockUntilIdleForTest());
EXPECT_THAT(
runFuzzyFind(Idx, ""),
UnorderedElementsAre(AllOf(Named("header_sym"), NumReferences(1U)),
AllOf(Named("main_sym"), NumReferences(1U))));
}
}

TEST_F(BackgroundIndexTest, ShardStorageTest) {
MockFS FS;
FS.Files[testPath("root/A.h")] = R"cpp(
Expand Down
Loading

0 comments on commit 58d7da9

Please sign in to comment.