Author: Arseniy Zaostrovnykh
Date: 2026-03-27T14:16:42+01:00
New Revision: 94c0d37c59541278145db54adb1bf17da94fd958

URL: 
https://github.com/llvm/llvm-project/commit/94c0d37c59541278145db54adb1bf17da94fd958
DIFF: 
https://github.com/llvm/llvm-project/commit/94c0d37c59541278145db54adb1bf17da94fd958.diff

LOG: [clang][analyzer] Add location to CTU failure diagnostics

Report CTU import failures at the place where the imported symbol would
be used. This helps to quicker understand why CSA might miss a certain
expected CTU bug.

--
CPP-7804

Added: 
    

Modified: 
    clang/include/clang/CrossTU/CrossTranslationUnit.h
    clang/lib/CrossTU/CrossTranslationUnit.cpp
    clang/lib/StaticAnalyzer/Core/CallEvent.cpp
    clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
    clang/test/Analysis/ctu/diag/invalid-index.cpp
    clang/test/Analysis/ctu/diag/missing-index.cpp
    clang/test/Analysis/ctu/different-triples.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/CrossTU/CrossTranslationUnit.h 
b/clang/include/clang/CrossTU/CrossTranslationUnit.h
index 9e0721edfc323..145bc8df27de6 100644
--- a/clang/include/clang/CrossTU/CrossTranslationUnit.h
+++ b/clang/include/clang/CrossTU/CrossTranslationUnit.h
@@ -184,7 +184,7 @@ class CrossTranslationUnitContext {
   static std::optional<std::string> getLookupName(const Decl *D);
 
   /// Emit diagnostics for the user for potential configuration errors.
-  void emitCrossTUDiagnostics(const IndexError &IE);
+  void emitCrossTUDiagnostics(const IndexError &IE, SourceLocation Loc);
 
   /// Returns the MacroExpansionContext for the imported TU to which the given
   /// source-location corresponds.

diff  --git a/clang/lib/CrossTU/CrossTranslationUnit.cpp 
b/clang/lib/CrossTU/CrossTranslationUnit.cpp
index ef25ad454da2e..8dd0ef13123d1 100644
--- a/clang/lib/CrossTU/CrossTranslationUnit.cpp
+++ b/clang/lib/CrossTU/CrossTranslationUnit.cpp
@@ -379,22 +379,23 @@ CrossTranslationUnitContext::getCrossTUDefinition(const 
VarDecl *VD,
                                   DisplayCTUProgress);
 }
 
