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