@@ -36,9 +36,11 @@ namespace {
36
36
struct DirectoryLookupInfo {
37
37
IncludeDirGroup Group;
38
38
DirectoryLookup Lookup;
39
+ Optional<unsigned > UserEntryIdx;
39
40
40
- DirectoryLookupInfo (IncludeDirGroup Group, DirectoryLookup Lookup)
41
- : Group(Group), Lookup(Lookup) {}
41
+ DirectoryLookupInfo (IncludeDirGroup Group, DirectoryLookup Lookup,
42
+ Optional<unsigned > UserEntryIdx)
43
+ : Group(Group), Lookup(Lookup), UserEntryIdx(UserEntryIdx) {}
42
44
};
43
45
44
46
// / InitHeaderSearch - This class makes it easier to set the search paths of
@@ -60,13 +62,15 @@ class InitHeaderSearch {
60
62
// / AddPath - Add the specified path to the specified group list, prefixing
61
63
// / the sysroot if used.
62
64
// / Returns true if the path exists, false if it was ignored.
63
- bool AddPath (const Twine &Path, IncludeDirGroup Group, bool isFramework);
65
+ bool AddPath (const Twine &Path, IncludeDirGroup Group, bool isFramework,
66
+ Optional<unsigned > UserEntryIdx = None);
64
67
65
68
// / AddUnmappedPath - Add the specified path to the specified group list,
66
69
// / without performing any sysroot remapping.
67
70
// / Returns true if the path exists, false if it was ignored.
68
71
bool AddUnmappedPath (const Twine &Path, IncludeDirGroup Group,
69
- bool isFramework);
72
+ bool isFramework,
73
+ Optional<unsigned > UserEntryIdx = None);
70
74
71
75
// / AddSystemHeaderPrefix - Add the specified prefix to the system header
72
76
// / prefix list.
@@ -119,22 +123,25 @@ static bool CanPrefixSysroot(StringRef Path) {
119
123
}
120
124
121
125
bool InitHeaderSearch::AddPath (const Twine &Path, IncludeDirGroup Group,
122
- bool isFramework) {
126
+ bool isFramework,
127
+ Optional<unsigned > UserEntryIdx) {
123
128
// Add the path with sysroot prepended, if desired and this is a system header
124
129
// group.
125
130
if (HasSysroot) {
126
131
SmallString<256 > MappedPathStorage;
127
132
StringRef MappedPathStr = Path.toStringRef (MappedPathStorage);
128
133
if (CanPrefixSysroot (MappedPathStr)) {
129
- return AddUnmappedPath (IncludeSysroot + Path, Group, isFramework);
134
+ return AddUnmappedPath (IncludeSysroot + Path, Group, isFramework,
135
+ UserEntryIdx);
130
136
}
131
137
}
132
138
133
- return AddUnmappedPath (Path, Group, isFramework);
139
+ return AddUnmappedPath (Path, Group, isFramework, UserEntryIdx );
134
140
}
135
141
136
142
bool InitHeaderSearch::AddUnmappedPath (const Twine &Path, IncludeDirGroup Group,
137
- bool isFramework) {
143
+ bool isFramework,
144
+ Optional<unsigned > UserEntryIdx) {
138
145
assert (!Path.isTriviallyEmpty () && " can't handle empty path here" );
139
146
140
147
FileManager &FM = Headers.getFileMgr ();
@@ -160,7 +167,8 @@ bool InitHeaderSearch::AddUnmappedPath(const Twine &Path, IncludeDirGroup Group,
160
167
161
168
// If the directory exists, add it.
162
169
if (auto DE = FM.getOptionalDirectoryRef (MappedPathStr)) {
163
- IncludePath.emplace_back (Group, DirectoryLookup (*DE, Type, isFramework));
170
+ IncludePath.emplace_back (Group, DirectoryLookup (*DE, Type, isFramework),
171
+ UserEntryIdx);
164
172
return true ;
165
173
}
166
174
@@ -171,7 +179,8 @@ bool InitHeaderSearch::AddUnmappedPath(const Twine &Path, IncludeDirGroup Group,
171
179
if (const HeaderMap *HM = Headers.CreateHeaderMap (*FE)) {
172
180
// It is a headermap, add it to the search path.
173
181
IncludePath.emplace_back (
174
- Group, DirectoryLookup (HM, Type, Group == IndexHeaderMap));
182
+ Group, DirectoryLookup (HM, Type, Group == IndexHeaderMap),
183
+ UserEntryIdx);
175
184
return true ;
176
185
}
177
186
}
@@ -471,7 +480,7 @@ void InitHeaderSearch::AddDefaultIncludePaths(const LangOptions &Lang,
471
480
// / RemoveDuplicates - If there are duplicate directory entries in the specified
472
481
// / search list, remove the later (dead) ones. Returns the number of non-system
473
482
// / headers removed, which is used to update NumAngled.
474
- static unsigned RemoveDuplicates (std::vector<DirectoryLookup > &SearchList,
483
+ static unsigned RemoveDuplicates (std::vector<DirectoryLookupInfo > &SearchList,
475
484
unsigned First, bool Verbose) {
476
485
llvm::SmallPtrSet<const DirectoryEntry *, 8 > SeenDirs;
477
486
llvm::SmallPtrSet<const DirectoryEntry *, 8 > SeenFrameworkDirs;
@@ -480,7 +489,7 @@ static unsigned RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
480
489
for (unsigned i = First; i != SearchList.size (); ++i) {
481
490
unsigned DirToRemove = i;
482
491
483
- const DirectoryLookup &CurEntry = SearchList[i];
492
+ const DirectoryLookup &CurEntry = SearchList[i]. Lookup ;
484
493
485
494
if (CurEntry.isNormalDir ()) {
486
495
// If this isn't the first time we've seen this dir, remove it.
@@ -510,7 +519,7 @@ static unsigned RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
510
519
for (FirstDir = First;; ++FirstDir) {
511
520
assert (FirstDir != i && " Didn't find dupe?" );
512
521
513
- const DirectoryLookup &SearchEntry = SearchList[FirstDir];
522
+ const DirectoryLookup &SearchEntry = SearchList[FirstDir]. Lookup ;
514
523
515
524
// If these are different lookup types, then they can't be the dupe.
516
525
if (SearchEntry.getLookupType () != CurEntry.getLookupType ())
@@ -532,7 +541,7 @@ static unsigned RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
532
541
533
542
// If the first dir in the search path is a non-system dir, zap it
534
543
// instead of the system one.
535
- if (SearchList[FirstDir].getDirCharacteristic () == SrcMgr::C_User)
544
+ if (SearchList[FirstDir].Lookup . getDirCharacteristic () == SrcMgr::C_User)
536
545
DirToRemove = FirstDir;
537
546
}
538
547
@@ -554,24 +563,45 @@ static unsigned RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
554
563
return NonSystemRemoved;
555
564
}
556
565
566
+ // / Extract DirectoryLookups from DirectoryLookupInfos.
567
+ static std::vector<DirectoryLookup>
568
+ extractLookups (const std::vector<DirectoryLookupInfo> &Infos) {
569
+ std::vector<DirectoryLookup> Lookups;
570
+ Lookups.reserve (Infos.size ());
571
+ llvm::transform (Infos, std::back_inserter (Lookups),
572
+ [](const DirectoryLookupInfo &Info) { return Info.Lookup ; });
573
+ return Lookups;
574
+ }
575
+
576
+ // / Collect the mapping between indices of DirectoryLookups and UserEntries.
577
+ static llvm::DenseMap<unsigned , unsigned >
578
+ mapToUserEntries (const std::vector<DirectoryLookupInfo> &Infos) {
579
+ llvm::DenseMap<unsigned , unsigned > LookupsToUserEntries;
580
+ for (unsigned I = 0 , E = Infos.size (); I < E; ++I) {
581
+ // Check whether this DirectoryLookup maps to a HeaderSearch::UserEntry.
582
+ if (Infos[I].UserEntryIdx )
583
+ LookupsToUserEntries.insert ({I, *Infos[I].UserEntryIdx });
584
+ }
585
+ return LookupsToUserEntries;
586
+ }
557
587
558
588
void InitHeaderSearch::Realize (const LangOptions &Lang) {
559
589
// Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
560
- std::vector<DirectoryLookup > SearchList;
590
+ std::vector<DirectoryLookupInfo > SearchList;
561
591
SearchList.reserve (IncludePath.size ());
562
592
563
593
// Quoted arguments go first.
564
594
for (auto &Include : IncludePath)
565
595
if (Include.Group == Quoted)
566
- SearchList.push_back (Include. Lookup );
596
+ SearchList.push_back (Include);
567
597
568
598
// Deduplicate and remember index.
569
599
RemoveDuplicates (SearchList, 0 , Verbose);
570
600
unsigned NumQuoted = SearchList.size ();
571
601
572
602
for (auto &Include : IncludePath)
573
603
if (Include.Group == Angled || Include.Group == IndexHeaderMap)
574
- SearchList.push_back (Include. Lookup );
604
+ SearchList.push_back (Include);
575
605
576
606
RemoveDuplicates (SearchList, NumQuoted, Verbose);
577
607
unsigned NumAngled = SearchList.size ();
@@ -583,11 +613,11 @@ void InitHeaderSearch::Realize(const LangOptions &Lang) {
583
613
Include.Group == CXXSystem) ||
584
614
(Lang.ObjC && !Lang.CPlusPlus && Include.Group == ObjCSystem) ||
585
615
(Lang.ObjC && Lang.CPlusPlus && Include.Group == ObjCXXSystem))
586
- SearchList.push_back (Include. Lookup );
616
+ SearchList.push_back (Include);
587
617
588
618
for (auto &Include : IncludePath)
589
619
if (Include.Group == After)
590
- SearchList.push_back (Include. Lookup );
620
+ SearchList.push_back (Include);
591
621
592
622
// Remove duplicates across both the Angled and System directories. GCC does
593
623
// this and failing to remove duplicates across these two groups breaks
@@ -596,7 +626,8 @@ void InitHeaderSearch::Realize(const LangOptions &Lang) {
596
626
NumAngled -= NonSystemRemoved;
597
627
598
628
bool DontSearchCurDir = false ; // TODO: set to true if -I- is set?
599
- Headers.SetSearchPaths (SearchList, NumQuoted, NumAngled, DontSearchCurDir);
629
+ Headers.SetSearchPaths (extractLookups (SearchList), NumQuoted, NumAngled,
630
+ DontSearchCurDir, mapToUserEntries (SearchList));
600
631
601
632
Headers.SetSystemHeaderPrefixes (SystemHeaderPrefixes);
602
633
@@ -606,14 +637,14 @@ void InitHeaderSearch::Realize(const LangOptions &Lang) {
606
637
for (unsigned i = 0 , e = SearchList.size (); i != e; ++i) {
607
638
if (i == NumQuoted)
608
639
llvm::errs () << " #include <...> search starts here:\n " ;
609
- StringRef Name = SearchList[i].getName ();
640
+ StringRef Name = SearchList[i].Lookup . getName ();
610
641
const char *Suffix;
611
- if (SearchList[i].isNormalDir ())
642
+ if (SearchList[i].Lookup . isNormalDir ())
612
643
Suffix = " " ;
613
- else if (SearchList[i].isFramework ())
644
+ else if (SearchList[i].Lookup . isFramework ())
614
645
Suffix = " (framework directory)" ;
615
646
else {
616
- assert (SearchList[i].isHeaderMap () && " Unknown DirectoryLookup" );
647
+ assert (SearchList[i].Lookup . isHeaderMap () && " Unknown DirectoryLookup" );
617
648
Suffix = " (headermap)" ;
618
649
}
619
650
llvm::errs () << " " << Name << Suffix << " \n " ;
@@ -632,9 +663,9 @@ void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
632
663
for (unsigned i = 0 , e = HSOpts.UserEntries .size (); i != e; ++i) {
633
664
const HeaderSearchOptions::Entry &E = HSOpts.UserEntries [i];
634
665
if (E.IgnoreSysRoot ) {
635
- Init.AddUnmappedPath (E.Path , E.Group , E.IsFramework );
666
+ Init.AddUnmappedPath (E.Path , E.Group , E.IsFramework , i );
636
667
} else {
637
- Init.AddPath (E.Path , E.Group , E.IsFramework );
668
+ Init.AddPath (E.Path , E.Group , E.IsFramework , i );
638
669
}
639
670
}
640
671
0 commit comments