-void CrossTranslationUnitContext::emitCrossTUDiagnostics(const IndexError &IE) 
{
+void CrossTranslationUnitContext::emitCrossTUDiagnostics(const IndexError &IE,
+                                                         SourceLocation Loc) {
   switch (IE.getCode()) {
   case index_error_code::missing_index_file:
-    Context.getDiagnostics().Report(diag::err_ctu_error_opening)
+    Context.getDiagnostics().Report(Loc, diag::err_ctu_error_opening)
         << IE.getFileName();
     return;
   case index_error_code::invalid_index_format:
-    Context.getDiagnostics().Report(diag::err_extdefmap_parsing)
+    Context.getDiagnostics().Report(Loc, diag::err_extdefmap_parsing)
         << IE.getFileName() << IE.getLineNum();
     return;
   case index_error_code::multiple_definitions:
-    Context.getDiagnostics().Report(diag::err_multiple_def_index)
+    Context.getDiagnostics().Report(Loc, diag::err_multiple_def_index)
         << IE.getLineNum();
     return;
   case index_error_code::triple_mismatch:
-    Context.getDiagnostics().Report(diag::warn_ctu_incompat_triple)
+    Context.getDiagnostics().Report(Loc, diag::warn_ctu_incompat_triple)
         << IE.getFileName() << IE.getTripleToName() << IE.getTripleFromName();
     return;
   case index_error_code::success:

diff  --git a/clang/lib/StaticAnalyzer/Core/CallEvent.cpp 
b/clang/lib/StaticAnalyzer/Core/CallEvent.cpp
index cd52083a278ae..e23f9fff4533a 100644
--- a/clang/lib/StaticAnalyzer/Core/CallEvent.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CallEvent.cpp
@@ -623,7 +623,9 @@ RuntimeDefinition AnyFunctionCall::getRuntimeDefinition() 
const {
   if (!CTUDeclOrError) {
     handleAllErrors(CTUDeclOrError.takeError(),
                     [&](const cross_tu::IndexError &IE) {
-                      CTUCtx.emitCrossTUDiagnostics(IE);
+                      auto Loc = getOriginExpr() ? 
getOriginExpr()->getExprLoc()
+                                                 : FD->getLocation();
+                      CTUCtx.emitCrossTUDiagnostics(IE, Loc);
                     });
     return {};
   }

diff  --git a/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp 
b/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
index 1cc2b8dfb9bee..4f435b70013ec 100644
--- a/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
+++ b/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
@@ -309,7 +309,7 @@ class AnalysisConsumer : public AnalysisASTConsumer,
     if (!CTUDeclOrError) {
       handleAllErrors(CTUDeclOrError.takeError(),
                       [&](const cross_tu::IndexError &IE) {
-                        CTU.emitCrossTUDiagnostics(IE);
+                        CTU.emitCrossTUDiagnostics(IE, VD->getLocation());
                       });
     }
 

diff  --git a/clang/test/Analysis/ctu/diag/invalid-index.cpp 
b/clang/test/Analysis/ctu/diag/invalid-index.cpp
index 640e6ceee654c..f8cc98c346fa1 100644
--- a/clang/test/Analysis/ctu/diag/invalid-index.cpp
+++ b/clang/test/Analysis/ctu/diag/invalid-index.cpp
@@ -8,11 +8,8 @@
 // RUN:   -analyzer-config ctu-dir=%t \
 // RUN:   -verify %s
 
-// We expect an error in this file, but without a location.
-// expected-error-re@./invalid-index.cpp:*{{error parsing index file: 
'{{.+}}externalDefMap.txt' line: 1 '<USR-Length>:<USR> <File-Path>' format 
expected}}
-
 int foo(int);
 
 void test() {
-  foo(1);
+  foo(1); // expected-error-re{{error parsing index file: 
'{{.+}}externalDefMap.txt' line: 1 '<USR-Length>:<USR> <File-Path>' format 
expected}}
 }

diff  --git a/clang/test/Analysis/ctu/diag/missing-index.cpp 
b/clang/test/Analysis/ctu/diag/missing-index.cpp
index e09042f9538f7..351537165a466 100644
--- a/clang/test/Analysis/ctu/diag/missing-index.cpp
+++ b/clang/test/Analysis/ctu/diag/missing-index.cpp
@@ -8,11 +8,8 @@
 // RUN:   -analyzer-config ctu-index-name=non-existing.txt \
 // RUN:   -verify %s
 
-// We expect an error in this file, but without a location.
-// expected-error-re@./missing-index.cpp:*{{error opening 
'{{.+}}non-existing.txt': required by the CrossTU functionality}}
-
 int foo(int);
 
 void test() {
-  foo(1);
+  foo(1); // expected-error-re{{error opening '{{.+}}non-existing.txt': 
required by the CrossTU functionality}}
 }

diff  --git a/clang/test/Analysis/ctu/
diff erent-triples.cpp b/clang/test/Analysis/ctu/
diff erent-triples.cpp
index 95b7f8d4c3fed..17170772c0207 100644
--- a/clang/test/Analysis/ctu/
diff erent-triples.cpp
+++ b/clang/test/Analysis/ctu/
diff erent-triples.cpp
@@ -10,11 +10,8 @@
 // RUN:   -Werror=ctu \
 // RUN:   -verify %s
 
-// We expect an error in this file, but without a location.
-// expected-error-re@./
diff erent-triples.cpp:*{{imported AST from {{.*}} had been generated for a 
diff erent target, current: powerpc64-montavista-linux-gnu, imported: 
x86_64-pc-linux-gnu}}
-
 int f(int);
 
 int main() {
-  return f(5);
+  return f(5); // expected-error-re{{imported AST from '{{.*}}' had been 
generated for a 
diff erent target, current: powerpc64-montavista-linux-gnu, imported: 
x86_64-pc-linux-gnu}}
 }


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

Reply via email to