Index: clang/include/clang/CrossTU/CrossTranslationUnit.h =================================================================== --- clang/include/clang/CrossTU/CrossTranslationUnit.h +++ clang/include/clang/CrossTU/CrossTranslationUnit.h @@ -15,6 +15,7 @@ #define LLVM_CLANG_CROSSTU_CROSSTRANSLATIONUNIT_H #include "clang/AST/ASTImporterSharedState.h" +#include "clang/Analysis/MacroExpansionContext.h" #include "clang/Basic/LLVM.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/Optional.h" @@ -182,16 +183,15 @@ /// Emit diagnostics for the user for potential configuration errors. void emitCrossTUDiagnostics(const IndexError &IE); - /// Determine the original source location in the original TU for an - /// imported source location. + /// Returns the MacroExpansionContext for the imported TU to which the given + /// corresponds. /// \p ToLoc Source location in the imported-to AST. - /// \return Source location in the imported-from AST and the corresponding - /// ASTUnit object (the AST was loaded from a file using an internal ASTUnit - /// object that is returned here). - /// If any error happens (ToLoc is a non-imported source location) empty is + /// If any error happens (\p ToLoc is a non-imported source location or macro + /// expansion tracking for imported TUs are not implemented yet) empty is /// returned. - llvm::Optional> - getImportedFromSourceLocation(const clang::SourceLocation &ToLoc) const; + llvm::Optional + getMacroExpansionContextForSourceLocation( + const clang::SourceLocation &ToLoc) const; private: using ImportedFileIDMap = Index: clang/lib/CrossTU/CrossTranslationUnit.cpp =================================================================== --- clang/lib/CrossTU/CrossTranslationUnit.cpp +++ clang/lib/CrossTU/CrossTranslationUnit.cpp @@ -763,22 +763,11 @@ return *NewImporter; } -llvm::Optional> -CrossTranslationUnitContext::getImportedFromSourceLocation( +llvm::Optional +CrossTranslationUnitContext::getMacroExpansionContextForSourceLocation( const clang::SourceLocation &ToLoc) const { - const SourceManager &SM = Context.getSourceManager(); - auto DecToLoc = SM.getDecomposedLoc(ToLoc); - - auto I = ImportedFileIDs.find(DecToLoc.first); - if (I == ImportedFileIDs.end()) - return {}; - - FileID FromID = I->second.first; - clang::ASTUnit *Unit = I->second.second; - SourceLocation FromLoc = - Unit->getSourceManager().getComposedLoc(FromID, DecToLoc.second); - - return std::make_pair(FromLoc, Unit); + // FIXME: Implement: Record such a context for every imported ASTUnit; lookup. + return llvm::None; } } // namespace cross_tu Index: clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp =================================================================== --- clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp +++ clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp @@ -830,9 +830,9 @@ const cross_tu::CrossTranslationUnitContext &CTU, const MacroExpansionContext &MacroExpansions, const SourceManager &SM) { - if (auto LocAndUnit = CTU.getImportedFromSourceLocation(MacroExpansionLoc)) { - // TODO: Implement macro expansions for CTU. - return llvm::None; + if (auto CTUMacroExpCtx = + CTU.getMacroExpansionContextForSourceLocation(MacroExpansionLoc)) { + return CTUMacroExpCtx->getExpandedText(MacroExpansionLoc); } return MacroExpansions.getExpandedText(MacroExpansionLoc); } Index: clang/test/Analysis/plist-macros-with-expansion-ctu.c =================================================================== --- clang/test/Analysis/plist-macros-with-expansion-ctu.c +++ clang/test/Analysis/plist-macros-with-expansion-ctu.c @@ -2,13 +2,13 @@ // RUN: mkdir -p %t/ctudir // RUN: %clang_cc1 -emit-pch -o %t/ctudir/plist-macros-ctu.c.ast %S/Inputs/plist-macros-ctu.c // RUN: cp %S/Inputs/plist-macros-with-expansion-ctu.c.externalDefMap.txt %t/ctudir/externalDefMap.txt - +// // RUN: %clang_analyze_cc1 -analyzer-checker=core \ // RUN: -analyzer-config experimental-enable-naive-ctu-analysis=true \ // RUN: -analyzer-config ctu-dir=%t/ctudir \ // RUN: -analyzer-config expand-macros=true \ // RUN: -analyzer-output=plist-multi-file -o %t.plist -verify %s -// XFAIL: * +// // Check the macro expansions from the plist output here, to make the test more // understandable. // RUN: FileCheck --input-file=%t.plist %s @@ -23,25 +23,30 @@ F3(&X); *X = 1; // expected-warning{{Dereference of null pointer}} } -// CHECK: nameM1 -// CHECK-NEXT: expansion*Z = (int *)0 - +// FIXME: Macro expansion for other TUs should also work. +// CHECK: macro_expansions +// CHECK-NEXT: +// CHECK-NEXT: void test1() { int *X; F1(&X); *X = 1; // expected-warning{{Dereference of null pointer}} } -// CHECK: nameM -// CHECK-NEXT: expansion*X = (int *)0 + +// CHECK: macro_expansions +// CHECK-NEXT: +// CHECK-NEXT: void test2() { int *X; F2(&X); *X = 1; // expected-warning{{Dereference of null pointer}} } -// CHECK: nameM -// CHECK-NEXT: expansion*Y = (int *)0 + +// CHECK: macro_expansions +// CHECK-NEXT: +// CHECK-NEXT: #define M F1(&X) @@ -50,10 +55,20 @@ M; *X = 1; // expected-warning{{Dereference of null pointer}} } -// CHECK: nameM -// CHECK-NEXT: expansionF1(&X) -// CHECK: nameM -// CHECK-NEXT: expansion*X = (int *)0 +// Macro expansions for the main TU still works, even in CTU mode. +// CHECK: macro_expansions +// CHECK-NEXT: +// CHECK-NEXT: +// CHECK-NEXT: location +// CHECK-NEXT: +// CHECK-NEXT: line55 +// CHECK-NEXT: col3 +// CHECK-NEXT: file0 +// CHECK-NEXT: +// CHECK-NEXT: nameM +// CHECK-NEXT: expansionF1 (&X ) +// CHECK-NEXT: +// CHECK-NEXT: #undef M #define M F2(&X) @@ -64,10 +79,19 @@ *X = 1; // expected-warning{{Dereference of null pointer}} } -// CHECK: nameM -// CHECK-NEXT: expansionF2(&X) -// CHECK: nameM -// CHECK-NEXT: expansion*Y = (int *)0 +// CHECK: macro_expansions +// CHECK-NEXT: +// CHECK-NEXT: +// CHECK-NEXT: location +// CHECK-NEXT: +// CHECK-NEXT: line78 +// CHECK-NEXT: col3 +// CHECK-NEXT: file0 +// CHECK-NEXT: +// CHECK-NEXT: nameM +// CHECK-NEXT: expansionF2 (&X ) +// CHECK-NEXT: +// CHECK-NEXT: void test_h() { int *X; @@ -75,5 +99,6 @@ *X = 1; // expected-warning{{Dereference of null pointer}} } -// CHECK: nameM_H -// CHECK-NEXT: expansion*A = (int *)0 +// CHECK: macro_expansions +// CHECK-NEXT: +// CHECK-NEXT: Index: clang/unittests/CrossTU/CrossTranslationUnitTest.cpp =================================================================== --- clang/unittests/CrossTU/CrossTranslationUnitTest.cpp +++ clang/unittests/CrossTU/CrossTranslationUnitTest.cpp @@ -91,26 +91,6 @@ *Success = NewFD && NewFD->hasBody() && !OrigFDHasBody; if (NewFD) { - // Check GetImportedFromSourceLocation. - llvm::Optional> SLocResult = - CTU.getImportedFromSourceLocation(NewFD->getLocation()); - EXPECT_TRUE(SLocResult); - if (SLocResult) { - SourceLocation OrigSLoc = (*SLocResult).first; - ASTUnit *OrigUnit = (*SLocResult).second; - // OrigUnit is created internally by CTU (is not the - // ASTWithDefinition). - TranslationUnitDecl *OrigTU = - OrigUnit->getASTContext().getTranslationUnitDecl(); - const FunctionDecl *FDWithDefinition = FindFInTU(OrigTU); - EXPECT_TRUE(FDWithDefinition); - if (FDWithDefinition) { - EXPECT_EQ(FDWithDefinition->getName(), "f"); - EXPECT_TRUE(FDWithDefinition->isThisDeclarationADefinition()); - EXPECT_EQ(OrigSLoc, FDWithDefinition->getLocation()); - } - } - // Check parent map. const DynTypedNodeList ParentsAfterImport = Ctx.getParentMapContext().getParents(*FD);