15
15
#include " clang/Basic/SourceManager.h"
16
16
#include " clang/Basic/Version.h"
17
17
#include " clang/CrossTU/CrossTranslationUnit.h"
18
+ #include " clang/Frontend/ASTUnit.h"
18
19
#include " clang/Lex/Preprocessor.h"
19
20
#include " clang/Lex/TokenConcatenation.h"
20
21
#include " clang/Rewrite/Core/HTMLRewrite.h"
@@ -75,12 +76,14 @@ class PlistPrinter {
75
76
const FIDMap& FM;
76
77
AnalyzerOptions &AnOpts;
77
78
const Preprocessor &PP;
79
+ const cross_tu::CrossTranslationUnitContext &CTU;
78
80
llvm::SmallVector<const PathDiagnosticMacroPiece *, 0 > MacroPieces;
79
81
80
82
public:
81
83
PlistPrinter (const FIDMap& FM, AnalyzerOptions &AnOpts,
82
- const Preprocessor &PP)
83
- : FM(FM), AnOpts(AnOpts), PP(PP) {
84
+ const Preprocessor &PP,
85
+ const cross_tu::CrossTranslationUnitContext &CTU)
86
+ : FM(FM), AnOpts(AnOpts), PP(PP), CTU(CTU) {
84
87
}
85
88
86
89
void ReportDiag (raw_ostream &o, const PathDiagnosticPiece& P) {
@@ -162,8 +165,8 @@ struct ExpansionInfo {
162
165
} // end of anonymous namespace
163
166
164
167
static void printBugPath (llvm::raw_ostream &o, const FIDMap& FM,
165
- AnalyzerOptions &AnOpts,
166
- const Preprocessor &PP ,
168
+ AnalyzerOptions &AnOpts, const Preprocessor &PP,
169
+ const cross_tu::CrossTranslationUnitContext &CTU ,
167
170
const PathPieces &Path);
168
171
169
172
// / Print coverage information to output stream {@code o}.
@@ -174,8 +177,9 @@ static void printCoverage(const PathDiagnostic *D,
174
177
FIDMap &FM,
175
178
llvm::raw_fd_ostream &o);
176
179
177
- static ExpansionInfo getExpandedMacro (SourceLocation MacroLoc,
178
- const Preprocessor &PP);
180
+ static ExpansionInfo
181
+ getExpandedMacro (SourceLocation MacroLoc, const Preprocessor &PP,
182
+ const cross_tu::CrossTranslationUnitContext &CTU);
179
183
180
184
// ===----------------------------------------------------------------------===//
181
185
// Methods of PlistPrinter.
@@ -349,7 +353,7 @@ void PlistPrinter::ReportMacroExpansions(raw_ostream &o, unsigned indent) {
349
353
350
354
for (const PathDiagnosticMacroPiece *P : MacroPieces) {
351
355
const SourceManager &SM = PP.getSourceManager ();
352
- ExpansionInfo EI = getExpandedMacro (P->getLocation ().asLocation (), PP);
356
+ ExpansionInfo EI = getExpandedMacro (P->getLocation ().asLocation (), PP, CTU );
353
357
354
358
Indent (o, indent) << " <dict>\n " ;
355
359
++indent;
@@ -471,10 +475,10 @@ static void printCoverage(const PathDiagnostic *D,
471
475
}
472
476
473
477
static void printBugPath (llvm::raw_ostream &o, const FIDMap& FM,
474
- AnalyzerOptions &AnOpts,
475
- const Preprocessor &PP ,
478
+ AnalyzerOptions &AnOpts, const Preprocessor &PP,
479
+ const cross_tu::CrossTranslationUnitContext &CTU ,
476
480
const PathPieces &Path) {
477
- PlistPrinter Printer (FM, AnOpts, PP);
481
+ PlistPrinter Printer (FM, AnOpts, PP, CTU );
478
482
assert (std::is_partitioned (
479
483
Path.begin (), Path.end (),
480
484
[](const std::shared_ptr<PathDiagnosticPiece> &E)
@@ -619,7 +623,7 @@ void PlistDiagnostics::FlushDiagnosticsImpl(
619
623
o << " <dict>\n " ;
620
624
621
625
const PathDiagnostic *D = *DI;
622
- printBugPath (o, FM, AnOpts, PP, D->path );
626
+ printBugPath (o, FM, AnOpts, PP, CTU, D->path );
623
627
624
628
// Output the bug type and bug category.
625
629
o << " <key>description</key>" ;
@@ -872,17 +876,23 @@ static const MacroInfo *getMacroInfoForLocation(const Preprocessor &PP,
872
876
// Definitions of helper functions and methods for expanding macros.
873
877
// ===----------------------------------------------------------------------===//
874
878
875
- static ExpansionInfo getExpandedMacro (SourceLocation MacroLoc,
876
- const Preprocessor &PP) {
879
+ static ExpansionInfo
880
+ getExpandedMacro (SourceLocation MacroLoc, const Preprocessor &PP,
881
+ const cross_tu::CrossTranslationUnitContext &CTU) {
882
+
883
+ const Preprocessor *PPToUse = &PP;
884
+ if (auto LocAndUnit = CTU.getImportedFromSourceLocation (MacroLoc)) {
885
+ MacroLoc = LocAndUnit->first ;
886
+ PPToUse = &LocAndUnit->second ->getPreprocessor ();
887
+ }
877
888
878
889
llvm::SmallString<200 > ExpansionBuf;
879
890
llvm::raw_svector_ostream OS (ExpansionBuf);
880
- TokenPrinter Printer (OS, PP );
891
+ TokenPrinter Printer (OS, *PPToUse );
881
892
llvm::SmallPtrSet<IdentifierInfo*, 8 > AlreadyProcessedTokens;
882
893
883
- std::string MacroName =
884
- getMacroNameAndPrintExpansion (Printer, MacroLoc, PP, MacroArgMap{},
885
- AlreadyProcessedTokens);
894
+ std::string MacroName = getMacroNameAndPrintExpansion (
895
+ Printer, MacroLoc, *PPToUse, MacroArgMap{}, AlreadyProcessedTokens);
886
896
return { MacroName, OS.str () };
887
897
}
888
898
0 commit comments