blob: 52af2a2bbeddb1184116ae768186cd59f56775c5 [file] [log] [blame]
John Thompson99794542013-10-31 12:23:32 +00001//===--- PPCallbacksTracker.cpp - Preprocessor tracker -*--*-------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===--------------------------------------------------------------------===//
9///
10/// \file
11/// \brief Implementations for preprocessor tracking.
12///
13/// See the header for details.
14///
15//===--------------------------------------------------------------------===//
16
17#include "PPCallbacksTracker.h"
18#include "clang/Lex/MacroArgs.h"
19#include "llvm/Support/raw_ostream.h"
20#include <stdarg.h>
21#include <stdio.h>
22
23// Utility functions.
24
25// Get a "file:line:column" source location string.
26static std::string getSourceLocationString(clang::Preprocessor &PP,
27 clang::SourceLocation Loc) {
28 if (Loc.isInvalid())
29 return std::string("(none)");
30
31 if (Loc.isFileID()) {
32 clang::PresumedLoc PLoc = PP.getSourceManager().getPresumedLoc(Loc);
33
34 if (PLoc.isInvalid()) {
35 return std::string("(invalid)");
36 }
37
38 std::string Str;
39 llvm::raw_string_ostream SS(Str);
40
41 // The macro expansion and spelling pos is identical for file locs.
42 SS << "\"" << PLoc.getFilename() << ':' << PLoc.getLine() << ':'
43 << PLoc.getColumn() << "\"";
44
45 std::string Result = SS.str();
46
47 // YAML treats backslash as escape, so use forward slashes.
48 std::replace(Result.begin(), Result.end(), '\\', '/');
49
50 return Result;
51 }
52
53 return std::string("(nonfile)");
54}
55
56// Enum string tables.
57
58// FileChangeReason strings.
59static const char *FileChangeReasonStrings[] = {
60 "EnterFile", "ExitFile", "SystemHeaderPragma", "RenameFile"
61};
62
63// CharacteristicKind strings.
64static const char *CharacteristicKindStrings[] = { "C_User", "C_System",
65 "C_ExternCSystem" };
66
67// MacroDirective::Kind strings.
68static const char *MacroDirectiveKindStrings[] = { "MD_Define", "MD_Undefine",
69 "MD_Visibility" };
70
71// PragmaIntroducerKind strings.
72static const char *PragmaIntroducerKindStrings[] = { "PIK_HashPragma",
73 "PIK__Pragma",
74 "PIK___pragma" };
75
76// PragmaMessageKind strings.
77static const char *PragmaMessageKindStrings[] = { "PMK_Message", "PMK_Warning",
78 "PMK_Error" };
79
80// Mapping strings.
81static const char *MappingStrings[] = { "0", "MAP_IGNORE",
82 "MAP_WARNING", "MAP_ERROR",
83 "MAP_FATAL" };
84
85// PPCallbacksTracker functions.
86
87PPCallbacksTracker::PPCallbacksTracker(llvm::SmallSet<std::string, 4> &Ignore,
88 std::vector<CallbackCall> &CallbackCalls,
89 clang::Preprocessor &PP)
90 : CallbackCalls(CallbackCalls), Ignore(Ignore), PP(PP) {}
91
92PPCallbacksTracker::~PPCallbacksTracker() {}
93
94// Callback functions.
95
96// Callback invoked whenever a source file is entered or exited.
97void PPCallbacksTracker::FileChanged(
98 clang::SourceLocation Loc, clang::PPCallbacks::FileChangeReason Reason,
99 clang::SrcMgr::CharacteristicKind FileType, clang::FileID PrevFID) {
100 beginCallback("FileChanged");
101 appendArgument("Loc", Loc);
102 appendArgument("Reason", Reason, FileChangeReasonStrings);
103 appendArgument("FileType", FileType, CharacteristicKindStrings);
104 appendArgument("PrevFID", PrevFID);
105}
106
107// Callback invoked whenever a source file is skipped as the result
108// of header guard optimization.
109void
110PPCallbacksTracker::FileSkipped(const clang::FileEntry &ParentFile,
111 const clang::Token &FilenameTok,
112 clang::SrcMgr::CharacteristicKind FileType) {
113 beginCallback("FileSkipped");
114 appendArgument("ParentFile", &ParentFile);
115 appendArgument("FilenameTok", FilenameTok);
116 appendArgument("FileType", FileType, CharacteristicKindStrings);
117}
118
119// Callback invoked whenever an inclusion directive results in a
120// file-not-found error.
121bool
122PPCallbacksTracker::FileNotFound(llvm::StringRef FileName,
123 llvm::SmallVectorImpl<char> &RecoveryPath) {
124 beginCallback("FileNotFound");
125 appendFilePathArgument("FileName", FileName);
126 return false;
127}
128
129// Callback invoked whenever an inclusion directive of
130// any kind (#include, #import, etc.) has been processed, regardless
131// of whether the inclusion will actually result in an inclusion.
132void PPCallbacksTracker::InclusionDirective(
133 clang::SourceLocation HashLoc, const clang::Token &IncludeTok,
134 llvm::StringRef FileName, bool IsAngled,
135 clang::CharSourceRange FilenameRange, const clang::FileEntry *File,
136 llvm::StringRef SearchPath, llvm::StringRef RelativePath,
137 const clang::Module *Imported) {
138 beginCallback("InclusionDirective");
139 appendArgument("IncludeTok", IncludeTok);
140 appendFilePathArgument("FileName", FileName);
141 appendArgument("IsAngled", IsAngled);
142 appendArgument("FilenameRange", FilenameRange);
143 appendArgument("File", File);
144 appendFilePathArgument("SearchPath", SearchPath);
145 appendFilePathArgument("RelativePath", RelativePath);
146 appendArgument("Imported", Imported);
147}
148
149// Callback invoked whenever there was an explicit module-import
150// syntax.
151void PPCallbacksTracker::moduleImport(clang::SourceLocation ImportLoc,
152 clang::ModuleIdPath Path,
153 const clang::Module *Imported) {
154 beginCallback("moduleImport");
155 appendArgument("ImportLoc", ImportLoc);
156 appendArgument("Path", Path);
157 appendArgument("Imported", Imported);
158}
159
160// Callback invoked when the end of the main file is reached.
161// No subsequent callbacks will be made.
162void PPCallbacksTracker::EndOfMainFile() { beginCallback("EndOfMainFile"); }
163
164// Callback invoked when a #ident or #sccs directive is read.
165void PPCallbacksTracker::Ident(clang::SourceLocation Loc,
166 const std::string &Str) {
167 beginCallback("Ident");
168 appendArgument("Loc", Loc);
169 appendArgument("Path", Str);
170}
171
172// Callback invoked when start reading any pragma directive.
173void
174PPCallbacksTracker::PragmaDirective(clang::SourceLocation Loc,
175 clang::PragmaIntroducerKind Introducer) {
176 beginCallback("PragmaDirective");
177 appendArgument("Loc", Loc);
178 appendArgument("Path", Introducer, PragmaIntroducerKindStrings);
179}
180
181// Callback invoked when a #pragma comment directive is read.
182void PPCallbacksTracker::PragmaComment(clang::SourceLocation Loc,
183 const clang::IdentifierInfo *Kind,
184 const std::string &Str) {
185 beginCallback("PragmaComment");
186 appendArgument("Loc", Loc);
187 appendArgument("Kind", Kind);
188 appendArgument("Str", Str);
189}
190
191// Callback invoked when a #pragma detect_mismatch directive is
192// read.
193void PPCallbacksTracker::PragmaDetectMismatch(clang::SourceLocation Loc,
194 const std::string &Name,
195 const std::string &Value) {
196 beginCallback("PragmaDetectMismatch");
197 appendArgument("Loc", Loc);
198 appendArgument("Name", Name);
199 appendArgument("Value", Value);
200}
201
202// Callback invoked when a #pragma clang __debug directive is read.
203void PPCallbacksTracker::PragmaDebug(clang::SourceLocation Loc,
204 llvm::StringRef DebugType) {
205 beginCallback("PragmaDebug");
206 appendArgument("Loc", Loc);
207 appendArgument("DebugType", DebugType);
208}
209
210// Callback invoked when a #pragma message directive is read.
211void PPCallbacksTracker::PragmaMessage(
212 clang::SourceLocation Loc, llvm::StringRef Namespace,
213 clang::PPCallbacks::PragmaMessageKind Kind, llvm::StringRef Str) {
214 beginCallback("PragmaMessage");
215 appendArgument("Loc", Loc);
216 appendArgument("Namespace", Namespace);
217 appendArgument("Kind", Kind, PragmaMessageKindStrings);
218 appendArgument("Str", Str);
219}
220
221// Callback invoked when a #pragma gcc dianostic push directive
222// is read.
223void PPCallbacksTracker::PragmaDiagnosticPush(clang::SourceLocation Loc,
224 llvm::StringRef Namespace) {
225 beginCallback("PragmaDiagnosticPush");
226 appendArgument("Loc", Loc);
227 appendArgument("Namespace", Namespace);
228}
229
230// Callback invoked when a #pragma gcc dianostic pop directive
231// is read.
232void PPCallbacksTracker::PragmaDiagnosticPop(clang::SourceLocation Loc,
233 llvm::StringRef Namespace) {
234 beginCallback("PragmaDiagnosticPop");
235 appendArgument("Loc", Loc);
236 appendArgument("Namespace", Namespace);
237}
238
239// Callback invoked when a #pragma gcc dianostic directive is read.
240void PPCallbacksTracker::PragmaDiagnostic(clang::SourceLocation Loc,
241 llvm::StringRef Namespace,
242 clang::diag::Mapping Mapping,
243 llvm::StringRef Str) {
244 beginCallback("PragmaDiagnostic");
245 appendArgument("Loc", Loc);
246 appendArgument("Namespace", Namespace);
247 appendArgument("Mapping", Mapping, MappingStrings);
248 appendArgument("Str", Str);
249}
250
251// Called when an OpenCL extension is either disabled or
252// enabled with a pragma.
253void PPCallbacksTracker::PragmaOpenCLExtension(
254 clang::SourceLocation NameLoc, const clang::IdentifierInfo *Name,
255 clang::SourceLocation StateLoc, unsigned State) {
256 beginCallback("PragmaOpenCLExtension");
257 appendArgument("NameLoc", NameLoc);
258 appendArgument("Name", Name);
259 appendArgument("StateLoc", StateLoc);
260 appendArgument("State", (int)State);
261}
262
263// Callback invoked when a #pragma warning directive is read.
264void PPCallbacksTracker::PragmaWarning(clang::SourceLocation Loc,
265 llvm::StringRef WarningSpec,
266 llvm::ArrayRef<int> Ids) {
267 beginCallback("PragmaWarning");
268 appendArgument("Loc", Loc);
269 appendArgument("WarningSpec", WarningSpec);
270
271 std::string Str;
272 llvm::raw_string_ostream SS(Str);
273 SS << "[";
274 for (int i = 0, e = Ids.size(); i != e; ++i) {
275 if (i)
276 SS << ", ";
277 SS << Ids[i];
278 }
279 appendArgument("Ids", SS.str());
280}
281
282// Callback invoked when a #pragma warning(push) directive is read.
283void PPCallbacksTracker::PragmaWarningPush(clang::SourceLocation Loc,
284 int Level) {
285 beginCallback("PragmaWarningPush");
286 appendArgument("Loc", Loc);
287 appendArgument("Level", Level);
288}
289
290// Callback invoked when a #pragma warning(pop) directive is read.
291void PPCallbacksTracker::PragmaWarningPop(clang::SourceLocation Loc) {
292 beginCallback("PragmaWarningPop");
293 appendArgument("Loc", Loc);
294}
295
296// Called by Preprocessor::HandleMacroExpandedIdentifier when a
297// macro invocation is found.
298void
299PPCallbacksTracker::MacroExpands(const clang::Token &MacroNameTok,
300 const clang::MacroDirective *MacroDirective,
301 clang::SourceRange Range,
302 const clang::MacroArgs *Args) {
303 beginCallback("MacroExpands");
304 appendArgument("MacroNameTok", MacroNameTok);
305 appendArgument("MacroDirective", MacroDirective);
306 appendArgument("Range", Range);
307 appendArgument("Args", Args);
308}
309
310// Hook called whenever a macro definition is seen.
311void
312PPCallbacksTracker::MacroDefined(const clang::Token &MacroNameTok,
313 const clang::MacroDirective *MacroDirective) {
314 beginCallback("MacroDefined");
315 appendArgument("MacroNameTok", MacroNameTok);
316 appendArgument("MacroDirective", MacroDirective);
317}
318
319// Hook called whenever a macro #undef is seen.
320void PPCallbacksTracker::MacroUndefined(
321 const clang::Token &MacroNameTok,
322 const clang::MacroDirective *MacroDirective) {
323 beginCallback("MacroUndefined");
324 appendArgument("MacroNameTok", MacroNameTok);
325 appendArgument("MacroDirective", MacroDirective);
326}
327
328// Hook called whenever the 'defined' operator is seen.
329void PPCallbacksTracker::Defined(const clang::Token &MacroNameTok,
330 const clang::MacroDirective *MacroDirective,
331 clang::SourceRange Range) {
332 beginCallback("Defined");
333 appendArgument("MacroNameTok", MacroNameTok);
334 appendArgument("MacroDirective", MacroDirective);
335 appendArgument("Range", Range);
336}
337
338// Hook called when a source range is skipped.
339void PPCallbacksTracker::SourceRangeSkipped(clang::SourceRange Range) {
340 beginCallback("SourceRangeSkipped");
341 appendArgument("Range", Range);
342}
343
344// Hook called whenever an #if is seen.
345void PPCallbacksTracker::If(clang::SourceLocation Loc,
346 clang::SourceRange ConditionRange,
347 bool ConditionValue) {
348 beginCallback("If");
349 appendArgument("Loc", Loc);
350 appendArgument("ConditionRange", ConditionRange);
351 appendArgument("ConditionValue", ConditionValue);
352}
353
354// Hook called whenever an #elif is seen.
355void PPCallbacksTracker::Elif(clang::SourceLocation Loc,
356 clang::SourceRange ConditionRange,
357 bool ConditionValue,
358 clang::SourceLocation IfLoc) {
359 beginCallback("Elif");
360 appendArgument("Loc", Loc);
361 appendArgument("ConditionRange", ConditionRange);
362 appendArgument("ConditionValue", ConditionValue);
363 appendArgument("IfLoc", IfLoc);
364}
365
366// Hook called whenever an #ifdef is seen.
367void PPCallbacksTracker::Ifdef(clang::SourceLocation Loc,
368 const clang::Token &MacroNameTok,
369 const clang::MacroDirective *MacroDirective) {
370 beginCallback("Ifdef");
371 appendArgument("Loc", Loc);
372 appendArgument("MacroNameTok", MacroNameTok);
373 appendArgument("MacroDirective", MacroDirective);
374}
375
376// Hook called whenever an #ifndef is seen.
377void PPCallbacksTracker::Ifndef(clang::SourceLocation Loc,
378 const clang::Token &MacroNameTok,
379 const clang::MacroDirective *MacroDirective) {
380 beginCallback("Ifndef");
381 appendArgument("Loc", Loc);
382 appendArgument("MacroNameTok", MacroNameTok);
383 appendArgument("MacroDirective", MacroDirective);
384}
385
386// Hook called whenever an #else is seen.
387void PPCallbacksTracker::Else(clang::SourceLocation Loc,
388 clang::SourceLocation IfLoc) {
389 beginCallback("Else");
390 appendArgument("Loc", Loc);
391 appendArgument("IfLoc", IfLoc);
392}
393
394// Hook called whenever an #endif is seen.
395void PPCallbacksTracker::Endif(clang::SourceLocation Loc,
396 clang::SourceLocation IfLoc) {
397 beginCallback("Endif");
398 appendArgument("Loc", Loc);
399 appendArgument("IfLoc", IfLoc);
400}
401
402// Helper functions.
403
404// Start a new callback.
405void PPCallbacksTracker::beginCallback(const char *Name) {
406 DisableTrace = Ignore.count(std::string(Name));
407 if (DisableTrace)
408 return;
409 CallbackCalls.push_back(CallbackCall(Name));
410}
411
412// Append a bool argument to the top trace item.
413void PPCallbacksTracker::appendArgument(const char *Name, bool Value) {
414 appendArgument(Name, (Value ? "true" : "false"));
415}
416
417// Append an int argument to the top trace item.
418void PPCallbacksTracker::appendArgument(const char *Name, int Value) {
419 std::string Str;
420 llvm::raw_string_ostream SS(Str);
421 SS << Value;
422 appendArgument(Name, SS.str());
423}
424
425// Append a string argument to the top trace item.
426void PPCallbacksTracker::appendArgument(const char *Name, const char *Value) {
427 if (DisableTrace)
428 return;
429 CallbackCalls.back().Arguments.push_back(Argument(Name, Value));
430}
431
432// Append a string object argument to the top trace item.
433void PPCallbacksTracker::appendArgument(const char *Name,
434 llvm::StringRef Value) {
435 appendArgument(Name, Value.str());
436}
437
438// Append a string object argument to the top trace item.
439void PPCallbacksTracker::appendArgument(const char *Name,
440 const std::string &Value) {
441 appendArgument(Name, Value.c_str());
442}
443
444// Append a token argument to the top trace item.
445void PPCallbacksTracker::appendArgument(const char *Name,
446 const clang::Token &Value) {
447 appendArgument(Name, PP.getSpelling(Value));
448}
449
450// Append an enum argument to the top trace item.
451void PPCallbacksTracker::appendArgument(const char *Name, int Value,
452 const char *Strings[]) {
453 appendArgument(Name, Strings[Value]);
454}
455
456// Append a FileID argument to the top trace item.
457void PPCallbacksTracker::appendArgument(const char *Name, clang::FileID Value) {
458 if (Value.isInvalid()) {
459 appendArgument(Name, "(invalid)");
460 return;
461 }
462 const clang::FileEntry *FileEntry =
463 PP.getSourceManager().getFileEntryForID(Value);
464 if (FileEntry == 0) {
465 appendArgument(Name, "(getFileEntryForID failed)");
466 return;
467 }
468 appendFilePathArgument(Name, FileEntry->getName());
469}
470
471// Append a FileEntry argument to the top trace item.
472void PPCallbacksTracker::appendArgument(const char *Name,
473 const clang::FileEntry *Value) {
474 if (Value == 0) {
475 appendArgument(Name, "(null)");
476 return;
477 }
478 appendFilePathArgument(Name, Value->getName());
479}
480
481// Append a SourceLocation argument to the top trace item.
482void PPCallbacksTracker::appendArgument(const char *Name,
483 clang::SourceLocation Value) {
484 if (Value.isInvalid()) {
485 appendArgument(Name, "(invalid)");
486 return;
487 }
488 appendArgument(Name, getSourceLocationString(PP, Value).c_str());
489}
490
491// Append a SourceRange argument to the top trace item.
492void PPCallbacksTracker::appendArgument(const char *Name,
493 clang::SourceRange Value) {
494 if (DisableTrace)
495 return;
496 if (Value.isInvalid()) {
497 appendArgument(Name, "(invalid)");
498 return;
499 }
500 std::string Str;
501 llvm::raw_string_ostream SS(Str);
502 SS << "[" << getSourceLocationString(PP, Value.getBegin()) << ", "
503 << getSourceLocationString(PP, Value.getEnd()) << "]";
504 appendArgument(Name, SS.str());
505}
506
507// Append a CharSourceRange argument to the top trace item.
508void PPCallbacksTracker::appendArgument(const char *Name,
509 clang::CharSourceRange Value) {
510 if (Value.isInvalid()) {
511 appendArgument(Name, "(invalid)");
512 return;
513 }
514 appendArgument(Name, getSourceString(Value).str().c_str());
515}
516
517// Append a SourceLocation argument to the top trace item.
518void PPCallbacksTracker::appendArgument(const char *Name,
519 clang::ModuleIdPath Value) {
520 if (DisableTrace)
521 return;
522 std::string Str;
523 llvm::raw_string_ostream SS(Str);
524 SS << "[";
525 for (int I = 0, E = Value.size(); I != E; ++I) {
526 if (I)
527 SS << ", ";
528 SS << "{"
529 << "Name: " << Value[I].first->getName() << ", "
530 << "Loc:" << getSourceLocationString(PP, Value[I].second) << "}";
531 }
532 SS << "]";
533 appendArgument(Name, SS.str());
534}
535
536// Append an IdentifierInfo argument to the top trace item.
537void PPCallbacksTracker::appendArgument(const char *Name,
538 const clang::IdentifierInfo *Value) {
539 if (!Value) {
540 appendArgument(Name, "(null)");
541 return;
542 }
543 appendArgument(Name, Value->getName().str().c_str());
544}
545
546// Append a MacroDirective argument to the top trace item.
547void PPCallbacksTracker::appendArgument(const char *Name,
548 const clang::MacroDirective *Value) {
549 if (!Value) {
550 appendArgument(Name, "(null)");
551 return;
552 }
553 appendArgument(Name, MacroDirectiveKindStrings[Value->getKind()]);
554}
555
556// Append a MacroArgs argument to the top trace item.
557void PPCallbacksTracker::appendArgument(const char *Name,
558 const clang::MacroArgs *Value) {
559 if (!Value) {
560 appendArgument(Name, "(null)");
561 return;
562 }
563 std::string Str;
564 llvm::raw_string_ostream SS(Str);
565 SS << "[";
566 // The argument tokens might include end tokens, so we reflect how
567 // how getUnexpArgument provides the arguments.
568 for (int I = 0, E = Value->getNumArguments(); I < E; ++I) {
569 const clang::Token *Current = Value->getUnexpArgument(I);
570 int TokenCount = Value->getArgLength(Current) + 1; // include EOF
571 E -= TokenCount;
572 if (I)
573 SS << ", ";
574 // We're assuming tokens are contiguous, as otherwise we have no
575 // other way to get at them.
576 --TokenCount;
577 for (int TokenIndex = 0; TokenIndex < TokenCount; ++TokenIndex, ++Current) {
578 if (TokenIndex)
579 SS << " ";
580 // We need to be careful here because the arguments might not be legal in
581 // YAML, so we use the token name for anything but identifiers and
582 // numeric literals.
583 if (Current->isAnyIdentifier() ||
584 Current->is(clang::tok::numeric_constant)) {
585 SS << PP.getSpelling(*Current);
586 } else {
587 SS << "<" << Current->getName() << ">";
588 }
589 }
590 }
591 SS << "]";
592 appendArgument(Name, SS.str());
593}
594
595// Append a Module argument to the top trace item.
596void PPCallbacksTracker::appendArgument(const char *Name,
597 const clang::Module *Value) {
598 if (!Value) {
599 appendArgument(Name, "(null)");
600 return;
601 }
602 appendArgument(Name, Value->Name.c_str());
603}
604
605// Append a double-quoted argument to the top trace item.
606void PPCallbacksTracker::appendQuotedArgument(const char *Name,
607 const std::string &Value) {
608 std::string Str;
609 llvm::raw_string_ostream SS(Str);
610 SS << "\"" << Value << "\"";
611 appendArgument(Name, SS.str());
612}
613
614// Append a double-quoted file path argument to the top trace item.
615void PPCallbacksTracker::appendFilePathArgument(const char *Name,
616 llvm::StringRef Value) {
617 std::string Path(Value);
618 // YAML treats backslash as escape, so use forward slashes.
619 std::replace(Path.begin(), Path.end(), '\\', '/');
620 appendQuotedArgument(Name, Path);
621}
622
623// Get the raw source string of the range.
624llvm::StringRef
625PPCallbacksTracker::getSourceString(clang::CharSourceRange Range) {
626 const char *B = PP.getSourceManager().getCharacterData(Range.getBegin());
627 const char *E = PP.getSourceManager().getCharacterData(Range.getEnd());
628 return llvm::StringRef(B, E - B);
629}