@@ -826,22 +826,8 @@ void SymbolCollector::setIncludeLocation(const Symbol &S, SourceLocation DefLoc,
826
826
// We update providers for a symbol with each occurence, as SymbolCollector
827
827
// might run while parsing, rather than at the end of a translation unit.
828
828
// Hence we see more and more redecls over time.
829
- auto [It, Inserted] = SymbolProviders.try_emplace (S.ID );
830
- auto Headers =
829
+ SymbolProviders[S.ID ] =
831
830
include_cleaner::headersForSymbol (Sym, SM, Opts.PragmaIncludes );
832
- if (Headers.empty ())
833
- return ;
834
-
835
- auto *HeadersIter = Headers.begin ();
836
- include_cleaner::Header H = *HeadersIter;
837
- while (HeadersIter != Headers.end () &&
838
- H.kind () == include_cleaner::Header::Physical &&
839
- !tooling::isSelfContainedHeader (H.physical (), SM,
840
- PP->getHeaderSearchInfo ())) {
841
- H = *HeadersIter;
842
- HeadersIter++;
843
- }
844
- It->second = H;
845
831
}
846
832
847
833
llvm::StringRef getStdHeader (const Symbol *S, const LangOptions &LangOpts) {
@@ -889,7 +875,7 @@ void SymbolCollector::finish() {
889
875
llvm::DenseMap<include_cleaner::Header, std::string> HeaderSpelling;
890
876
// Fill in IncludeHeaders.
891
877
// We delay this until end of TU so header guards are all resolved.
892
- for (const auto &[SID, OptionalProvider ] : SymbolProviders) {
878
+ for (const auto &[SID, Providers ] : SymbolProviders) {
893
879
const Symbol *S = Symbols.find (SID);
894
880
if (!S)
895
881
continue ;
@@ -931,9 +917,27 @@ void SymbolCollector::finish() {
931
917
continue ;
932
918
}
933
919
934
- assert (Directives == Symbol::Include);
935
920
// For #include's, use the providers computed by the include-cleaner
936
921
// library.
922
+ assert (Directives == Symbol::Include);
923
+ // Ignore providers that are not self-contained, this is especially
924
+ // important for symbols defined in the main-file. We want to prefer the
925
+ // header, if possible.
926
+ // TODO: Limit this to specifically ignore main file, when we're indexing a
927
+ // non-header file?
928
+ auto SelfContainedProvider =
929
+ [this ](llvm::ArrayRef<include_cleaner::Header> Providers)
930
+ -> std::optional<include_cleaner::Header> {
931
+ for (const auto &H : Providers) {
932
+ if (H.kind () != include_cleaner::Header::Physical)
933
+ return H;
934
+ if (tooling::isSelfContainedHeader (H.physical (), PP->getSourceManager (),
935
+ PP->getHeaderSearchInfo ()))
936
+ return H;
937
+ }
938
+ return std::nullopt;
939
+ };
940
+ const auto OptionalProvider = SelfContainedProvider (Providers);
937
941
if (!OptionalProvider)
938
942
continue ;
939
943
const auto &H = *OptionalProvider;
0 commit comments