================
@@ -881,6 +886,35 @@ diagnoseFrameworkInclude(DiagnosticsEngine &Diags, 
SourceLocation IncludeLoc,
         << IncludeFilename;
 }
 
+/// Return true if a shadow has been detected and the caller should
+/// stop and return the first-found file and module, false otherwise.
+static bool checkAndStoreCandidate(
+    ModuleMap::KnownHeader *SuggestedModule, OptionalFileEntryRef 
CandidateFile,
+    StringRef CandidateDir, DiagnosticsEngine &Diags, StringRef Filename,
+    SourceLocation IncludeLoc, ModuleMap::KnownHeader &FirstModule,
+    OptionalFileEntryRef &FirstHeader, SmallString<1024> &FirstDir) {
+  if (!FirstHeader) {
+    // Found the first candidate
+    FirstHeader = CandidateFile;
+    FirstDir = CandidateDir;
+    if (SuggestedModule)
+      FirstModule = *SuggestedModule;
+    return false;
+  }
+
+  if (FirstDir != CandidateDir) {
+    // Found a second candidate from a different directory
+    Diags.Report(IncludeLoc, diag::warn_header_shadowed)
+        << Filename << FirstDir << CandidateDir;
+    if (SuggestedModule)
+      *SuggestedModule = FirstModule;
+    return true;
+  }
+
+  // Found a candidate from the same directory as the first one
+  return false;
----------------
Jinjie-Huang wrote:

Thanks for review. Based on the code implementation and my testing, it appears 
that although the search paths themselves are deduplicated, the current 
baseline implementation executes two search strategies simultaneously:

​1. MSVC rule (not corrected) for "quoted search":​​ This strategy searches 
based on the include stack, checking each directory level in the stack. For 
example, in ./x.cpp -> a/a.h -> a/b.h, when searching for b.h, it first looks 
in the paths "a" and ".". These paths are represented in the code by 
"Includers". 
[code](https://github.com/llvm/llvm-project/blob/main/clang/lib/Lex/HeaderSearch.cpp#L939-L1016)
2. Regular gcc/clang rule:​​ This constructs search paths by combining the 
provided -I lists and system paths. 
[code](https://github.com/llvm/llvm-project/blob/main/clang/lib/Lex/HeaderSearch.cpp#L1074-L1139)

Therefore, when paths from strategy 1 and strategy 2 happen to be identical, 
this case can occur.

The current implementation of this diagnostic does not return immediately upon 
the first finding. Its purpose is to check if a header file with the same name 
exists anywhere within the combined set of paths from strategies 1 and 2. This 
check avoids false positives that could arise if, for instance, a/b.his found 
in strategy 1 (Includers) and then another b.h is found in an -I a from 
strategy 2.

https://github.com/llvm/llvm-project/pull/162491
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to