Skip to content

Commit d4a6c50

Browse files
VitaNuotru
authored andcommitted
[clangd] Revert the symbol collector behavior to old pre-include-cleaner-library behavior due to a regression.
Differential Revision: https://reviews.llvm.org/D156403 (cherry picked from commit 3c6a7b0)
1 parent 1de314f commit d4a6c50

File tree

4 files changed

+28
-58
lines changed

4 files changed

+28
-58
lines changed

clang-tools-extra/clangd/index/SymbolCollector.cpp

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "clang/Lex/Preprocessor.h"
3737
#include "clang/Lex/Token.h"
3838
#include "clang/Tooling/Inclusions/HeaderAnalysis.h"
39+
#include "clang/Tooling/Inclusions/StandardLibrary.h"
3940
#include "llvm/ADT/ArrayRef.h"
4041
#include "llvm/ADT/DenseMap.h"
4142
#include "llvm/ADT/SmallVector.h"
@@ -836,14 +837,25 @@ void SymbolCollector::setIncludeLocation(const Symbol &S, SourceLocation DefLoc,
836837
// Use the expansion location to get the #include header since this is
837838
// where the symbol is exposed.
838839
IncludeFiles[S.ID] = SM.getDecomposedExpansionLoc(DefLoc).first;
840+
}
839841

840-
auto [It, Inserted] = SymbolProviders.try_emplace(S.ID);
841-
if (Inserted) {
842-
auto Headers =
843-
include_cleaner::headersForSymbol(Sym, SM, Opts.PragmaIncludes);
844-
if (!Headers.empty())
845-
It->second = Headers.front();
846-
}
842+
llvm::StringRef getStdHeader(const Symbol *S, const LangOptions &LangOpts) {
843+
tooling::stdlib::Lang Lang = tooling::stdlib::Lang::CXX;
844+
if (LangOpts.C11)
845+
Lang = tooling::stdlib::Lang::C;
846+
else if(!LangOpts.CPlusPlus)
847+
return "";
848+
849+
if (S->Scope == "std::" && S->Name == "move") {
850+
if (!S->Signature.contains(','))
851+
return "<utility>";
852+
return "<algorithm>";
853+
}
854+
855+
if (auto StdSym = tooling::stdlib::Symbol::named(S->Scope, S->Name, Lang))
856+
if (auto Header = StdSym->header())
857+
return Header->name();
858+
return "";
847859
}
848860

