1 //===--- PPCallbacksTracker.cpp - Preprocessor tracker -*--*---------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// Implementations for preprocessor tracking.
12 /// See the header for details.
14 //===----------------------------------------------------------------------===//
16 #include "PPCallbacksTracker.h"
17 #include "clang/Basic/FileManager.h"
18 #include "clang/Lex/MacroArgs.h"
19 #include "llvm/Support/raw_ostream.h"
24 // Get a "file:line:column" source location string.
25 static std::string
getSourceLocationString(Preprocessor
&PP
,
28 return std::string("(none)");
31 PresumedLoc PLoc
= PP
.getSourceManager().getPresumedLoc(Loc
);
33 if (PLoc
.isInvalid()) {
34 return std::string("(invalid)");
38 llvm::raw_string_ostream
SS(Str
);
40 // The macro expansion and spelling pos is identical for file locs.
41 SS
<< "\"" << PLoc
.getFilename() << ':' << PLoc
.getLine() << ':'
42 << PLoc
.getColumn() << "\"";
44 std::string Result
= SS
.str();
46 // YAML treats backslash as escape, so use forward slashes.
47 std::replace(Result
.begin(), Result
.end(), '\\', '/');
52 return std::string("(nonfile)");
55 // Enum string tables.
57 // FileChangeReason strings.
58 static const char *const FileChangeReasonStrings
[] = {
59 "EnterFile", "ExitFile", "SystemHeaderPragma", "RenameFile"
62 // CharacteristicKind strings.
63 static const char *const CharacteristicKindStrings
[] = { "C_User", "C_System",
66 // MacroDirective::Kind strings.
67 static const char *const MacroDirectiveKindStrings
[] = {
68 "MD_Define","MD_Undefine", "MD_Visibility"
71 // PragmaIntroducerKind strings.
72 static const char *const PragmaIntroducerKindStrings
[] = { "PIK_HashPragma",
76 // PragmaMessageKind strings.
77 static const char *const PragmaMessageKindStrings
[] = {
78 "PMK_Message", "PMK_Warning", "PMK_Error"
81 // PragmaWarningSpecifier strings.
82 static const char *const PragmaWarningSpecifierStrings
[] = {
83 "PWS_Default", "PWS_Disable", "PWS_Error", "PWS_Once", "PWS_Suppress",
84 "PWS_Level1", "PWS_Level2", "PWS_Level3", "PWS_Level4",
87 // ConditionValueKind strings.
88 static const char *const ConditionValueKindStrings
[] = {
89 "CVK_NotEvaluated", "CVK_False", "CVK_True"
93 static const char *const MappingStrings
[] = { "0", "MAP_IGNORE",
94 "MAP_REMARK", "MAP_WARNING",
95 "MAP_ERROR", "MAP_FATAL" };
97 // PPCallbacksTracker functions.
99 PPCallbacksTracker::PPCallbacksTracker(const FilterType
&Filters
,
100 std::vector
<CallbackCall
> &CallbackCalls
,
102 : CallbackCalls(CallbackCalls
), Filters(Filters
), PP(PP
) {}
104 PPCallbacksTracker::~PPCallbacksTracker() {}
106 // Callback functions.
108 // Callback invoked whenever a source file is entered or exited.
109 void PPCallbacksTracker::FileChanged(SourceLocation Loc
,
110 PPCallbacks::FileChangeReason Reason
,
111 SrcMgr::CharacteristicKind FileType
,
113 beginCallback("FileChanged");
114 appendArgument("Loc", Loc
);
115 appendArgument("Reason", Reason
, FileChangeReasonStrings
);
116 appendArgument("FileType", FileType
, CharacteristicKindStrings
);
117 appendArgument("PrevFID", PrevFID
);
120 // Callback invoked whenever a source file is skipped as the result
121 // of header guard optimization.
122 void PPCallbacksTracker::FileSkipped(const FileEntryRef
&SkippedFile
,
123 const Token
&FilenameTok
,
124 SrcMgr::CharacteristicKind FileType
) {
125 beginCallback("FileSkipped");
126 appendArgument("ParentFile", SkippedFile
);
127 appendArgument("FilenameTok", FilenameTok
);
128 appendArgument("FileType", FileType
, CharacteristicKindStrings
);
131 // Callback invoked whenever an inclusion directive of
132 // any kind (#include, #import, etc.) has been processed, regardless
133 // of whether the inclusion will actually result in an inclusion.
134 void PPCallbacksTracker::InclusionDirective(
135 SourceLocation HashLoc
, const Token
&IncludeTok
, llvm::StringRef FileName
,
136 bool IsAngled
, CharSourceRange FilenameRange
, OptionalFileEntryRef File
,
137 llvm::StringRef SearchPath
, llvm::StringRef RelativePath
,
138 const Module
*Imported
, SrcMgr::CharacteristicKind FileType
) {
139 beginCallback("InclusionDirective");
140 appendArgument("HashLoc", HashLoc
);
141 appendArgument("IncludeTok", IncludeTok
);
142 appendFilePathArgument("FileName", FileName
);
143 appendArgument("IsAngled", IsAngled
);
144 appendArgument("FilenameRange", FilenameRange
);
145 appendArgument("File", File
);
146 appendFilePathArgument("SearchPath", SearchPath
);
147 appendFilePathArgument("RelativePath", RelativePath
);
148 appendArgument("Imported", Imported
);
151 // Callback invoked whenever there was an explicit module-import
153 void PPCallbacksTracker::moduleImport(SourceLocation ImportLoc
,
155 const Module
*Imported
) {
156 beginCallback("moduleImport");
157 appendArgument("ImportLoc", ImportLoc
);
158 appendArgument("Path", Path
);
159 appendArgument("Imported", Imported
);
162 // Callback invoked when the end of the main file is reached.
163 // No subsequent callbacks will be made.
164 void PPCallbacksTracker::EndOfMainFile() { beginCallback("EndOfMainFile"); }
166 // Callback invoked when a #ident or #sccs directive is read.
167 void PPCallbacksTracker::Ident(SourceLocation Loc
, llvm::StringRef Str
) {
168 beginCallback("Ident");
169 appendArgument("Loc", Loc
);
170 appendArgument("Str", Str
);
173 // Callback invoked when start reading any pragma directive.
174 void PPCallbacksTracker::PragmaDirective(SourceLocation Loc
,
175 PragmaIntroducerKind Introducer
) {
176 beginCallback("PragmaDirective");
177 appendArgument("Loc", Loc
);
178 appendArgument("Introducer", Introducer
, PragmaIntroducerKindStrings
);
181 // Callback invoked when a #pragma comment directive is read.
182 void PPCallbacksTracker::PragmaComment(SourceLocation Loc
,
183 const IdentifierInfo
*Kind
,
184 llvm::StringRef Str
) {
185 beginCallback("PragmaComment");
186 appendArgument("Loc", Loc
);
187 appendArgument("Kind", Kind
);
188 appendArgument("Str", Str
);
191 // Callback invoked when a #pragma detect_mismatch directive is
193 void PPCallbacksTracker::PragmaDetectMismatch(SourceLocation Loc
,
194 llvm::StringRef Name
,
195 llvm::StringRef Value
) {
196 beginCallback("PragmaDetectMismatch");
197 appendArgument("Loc", Loc
);
198 appendArgument("Name", Name
);
199 appendArgument("Value", Value
);
202 // Callback invoked when a #pragma clang __debug directive is read.
203 void PPCallbacksTracker::PragmaDebug(SourceLocation Loc
,
204 llvm::StringRef DebugType
) {
205 beginCallback("PragmaDebug");
206 appendArgument("Loc", Loc
);
207 appendArgument("DebugType", DebugType
);
210 // Callback invoked when a #pragma message directive is read.
211 void PPCallbacksTracker::PragmaMessage(SourceLocation Loc
,
212 llvm::StringRef Namespace
,
213 PPCallbacks::PragmaMessageKind Kind
,
214 llvm::StringRef Str
) {
215 beginCallback("PragmaMessage");
216 appendArgument("Loc", Loc
);
217 appendArgument("Namespace", Namespace
);
218 appendArgument("Kind", Kind
, PragmaMessageKindStrings
);
219 appendArgument("Str", Str
);
222 // Callback invoked when a #pragma gcc diagnostic push directive
224 void PPCallbacksTracker::PragmaDiagnosticPush(SourceLocation Loc
,
225 llvm::StringRef Namespace
) {
226 beginCallback("PragmaDiagnosticPush");
227 appendArgument("Loc", Loc
);
228 appendArgument("Namespace", Namespace
);
231 // Callback invoked when a #pragma gcc diagnostic pop directive
233 void PPCallbacksTracker::PragmaDiagnosticPop(SourceLocation Loc
,
234 llvm::StringRef Namespace
) {
235 beginCallback("PragmaDiagnosticPop");
236 appendArgument("Loc", Loc
);
237 appendArgument("Namespace", Namespace
);
240 // Callback invoked when a #pragma gcc diagnostic directive is read.
241 void PPCallbacksTracker::PragmaDiagnostic(SourceLocation Loc
,
242 llvm::StringRef Namespace
,
243 diag::Severity Mapping
,
244 llvm::StringRef Str
) {
245 beginCallback("PragmaDiagnostic");
246 appendArgument("Loc", Loc
);
247 appendArgument("Namespace", Namespace
);
248 appendArgument("Mapping", (unsigned)Mapping
, MappingStrings
);
249 appendArgument("Str", Str
);
252 // Called when an OpenCL extension is either disabled or
253 // enabled with a pragma.
254 void PPCallbacksTracker::PragmaOpenCLExtension(SourceLocation NameLoc
,
255 const IdentifierInfo
*Name
,
256 SourceLocation StateLoc
,
258 beginCallback("PragmaOpenCLExtension");
259 appendArgument("NameLoc", NameLoc
);
260 appendArgument("Name", Name
);
261 appendArgument("StateLoc", StateLoc
);
262 appendArgument("State", (int)State
);
265 // Callback invoked when a #pragma warning directive is read.
266 void PPCallbacksTracker::PragmaWarning(SourceLocation Loc
,
267 PragmaWarningSpecifier WarningSpec
,
268 llvm::ArrayRef
<int> Ids
) {
269 beginCallback("PragmaWarning");
270 appendArgument("Loc", Loc
);
271 appendArgument("WarningSpec", WarningSpec
, PragmaWarningSpecifierStrings
);
274 llvm::raw_string_ostream
SS(Str
);
276 for (int i
= 0, e
= Ids
.size(); i
!= e
; ++i
) {
282 appendArgument("Ids", SS
.str());
285 // Callback invoked when a #pragma warning(push) directive is read.
286 void PPCallbacksTracker::PragmaWarningPush(SourceLocation Loc
, int Level
) {
287 beginCallback("PragmaWarningPush");
288 appendArgument("Loc", Loc
);
289 appendArgument("Level", Level
);
292 // Callback invoked when a #pragma warning(pop) directive is read.
293 void PPCallbacksTracker::PragmaWarningPop(SourceLocation Loc
) {
294 beginCallback("PragmaWarningPop");
295 appendArgument("Loc", Loc
);
298 // Callback invoked when a #pragma execution_character_set(push) directive
300 void PPCallbacksTracker::PragmaExecCharsetPush(SourceLocation Loc
,
302 beginCallback("PragmaExecCharsetPush");
303 appendArgument("Loc", Loc
);
304 appendArgument("Charset", Str
);
307 // Callback invoked when a #pragma execution_character_set(pop) directive
309 void PPCallbacksTracker::PragmaExecCharsetPop(SourceLocation Loc
) {
310 beginCallback("PragmaExecCharsetPop");
311 appendArgument("Loc", Loc
);
314 // Called by Preprocessor::HandleMacroExpandedIdentifier when a
315 // macro invocation is found.
316 void PPCallbacksTracker::MacroExpands(const Token
&MacroNameTok
,
317 const MacroDefinition
&MacroDefinition
,
319 const MacroArgs
*Args
) {
320 beginCallback("MacroExpands");
321 appendArgument("MacroNameTok", MacroNameTok
);
322 appendArgument("MacroDefinition", MacroDefinition
);
323 appendArgument("Range", Range
);
324 appendArgument("Args", Args
);
327 // Hook called whenever a macro definition is seen.
328 void PPCallbacksTracker::MacroDefined(const Token
&MacroNameTok
,
329 const MacroDirective
*MacroDirective
) {
330 beginCallback("MacroDefined");
331 appendArgument("MacroNameTok", MacroNameTok
);
332 appendArgument("MacroDirective", MacroDirective
);
335 // Hook called whenever a macro #undef is seen.
336 void PPCallbacksTracker::MacroUndefined(const Token
&MacroNameTok
,
337 const MacroDefinition
&MacroDefinition
,
338 const MacroDirective
*Undef
) {
339 beginCallback("MacroUndefined");
340 appendArgument("MacroNameTok", MacroNameTok
);
341 appendArgument("MacroDefinition", MacroDefinition
);
344 // Hook called whenever the 'defined' operator is seen.
345 void PPCallbacksTracker::Defined(const Token
&MacroNameTok
,
346 const MacroDefinition
&MacroDefinition
,
348 beginCallback("Defined");
349 appendArgument("MacroNameTok", MacroNameTok
);
350 appendArgument("MacroDefinition", MacroDefinition
);
351 appendArgument("Range", Range
);
354 // Hook called when a source range is skipped.
355 void PPCallbacksTracker::SourceRangeSkipped(SourceRange Range
,
356 SourceLocation EndifLoc
) {
357 beginCallback("SourceRangeSkipped");
358 appendArgument("Range", SourceRange(Range
.getBegin(), EndifLoc
));
361 // Hook called whenever an #if is seen.
362 void PPCallbacksTracker::If(SourceLocation Loc
, SourceRange ConditionRange
,
363 ConditionValueKind ConditionValue
) {
365 appendArgument("Loc", Loc
);
366 appendArgument("ConditionRange", ConditionRange
);
367 appendArgument("ConditionValue", ConditionValue
, ConditionValueKindStrings
);
370 // Hook called whenever an #elif is seen.
371 void PPCallbacksTracker::Elif(SourceLocation Loc
, SourceRange ConditionRange
,
372 ConditionValueKind ConditionValue
,
373 SourceLocation IfLoc
) {
374 beginCallback("Elif");
375 appendArgument("Loc", Loc
);
376 appendArgument("ConditionRange", ConditionRange
);
377 appendArgument("ConditionValue", ConditionValue
, ConditionValueKindStrings
);
378 appendArgument("IfLoc", IfLoc
);
381 // Hook called whenever an #ifdef is seen.
382 void PPCallbacksTracker::Ifdef(SourceLocation Loc
, const Token
&MacroNameTok
,
383 const MacroDefinition
&MacroDefinition
) {
384 beginCallback("Ifdef");
385 appendArgument("Loc", Loc
);
386 appendArgument("MacroNameTok", MacroNameTok
);
387 appendArgument("MacroDefinition", MacroDefinition
);
390 // Hook called whenever an #ifndef is seen.
391 void PPCallbacksTracker::Ifndef(SourceLocation Loc
, const Token
&MacroNameTok
,
392 const MacroDefinition
&MacroDefinition
) {
393 beginCallback("Ifndef");
394 appendArgument("Loc", Loc
);
395 appendArgument("MacroNameTok", MacroNameTok
);
396 appendArgument("MacroDefinition", MacroDefinition
);
399 // Hook called whenever an #else is seen.
400 void PPCallbacksTracker::Else(SourceLocation Loc
, SourceLocation IfLoc
) {
401 beginCallback("Else");
402 appendArgument("Loc", Loc
);
403 appendArgument("IfLoc", IfLoc
);
406 // Hook called whenever an #endif is seen.
407 void PPCallbacksTracker::Endif(SourceLocation Loc
, SourceLocation IfLoc
) {
408 beginCallback("Endif");
409 appendArgument("Loc", Loc
);
410 appendArgument("IfLoc", IfLoc
);
415 // Start a new callback.
416 void PPCallbacksTracker::beginCallback(const char *Name
) {
417 auto R
= CallbackIsEnabled
.try_emplace(Name
, false);
419 llvm::StringRef
N(Name
);
420 for (const std::pair
<llvm::GlobPattern
, bool> &Filter
: Filters
)
421 if (Filter
.first
.match(N
))
422 R
.first
->second
= Filter
.second
;
424 DisableTrace
= !R
.first
->second
;
427 CallbackCalls
.push_back(CallbackCall(Name
));
430 // Append a bool argument to the top trace item.
431 void PPCallbacksTracker::appendArgument(const char *Name
, bool Value
) {
432 appendArgument(Name
, (Value
? "true" : "false"));
435 // Append an int argument to the top trace item.
436 void PPCallbacksTracker::appendArgument(const char *Name
, int Value
) {
438 llvm::raw_string_ostream
SS(Str
);
440 appendArgument(Name
, SS
.str());
443 // Append a string argument to the top trace item.
444 void PPCallbacksTracker::appendArgument(const char *Name
, const char *Value
) {
447 CallbackCalls
.back().Arguments
.push_back(Argument
{Name
, Value
});
450 // Append a string object argument to the top trace item.
451 void PPCallbacksTracker::appendArgument(const char *Name
,
452 llvm::StringRef Value
) {
453 appendArgument(Name
, Value
.str());
456 // Append a string object argument to the top trace item.
457 void PPCallbacksTracker::appendArgument(const char *Name
,
458 const std::string
&Value
) {
459 appendArgument(Name
, Value
.c_str());
462 // Append a token argument to the top trace item.
463 void PPCallbacksTracker::appendArgument(const char *Name
, const Token
&Value
) {
464 appendArgument(Name
, PP
.getSpelling(Value
));
467 // Append an enum argument to the top trace item.
468 void PPCallbacksTracker::appendArgument(const char *Name
, int Value
,
469 const char *const Strings
[]) {
470 appendArgument(Name
, Strings
[Value
]);
473 // Append a FileID argument to the top trace item.
474 void PPCallbacksTracker::appendArgument(const char *Name
, FileID Value
) {
475 if (Value
.isInvalid()) {
476 appendArgument(Name
, "(invalid)");
479 OptionalFileEntryRef FileEntry
=
480 PP
.getSourceManager().getFileEntryRefForID(Value
);
482 appendArgument(Name
, "(getFileEntryForID failed)");
485 appendFilePathArgument(Name
, FileEntry
->getName());
488 // Append a FileEntry argument to the top trace item.
489 void PPCallbacksTracker::appendArgument(const char *Name
,
490 OptionalFileEntryRef Value
) {
492 appendArgument(Name
, "(null)");
495 appendArgument(Name
, *Value
);
498 void PPCallbacksTracker::appendArgument(const char *Name
, FileEntryRef Value
) {
499 appendFilePathArgument(Name
, Value
.getName());
502 // Append a SourceLocation argument to the top trace item.
503 void PPCallbacksTracker::appendArgument(const char *Name
,
504 SourceLocation Value
) {
505 if (Value
.isInvalid()) {
506 appendArgument(Name
, "(invalid)");
509 appendArgument(Name
, getSourceLocationString(PP
, Value
).c_str());
512 // Append a SourceRange argument to the top trace item.
513 void PPCallbacksTracker::appendArgument(const char *Name
, SourceRange Value
) {
516 if (Value
.isInvalid()) {
517 appendArgument(Name
, "(invalid)");
521 llvm::raw_string_ostream
SS(Str
);
522 SS
<< "[" << getSourceLocationString(PP
, Value
.getBegin()) << ", "
523 << getSourceLocationString(PP
, Value
.getEnd()) << "]";
524 appendArgument(Name
, SS
.str());
527 // Append a CharSourceRange argument to the top trace item.
528 void PPCallbacksTracker::appendArgument(const char *Name
,
529 CharSourceRange Value
) {
530 if (Value
.isInvalid()) {
531 appendArgument(Name
, "(invalid)");
534 appendArgument(Name
, getSourceString(Value
).str().c_str());
537 // Append a SourceLocation argument to the top trace item.
538 void PPCallbacksTracker::appendArgument(const char *Name
, ModuleIdPath Value
) {
542 llvm::raw_string_ostream
SS(Str
);
544 for (int I
= 0, E
= Value
.size(); I
!= E
; ++I
) {
548 << "Name: " << Value
[I
].first
->getName() << ", "
549 << "Loc: " << getSourceLocationString(PP
, Value
[I
].second
) << "}";
552 appendArgument(Name
, SS
.str());
555 // Append an IdentifierInfo argument to the top trace item.
556 void PPCallbacksTracker::appendArgument(const char *Name
,
557 const IdentifierInfo
*Value
) {
559 appendArgument(Name
, "(null)");
562 appendArgument(Name
, Value
->getName().str().c_str());
565 // Append a MacroDirective argument to the top trace item.
566 void PPCallbacksTracker::appendArgument(const char *Name
,
567 const MacroDirective
*Value
) {
569 appendArgument(Name
, "(null)");
572 appendArgument(Name
, MacroDirectiveKindStrings
[Value
->getKind()]);
575 // Append a MacroDefinition argument to the top trace item.
576 void PPCallbacksTracker::appendArgument(const char *Name
,
577 const MacroDefinition
&Value
) {
579 llvm::raw_string_ostream
SS(Str
);
582 if (Value
.getLocalDirective()) {
586 for (auto *MM
: Value
.getModuleMacros()) {
588 SS
<< MM
->getOwningModule()->getFullModuleName();
591 appendArgument(Name
, SS
.str());
594 // Append a MacroArgs argument to the top trace item.
595 void PPCallbacksTracker::appendArgument(const char *Name
,
596 const MacroArgs
*Value
) {
598 appendArgument(Name
, "(null)");
602 llvm::raw_string_ostream
SS(Str
);
605 // Each argument is a series of contiguous Tokens, terminated by a eof.
606 // Go through each argument printing tokens until we reach eof.
607 for (unsigned I
= 0; I
< Value
->getNumMacroArguments(); ++I
) {
608 const Token
*Current
= Value
->getUnexpArgument(I
);
612 while (Current
->isNot(tok::eof
)) {
615 // We need to be careful here because the arguments might not be legal in
616 // YAML, so we use the token name for anything but identifiers and
618 if (Current
->isAnyIdentifier() || Current
->is(tok::numeric_constant
)) {
619 SS
<< PP
.getSpelling(*Current
);
621 SS
<< "<" << Current
->getName() << ">";
628 appendArgument(Name
, SS
.str());
631 // Append a Module argument to the top trace item.
632 void PPCallbacksTracker::appendArgument(const char *Name
, const Module
*Value
) {
634 appendArgument(Name
, "(null)");
637 appendArgument(Name
, Value
->Name
.c_str());
640 // Append a double-quoted argument to the top trace item.
641 void PPCallbacksTracker::appendQuotedArgument(const char *Name
,
642 const std::string
&Value
) {
644 llvm::raw_string_ostream
SS(Str
);
645 SS
<< "\"" << Value
<< "\"";
646 appendArgument(Name
, SS
.str());
649 // Append a double-quoted file path argument to the top trace item.
650 void PPCallbacksTracker::appendFilePathArgument(const char *Name
,
651 llvm::StringRef Value
) {
652 std::string
Path(Value
);
653 // YAML treats backslash as escape, so use forward slashes.
654 std::replace(Path
.begin(), Path
.end(), '\\', '/');
655 appendQuotedArgument(Name
, Path
);
658 // Get the raw source string of the range.
659 llvm::StringRef
PPCallbacksTracker::getSourceString(CharSourceRange Range
) {
660 const char *B
= PP
.getSourceManager().getCharacterData(Range
.getBegin());
661 const char *E
= PP
.getSourceManager().getCharacterData(Range
.getEnd());
662 return llvm::StringRef(B
, E
- B
);
665 } // namespace pp_trace