[clang-tools-extra] r333519 - [clangd] Avoid inserting new #include when declaration is present in the main file.
Eric Liu via cfe-commits
cfe-commits at lists.llvm.org
Wed May 30 02:03:39 PDT 2018
Author: ioeric
Date: Wed May 30 02:03:39 2018
New Revision: 333519
URL: http://llvm.org/viewvc/llvm-project?rev=333519&view=rev
Log:
[clangd] Avoid inserting new #include when declaration is present in the main file.
Summary:
Also fix USR generation for classes in unit tests. The previous USR
only works for class members, which happens to work when completing class name
inside the class, where constructors are suggested by sema.
Reviewers: sammccall, ilya-biryukov
Subscribers: klimek, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47466
Modified:
clang-tools-extra/trunk/clangd/CodeComplete.cpp
clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp
Modified: clang-tools-extra/trunk/clangd/CodeComplete.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/CodeComplete.cpp?rev=333519&r1=333518&r2=333519&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/CodeComplete.cpp (original)
+++ clang-tools-extra/trunk/clangd/CodeComplete.cpp Wed May 30 02:03:39 2018
@@ -235,6 +235,7 @@ struct CompletionCandidate {
llvm::StringRef SemaDocComment) const {
assert(bool(SemaResult) == bool(SemaCCS));
CompletionItem I;
+ bool ShouldInsertInclude = true;
if (SemaResult) {
I.kind = toCompletionItemKind(SemaResult->Kind, SemaResult->CursorKind);
getLabelAndInsertText(*SemaCCS, &I.label, &I.insertText,
@@ -242,6 +243,20 @@ struct CompletionCandidate {
I.filterText = getFilterText(*SemaCCS);
I.documentation = formatDocumentation(*SemaCCS, SemaDocComment);
I.detail = getDetail(*SemaCCS);
+ // Avoid inserting new #include if the declaration is found in the current
+ // file e.g. the symbol is forward declared.
+ if (SemaResult->Kind == CodeCompletionResult::RK_Declaration) {
+ if (const auto *D = SemaResult->getDeclaration()) {
+ const auto &SM = D->getASTContext().getSourceManager();
+ ShouldInsertInclude =
+ ShouldInsertInclude &&
+ std::none_of(D->redecls_begin(), D->redecls_end(),
+ [&SM](const Decl *RD) {
+ return SM.isInMainFile(
+ SM.getExpansionLoc(RD->getLocStart()));
+ });
+ }
+ }
}
if (IndexResult) {
if (I.kind == CompletionItemKind::Missing)
@@ -263,7 +278,7 @@ struct CompletionCandidate {
I.documentation = D->Documentation;
if (I.detail.empty())
I.detail = D->CompletionDetail;
- if (Includes && !D->IncludeHeader.empty()) {
+ if (ShouldInsertInclude && Includes && !D->IncludeHeader.empty()) {
auto Edit = [&]() -> Expected<Optional<TextEdit>> {
auto ResolvedDeclaring = toHeaderFile(
IndexResult->CanonicalDeclaration.FileURI, FileName);
Modified: clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp?rev=333519&r1=333518&r2=333519&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp (original)
+++ clang-tools-extra/trunk/unittests/clangd/CodeCompleteTests.cpp Wed May 30 02:03:39 2018
@@ -159,7 +159,7 @@ Symbol func(StringRef Name) { // Assumes
return sym(Name, index::SymbolKind::Function, "@F@\\0#"); // no args
}
Symbol cls(StringRef Name) {
- return sym(Name, index::SymbolKind::Class, "@S@\\0 at S@\\0");
+ return sym(Name, index::SymbolKind::Class, "@S@\\0");
}
Symbol var(StringRef Name) {
return sym(Name, index::SymbolKind::Variable, "@\\0");
@@ -425,10 +425,9 @@ TEST(CompletionTest, NoDuplicates) {
auto Results = completions(
R"cpp(
class Adapter {
- void method();
};
- void Adapter::method() {
+ void f() {
Adapter^
}
)cpp",
@@ -559,6 +558,37 @@ TEST(CompletionTest, IncludeInsertionPre
ElementsAre(AllOf(Named("X"), Not(HasAdditionalEdits()))));
}
+TEST(CompletionTest, NoIncludeInsertionWhenDeclFoundInFile) {
+ MockFSProvider FS;
+ MockCompilationDatabase CDB;
+
+ IgnoreDiagnostics DiagConsumer;
+ ClangdServer Server(CDB, FS, DiagConsumer, ClangdServer::optsForTest());
+ Symbol::Details Scratch;
+ Symbol SymX = cls("ns::X");
+ Symbol SymY = cls("ns::Y");
+ std::string BarHeader = testPath("bar.h");
+ auto BarURI = URI::createFile(BarHeader).toString();
+ SymX.CanonicalDeclaration.FileURI = BarURI;
+ SymY.CanonicalDeclaration.FileURI = BarURI;
+ Scratch.IncludeHeader = "<bar>";
+ SymX.Detail = &Scratch;
+ SymY.Detail = &Scratch;
+ // Shoten include path based on search dirctory and insert.
+ auto Results = completions(Server,
+ R"cpp(
+ namespace ns {
+ class X;
+ class Y {}
+ }
+ int main() { ns::^ }
+ )cpp",
+ {SymX, SymY});
+ EXPECT_THAT(Results.items,
+ ElementsAre(AllOf(Named("X"), Not(HasAdditionalEdits())),
+ AllOf(Named("Y"), Not(HasAdditionalEdits()))));
+}
+
TEST(CompletionTest, IndexSuppressesPreambleCompletions) {
MockFSProvider FS;
MockCompilationDatabase CDB;
More information about the cfe-commits
mailing list