849861
void SymbolCollector::finish() {
@@ -869,16 +881,13 @@ void SymbolCollector::finish() {
869881
}
870882
}
871883
llvm::DenseMap<FileID, bool> FileToContainsImportsOrObjC;
872-
llvm::DenseMap<include_cleaner::Header, std::string> HeaderSpelling;
873884
// Fill in IncludeHeaders.
874885
// We delay this until end of TU so header guards are all resolved.
875-
for (const auto &[SID, OptionalProvider] : SymbolProviders) {
886+
for (const auto &[SID, FID] : IncludeFiles) {
876887
const Symbol *S = Symbols.find(SID);
877888
if (!S)
878889
continue;
879-
assert(IncludeFiles.find(SID) != IncludeFiles.end());
880890

881-
const auto FID = IncludeFiles.at(SID);
882891
// Determine if the FID is #include'd or #import'ed.
883892
Symbol::IncludeDirective Directives = Symbol::Invalid;
884893
auto CollectDirectives = shouldCollectIncludePath(S->SymInfo.Kind);
@@ -898,54 +907,20 @@ void SymbolCollector::finish() {
898907
if (Directives == Symbol::Invalid)
899908
continue;
900909

901-
// Use the include location-based logic for Objective-C symbols.
902-
if (Directives & Symbol::Import) {
903-
if (auto IncludeHeader = HeaderFileURIs->getIncludeHeader(FID);
904-
!IncludeHeader.empty()) {
905-
auto NewSym = *S;
906-
NewSym.IncludeHeaders.push_back({IncludeHeader, 1, Directives});
907-
Symbols.insert(NewSym);
908-
}
909-
// FIXME: use providers from include-cleaner library once it's polished
910-
// for Objective-C.
911-
continue;
912-
}
913-
914-
assert(Directives == Symbol::Include);
915-
// For #include's, use the providers computed by the include-cleaner
916-
// library.
917-
if (!OptionalProvider)
918-
continue;
919-
const auto &H = *OptionalProvider;
920-
const auto [SpellingIt, Inserted] = HeaderSpelling.try_emplace(H);
921-
if (Inserted) {
922-
auto &SM = ASTCtx->getSourceManager();
923-
if (H.kind() == include_cleaner::Header::Kind::Physical) {
924-
if (auto Canonical =
925-
HeaderFileURIs->mapCanonical(H.physical()->getName());
926-
!Canonical.empty())
927-
SpellingIt->second = Canonical;
928-
else if (tooling::isSelfContainedHeader(H.physical(), SM,
929-
PP->getHeaderSearchInfo()))
930-
SpellingIt->second =
931-
HeaderFileURIs->toURI(H.physical()->getLastRef());
932-
} else {
933-
SpellingIt->second = include_cleaner::spellHeader(
934-
{H, PP->getHeaderSearchInfo(),
935-
SM.getFileEntryForID(SM.getMainFileID())});
936-
}
937-
}
910+
// FIXME: Use the include-cleaner library instead.
911+
llvm::StringRef IncludeHeader = getStdHeader(S, ASTCtx->getLangOpts());
912+
if (IncludeHeader.empty())
913+
IncludeHeader = HeaderFileURIs->getIncludeHeader(FID);
938914

939-
if (!SpellingIt->second.empty()) {
915+
if (!IncludeHeader.empty()) {
940916
auto NewSym = *S;
941-
NewSym.IncludeHeaders.push_back({SpellingIt->second, 1, Directives});
917+
NewSym.IncludeHeaders.push_back({IncludeHeader, 1, Directives});
942918
Symbols.insert(NewSym);
943919
}
944920
}
945921

946922
ReferencedSymbols.clear();
947923
IncludeFiles.clear();
948-
SymbolProviders.clear();
949924
FilesWithObjCConstructs.clear();
950925
}
951926

clang-tools-extra/clangd/index/SymbolCollector.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,6 @@ class SymbolCollector : public index::IndexDataConsumer {
175175
void setIncludeLocation(const Symbol &S, SourceLocation,
176176
const include_cleaner::Symbol &Sym);
177177

178-
// Providers for Symbol.IncludeHeaders.
179-
// The final spelling is calculated in finish().
180-
llvm::DenseMap<SymbolID, std::optional<include_cleaner::Header>>
181-
SymbolProviders;
182-
183178
// Files which contain ObjC symbols.
184179
// This is finalized and used in finish().
185180
llvm::DenseSet<FileID> FilesWithObjCConstructs;

clang-tools-extra/clangd/unittests/FileIndexTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ TEST(FileIndexTest, IncludeCollected) {
239239
"<the/good/header.h>");
240240
}
241241

242-
TEST(FileIndexTest, IWYUPragmaExport) {
242+
TEST(FileIndexTest, DISABLED_IWYUPragmaExport) {
243243
FileIndex M;
244244

245245
TestTU File;

clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,7 @@ TEST_F(SymbolCollectorTest, IWYUPragmaWithDoubleQuotes) {
15921592
includeHeader("\"the/good/header.h\""))));
15931593
}
15941594

1595-
TEST_F(SymbolCollectorTest, IWYUPragmaExport) {
1595+
TEST_F(SymbolCollectorTest, DISABLED_IWYUPragmaExport) {
15961596
CollectorOpts.CollectIncludePath = true;
15971597
const std::string Header = R"cpp(#pragma once
15981598
#include "exporter.h"

0 commit comments

Comments
 (0)