36
36
#include " clang/Lex/Preprocessor.h"
37
37
#include " clang/Lex/Token.h"
38
38
#include " clang/Tooling/Inclusions/HeaderAnalysis.h"
39
+ #include " clang/Tooling/Inclusions/StandardLibrary.h"
39
40
#include " llvm/ADT/ArrayRef.h"
40
41
#include " llvm/ADT/DenseMap.h"
41
42
#include " llvm/ADT/SmallVector.h"
@@ -836,14 +837,25 @@ void SymbolCollector::setIncludeLocation(const Symbol &S, SourceLocation DefLoc,
836
837
// Use the expansion location to get the #include header since this is
837
838
// where the symbol is exposed.
838
839
IncludeFiles[S.ID ] = SM.getDecomposedExpansionLoc (DefLoc).first ;
840
+ }
839
841
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 " " ;
847
859
}
848
860
849
861
void SymbolCollector::finish () {
@@ -869,16 +881,13 @@ void SymbolCollector::finish() {
869
881
}
870
882
}
871
883
llvm::DenseMap<FileID, bool > FileToContainsImportsOrObjC;
872
- llvm::DenseMap<include_cleaner::Header, std::string> HeaderSpelling;
873
884
// Fill in IncludeHeaders.
874
885
// 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 ) {
876
887
const Symbol *S = Symbols.find (SID);
877
888
if (!S)
878
889
continue ;
879
- assert (IncludeFiles.find (SID) != IncludeFiles.end ());
880
890
881
- const auto FID = IncludeFiles.at (SID);
882
891
// Determine if the FID is #include'd or #import'ed.
883
892
Symbol::IncludeDirective Directives = Symbol::Invalid;
884
893
auto CollectDirectives = shouldCollectIncludePath (S->SymInfo .Kind );
@@ -898,54 +907,20 @@ void SymbolCollector::finish() {
898
907
if (Directives == Symbol::Invalid)
899
908
continue ;
900
909
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);
938
914
939
- if (!SpellingIt-> second .empty ()) {
915
+ if (!IncludeHeader .empty ()) {
940
916
auto NewSym = *S;
941
- NewSym.IncludeHeaders .push_back ({SpellingIt-> second , 1 , Directives});
917
+ NewSym.IncludeHeaders .push_back ({IncludeHeader , 1 , Directives});
942
918
Symbols.insert (NewSym);
943
919
}
944
920
}
945
921
946
922
ReferencedSymbols.clear ();
947
923
IncludeFiles.clear ();
948
- SymbolProviders.clear ();
949
924
FilesWithObjCConstructs.clear ();
950
925
}
951
926
0 commit comments