• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
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 // This code simply runs the preprocessor on the input file and prints out the
11 // result.  This is the traditional behavior of the -E option.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/Frontend/Utils.h"
16 #include "clang/Basic/CharInfo.h"
17 #include "clang/Basic/Diagnostic.h"
18 #include "clang/Basic/SourceManager.h"
19 #include "clang/Frontend/PreprocessorOutputOptions.h"
20 #include "clang/Lex/MacroInfo.h"
21 #include "clang/Lex/PPCallbacks.h"
22 #include "clang/Lex/Pragma.h"
23 #include "clang/Lex/Preprocessor.h"
24 #include "clang/Lex/TokenConcatenation.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include <cstdio>
31 using namespace clang;
32 
33 /// PrintMacroDefinition - Print a macro definition in a form that will be
34 /// properly accepted back as a definition.
PrintMacroDefinition(const IdentifierInfo & II,const MacroInfo & MI,Preprocessor & PP,raw_ostream & OS)35 static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI,
36                                  Preprocessor &PP, raw_ostream &OS) {
37   OS << "#define " << II.getName();
38 
39   if (MI.isFunctionLike()) {
40     OS << '(';
41     if (!MI.arg_empty()) {
42       MacroInfo::arg_iterator AI = MI.arg_begin(), E = MI.arg_end();
43       for (; AI+1 != E; ++AI) {
44         OS << (*AI)->getName();
45         OS << ',';
46       }
47 
48       // Last argument.
49       if ((*AI)->getName() == "__VA_ARGS__")
50         OS << "...";
51       else
52         OS << (*AI)->getName();
53     }
54 
55     if (MI.isGNUVarargs())
56       OS << "...";  // #define foo(x...)
57 
58     OS << ')';
59   }
60 
61   // GCC always emits a space, even if the macro body is empty.  However, do not
62   // want to emit two spaces if the first token has a leading space.
63   if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace())
64     OS << ' ';
65 
66   SmallString<128> SpellingBuffer;
67   for (const auto &T : MI.tokens()) {
68     if (T.hasLeadingSpace())
69       OS << ' ';
70 
71     OS << PP.getSpelling(T, SpellingBuffer);
72   }
73 }
74 
75 //===----------------------------------------------------------------------===//
76 // Preprocessed token printer
77 //===----------------------------------------------------------------------===//
78 
79 namespace {
80 class PrintPPOutputPPCallbacks : public PPCallbacks {
81   Preprocessor &PP;
82   SourceManager &SM;
83   TokenConcatenation ConcatInfo;
84 public:
85   raw_ostream &OS;
86 private:
87   unsigned CurLine;
88 
89   bool EmittedTokensOnThisLine;
90   bool EmittedDirectiveOnThisLine;
91   SrcMgr::CharacteristicKind FileType;
92   SmallString<512> CurFilename;
93   bool Initialized;
94   bool DisableLineMarkers;
95   bool DumpDefines;
96   bool UseLineDirectives;
97   bool IsFirstFileEntered;
98 public:
PrintPPOutputPPCallbacks(Preprocessor & pp,raw_ostream & os,bool lineMarkers,bool defines,bool UseLineDirectives)99   PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os, bool lineMarkers,
100                            bool defines, bool UseLineDirectives)
101       : PP(pp), SM(PP.getSourceManager()), ConcatInfo(PP), OS(os),
102         DisableLineMarkers(lineMarkers), DumpDefines(defines),
103         UseLineDirectives(UseLineDirectives) {
104     CurLine = 0;
105     CurFilename += "<uninit>";
106     EmittedTokensOnThisLine = false;
107     EmittedDirectiveOnThisLine = false;
108     FileType = SrcMgr::C_User;
109     Initialized = false;
110     IsFirstFileEntered = false;
111   }
112 
setEmittedTokensOnThisLine()113   void setEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
hasEmittedTokensOnThisLine() const114   bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
115 
setEmittedDirectiveOnThisLine()116   void setEmittedDirectiveOnThisLine() { EmittedDirectiveOnThisLine = true; }
hasEmittedDirectiveOnThisLine() const117   bool hasEmittedDirectiveOnThisLine() const {
118     return EmittedDirectiveOnThisLine;
119   }
120 
121   bool startNewLineIfNeeded(bool ShouldUpdateCurrentLine = true);
122 
123   void FileChanged(SourceLocation Loc, FileChangeReason Reason,
124                    SrcMgr::CharacteristicKind FileType,
125                    FileID PrevFID) override;
126   void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
127                           StringRef FileName, bool IsAngled,
128                           CharSourceRange FilenameRange, const FileEntry *File,
129                           StringRef SearchPath, StringRef RelativePath,
130                           const Module *Imported) override;
131   void Ident(SourceLocation Loc, StringRef str) override;
132   void PragmaMessage(SourceLocation Loc, StringRef Namespace,
133                      PragmaMessageKind Kind, StringRef Str) override;
134   void PragmaDebug(SourceLocation Loc, StringRef DebugType) override;
135   void PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) override;
136   void PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) override;
137   void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
138                         diag::Severity Map, StringRef Str) override;
139   void PragmaWarning(SourceLocation Loc, StringRef WarningSpec,
140                      ArrayRef<int> Ids) override;
141   void PragmaWarningPush(SourceLocation Loc, int Level) override;
142   void PragmaWarningPop(SourceLocation Loc) override;
143 
144   bool HandleFirstTokOnLine(Token &Tok);
145 
146   /// Move to the line of the provided source location. This will
147   /// return true if the output stream required adjustment or if
148   /// the requested location is on the first line.
MoveToLine(SourceLocation Loc)149   bool MoveToLine(SourceLocation Loc) {
150     PresumedLoc PLoc = SM.getPresumedLoc(Loc);
151     if (PLoc.isInvalid())
152       return false;
153     return MoveToLine(PLoc.getLine()) || (PLoc.getLine() == 1);
154   }
155   bool MoveToLine(unsigned LineNo);
156 
AvoidConcat(const Token & PrevPrevTok,const Token & PrevTok,const Token & Tok)157   bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok,
158                    const Token &Tok) {
159     return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok);
160   }
161   void WriteLineInfo(unsigned LineNo, const char *Extra=nullptr,
162                      unsigned ExtraLen=0);
LineMarkersAreDisabled() const163   bool LineMarkersAreDisabled() const { return DisableLineMarkers; }
164   void HandleNewlinesInToken(const char *TokStr, unsigned Len);
165 
166   /// MacroDefined - This hook is called whenever a macro definition is seen.
167   void MacroDefined(const Token &MacroNameTok,
168                     const MacroDirective *MD) override;
169 
170   /// MacroUndefined - This hook is called whenever a macro #undef is seen.
171   void MacroUndefined(const Token &MacroNameTok,
172                       const MacroDefinition &MD) override;
173 };
174 }  // end anonymous namespace
175 
WriteLineInfo(unsigned LineNo,const char * Extra,unsigned ExtraLen)176 void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
177                                              const char *Extra,
178                                              unsigned ExtraLen) {
179   startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
180 
181   // Emit #line directives or GNU line markers depending on what mode we're in.
182   if (UseLineDirectives) {
183     OS << "#line" << ' ' << LineNo << ' ' << '"';
184     OS.write_escaped(CurFilename);
185     OS << '"';
186   } else {
187     OS << '#' << ' ' << LineNo << ' ' << '"';
188     OS.write_escaped(CurFilename);
189     OS << '"';
190 
191     if (ExtraLen)
192       OS.write(Extra, ExtraLen);
193 
194     if (FileType == SrcMgr::C_System)
195       OS.write(" 3", 2);
196     else if (FileType == SrcMgr::C_ExternCSystem)
197       OS.write(" 3 4", 4);
198   }
199   OS << '\n';
200 }
201 
202 /// MoveToLine - Move the output to the source line specified by the location
203 /// object.  We can do this by emitting some number of \n's, or be emitting a
204 /// #line directive.  This returns false if already at the specified line, true
205 /// if some newlines were emitted.
MoveToLine(unsigned LineNo)206 bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo) {
207   // If this line is "close enough" to the original line, just print newlines,
208   // otherwise print a #line directive.
209   if (LineNo-CurLine <= 8) {
210     if (LineNo-CurLine == 1)
211       OS << '\n';
212     else if (LineNo == CurLine)
213       return false;    // Spelling line moved, but expansion line didn't.
214     else {
215       const char *NewLines = "\n\n\n\n\n\n\n\n";
216       OS.write(NewLines, LineNo-CurLine);
217     }
218   } else if (!DisableLineMarkers) {
219     // Emit a #line or line marker.
220     WriteLineInfo(LineNo, nullptr, 0);
221   } else {
222     // Okay, we're in -P mode, which turns off line markers.  However, we still
223     // need to emit a newline between tokens on different lines.
224     startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
225   }
226 
227   CurLine = LineNo;
228   return true;
229 }
230 
231 bool
startNewLineIfNeeded(bool ShouldUpdateCurrentLine)232 PrintPPOutputPPCallbacks::startNewLineIfNeeded(bool ShouldUpdateCurrentLine) {
233   if (EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) {
234     OS << '\n';
235     EmittedTokensOnThisLine = false;
236     EmittedDirectiveOnThisLine = false;
237     if (ShouldUpdateCurrentLine)
238       ++CurLine;
239     return true;
240   }
241 
242   return false;
243 }
244 
245 /// FileChanged - Whenever the preprocessor enters or exits a #include file
246 /// it invokes this handler.  Update our conception of the current source
247 /// position.
FileChanged(SourceLocation Loc,FileChangeReason Reason,SrcMgr::CharacteristicKind NewFileType,FileID PrevFID)248 void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
249                                            FileChangeReason Reason,
250                                        SrcMgr::CharacteristicKind NewFileType,
251                                        FileID PrevFID) {
252   // Unless we are exiting a #include, make sure to skip ahead to the line the
253   // #include directive was at.
254   SourceManager &SourceMgr = SM;
255 
256   PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc);
257   if (UserLoc.isInvalid())
258     return;
259 
260   unsigned NewLine = UserLoc.getLine();
261 
262   if (Reason == PPCallbacks::EnterFile) {
263     SourceLocation IncludeLoc = UserLoc.getIncludeLoc();
264     if (IncludeLoc.isValid())
265       MoveToLine(IncludeLoc);
266   } else if (Reason == PPCallbacks::SystemHeaderPragma) {
267     // GCC emits the # directive for this directive on the line AFTER the
268     // directive and emits a bunch of spaces that aren't needed. This is because
269     // otherwise we will emit a line marker for THIS line, which requires an
270     // extra blank line after the directive to avoid making all following lines
271     // off by one. We can do better by simply incrementing NewLine here.
272     NewLine += 1;
273   }
274 
275   CurLine = NewLine;
276 
277   CurFilename.clear();
278   CurFilename += UserLoc.getFilename();
279   FileType = NewFileType;
280 
281   if (DisableLineMarkers) {
282     startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
283     return;
284   }
285 
286   if (!Initialized) {
287     WriteLineInfo(CurLine);
288     Initialized = true;
289   }
290 
291   // Do not emit an enter marker for the main file (which we expect is the first
292   // entered file). This matches gcc, and improves compatibility with some tools
293   // which track the # line markers as a way to determine when the preprocessed
294   // output is in the context of the main file.
295   if (Reason == PPCallbacks::EnterFile && !IsFirstFileEntered) {
296     IsFirstFileEntered = true;
297     return;
298   }
299 
300   switch (Reason) {
301   case PPCallbacks::EnterFile:
302     WriteLineInfo(CurLine, " 1", 2);
303     break;
304   case PPCallbacks::ExitFile:
305     WriteLineInfo(CurLine, " 2", 2);
306     break;
307   case PPCallbacks::SystemHeaderPragma:
308   case PPCallbacks::RenameFile:
309     WriteLineInfo(CurLine);
310     break;
311   }
312 }
313 
InclusionDirective(SourceLocation HashLoc,const Token & IncludeTok,StringRef FileName,bool IsAngled,CharSourceRange FilenameRange,const FileEntry * File,StringRef SearchPath,StringRef RelativePath,const Module * Imported)314 void PrintPPOutputPPCallbacks::InclusionDirective(SourceLocation HashLoc,
315                                                   const Token &IncludeTok,
316                                                   StringRef FileName,
317                                                   bool IsAngled,
318                                                   CharSourceRange FilenameRange,
319                                                   const FileEntry *File,
320                                                   StringRef SearchPath,
321                                                   StringRef RelativePath,
322                                                   const Module *Imported) {
323   // When preprocessing, turn implicit imports into @imports.
324   // FIXME: This is a stop-gap until a more comprehensive "preprocessing with
325   // modules" solution is introduced.
326   if (Imported) {
327     startNewLineIfNeeded();
328     MoveToLine(HashLoc);
329     if (PP.getLangOpts().ObjC2) {
330       OS << "@import " << Imported->getFullModuleName() << ";"
331          << " /* clang -E: implicit import for \"" << File->getName()
332          << "\" */";
333     } else {
334       // FIXME: Preseve whether this was a
335       // #include/#include_next/#include_macros/#import.
336       OS << "#include "
337          << (IsAngled ? '<' : '"')
338          << FileName
339          << (IsAngled ? '>' : '"')
340          << " /* clang -E: implicit import for module "
341          << Imported->getFullModuleName() << " */";
342     }
343     // Since we want a newline after the @import, but not a #<line>, start a new
344     // line immediately.
345     EmittedTokensOnThisLine = true;
346     startNewLineIfNeeded();
347   }
348 }
349 
350 /// Ident - Handle #ident directives when read by the preprocessor.
351 ///
Ident(SourceLocation Loc,StringRef S)352 void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, StringRef S) {
353   MoveToLine(Loc);
354 
355   OS.write("#ident ", strlen("#ident "));
356   OS.write(S.begin(), S.size());
357   EmittedTokensOnThisLine = true;
358 }
359 
360 /// MacroDefined - This hook is called whenever a macro definition is seen.
MacroDefined(const Token & MacroNameTok,const MacroDirective * MD)361 void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok,
362                                             const MacroDirective *MD) {
363   const MacroInfo *MI = MD->getMacroInfo();
364   // Only print out macro definitions in -dD mode.
365   if (!DumpDefines ||
366       // Ignore __FILE__ etc.
367       MI->isBuiltinMacro()) return;
368 
369   MoveToLine(MI->getDefinitionLoc());
370   PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS);
371   setEmittedDirectiveOnThisLine();
372 }
373 
MacroUndefined(const Token & MacroNameTok,const MacroDefinition & MD)374 void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok,
375                                               const MacroDefinition &MD) {
376   // Only print out macro definitions in -dD mode.
377   if (!DumpDefines) return;
378 
379   MoveToLine(MacroNameTok.getLocation());
380   OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName();
381   setEmittedDirectiveOnThisLine();
382 }
383 
outputPrintable(raw_ostream & OS,StringRef Str)384 static void outputPrintable(raw_ostream &OS, StringRef Str) {
385   for (unsigned char Char : Str) {
386     if (isPrintable(Char) && Char != '\\' && Char != '"')
387       OS << (char)Char;
388     else // Output anything hard as an octal escape.
389       OS << '\\'
390          << (char)('0' + ((Char >> 6) & 7))
391          << (char)('0' + ((Char >> 3) & 7))
392          << (char)('0' + ((Char >> 0) & 7));
393   }
394 }
395 
PragmaMessage(SourceLocation Loc,StringRef Namespace,PragmaMessageKind Kind,StringRef Str)396 void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc,
397                                              StringRef Namespace,
398                                              PragmaMessageKind Kind,
399                                              StringRef Str) {
400   startNewLineIfNeeded();
401   MoveToLine(Loc);
402   OS << "#pragma ";
403   if (!Namespace.empty())
404     OS << Namespace << ' ';
405   switch (Kind) {
406     case PMK_Message:
407       OS << "message(\"";
408       break;
409     case PMK_Warning:
410       OS << "warning \"";
411       break;
412     case PMK_Error:
413       OS << "error \"";
414       break;
415   }
416 
417   outputPrintable(OS, Str);
418   OS << '"';
419   if (Kind == PMK_Message)
420     OS << ')';
421   setEmittedDirectiveOnThisLine();
422 }
423 
PragmaDebug(SourceLocation Loc,StringRef DebugType)424 void PrintPPOutputPPCallbacks::PragmaDebug(SourceLocation Loc,
425                                            StringRef DebugType) {
426   startNewLineIfNeeded();
427   MoveToLine(Loc);
428 
429   OS << "#pragma clang __debug ";
430   OS << DebugType;
431 
432   setEmittedDirectiveOnThisLine();
433 }
434 
435 void PrintPPOutputPPCallbacks::
PragmaDiagnosticPush(SourceLocation Loc,StringRef Namespace)436 PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) {
437   startNewLineIfNeeded();
438   MoveToLine(Loc);
439   OS << "#pragma " << Namespace << " diagnostic push";
440   setEmittedDirectiveOnThisLine();
441 }
442 
443 void PrintPPOutputPPCallbacks::
PragmaDiagnosticPop(SourceLocation Loc,StringRef Namespace)444 PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) {
445   startNewLineIfNeeded();
446   MoveToLine(Loc);
447   OS << "#pragma " << Namespace << " diagnostic pop";
448   setEmittedDirectiveOnThisLine();
449 }
450 
PragmaDiagnostic(SourceLocation Loc,StringRef Namespace,diag::Severity Map,StringRef Str)451 void PrintPPOutputPPCallbacks::PragmaDiagnostic(SourceLocation Loc,
452                                                 StringRef Namespace,
453                                                 diag::Severity Map,
454                                                 StringRef Str) {
455   startNewLineIfNeeded();
456   MoveToLine(Loc);
457   OS << "#pragma " << Namespace << " diagnostic ";
458   switch (Map) {
459   case diag::Severity::Remark:
460     OS << "remark";
461     break;
462   case diag::Severity::Warning:
463     OS << "warning";
464     break;
465   case diag::Severity::Error:
466     OS << "error";
467     break;
468   case diag::Severity::Ignored:
469     OS << "ignored";
470     break;
471   case diag::Severity::Fatal:
472     OS << "fatal";
473     break;
474   }
475   OS << " \"" << Str << '"';
476   setEmittedDirectiveOnThisLine();
477 }
478 
PragmaWarning(SourceLocation Loc,StringRef WarningSpec,ArrayRef<int> Ids)479 void PrintPPOutputPPCallbacks::PragmaWarning(SourceLocation Loc,
480                                              StringRef WarningSpec,
481                                              ArrayRef<int> Ids) {
482   startNewLineIfNeeded();
483   MoveToLine(Loc);
484   OS << "#pragma warning(" << WarningSpec << ':';
485   for (ArrayRef<int>::iterator I = Ids.begin(), E = Ids.end(); I != E; ++I)
486     OS << ' ' << *I;
487   OS << ')';
488   setEmittedDirectiveOnThisLine();
489 }
490 
PragmaWarningPush(SourceLocation Loc,int Level)491 void PrintPPOutputPPCallbacks::PragmaWarningPush(SourceLocation Loc,
492                                                  int Level) {
493   startNewLineIfNeeded();
494   MoveToLine(Loc);
495   OS << "#pragma warning(push";
496   if (Level >= 0)
497     OS << ", " << Level;
498   OS << ')';
499   setEmittedDirectiveOnThisLine();
500 }
501 
PragmaWarningPop(SourceLocation Loc)502 void PrintPPOutputPPCallbacks::PragmaWarningPop(SourceLocation Loc) {
503   startNewLineIfNeeded();
504   MoveToLine(Loc);
505   OS << "#pragma warning(pop)";
506   setEmittedDirectiveOnThisLine();
507 }
508 
509 /// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
510 /// is called for the first token on each new line.  If this really is the start
511 /// of a new logical line, handle it and return true, otherwise return false.
512 /// This may not be the start of a logical line because the "start of line"
513 /// marker is set for spelling lines, not expansion ones.
HandleFirstTokOnLine(Token & Tok)514 bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
515   // Figure out what line we went to and insert the appropriate number of
516   // newline characters.
517   if (!MoveToLine(Tok.getLocation()))
518     return false;
519 
520   // Print out space characters so that the first token on a line is
521   // indented for easy reading.
522   unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation());
523 
524   // The first token on a line can have a column number of 1, yet still expect
525   // leading white space, if a macro expansion in column 1 starts with an empty
526   // macro argument, or an empty nested macro expansion. In this case, move the
527   // token to column 2.
528   if (ColNo == 1 && Tok.hasLeadingSpace())
529     ColNo = 2;
530 
531   // This hack prevents stuff like:
532   // #define HASH #
533   // HASH define foo bar
534   // From having the # character end up at column 1, which makes it so it
535   // is not handled as a #define next time through the preprocessor if in
536   // -fpreprocessed mode.
537   if (ColNo <= 1 && Tok.is(tok::hash))
538     OS << ' ';
539 
540   // Otherwise, indent the appropriate number of spaces.
541   for (; ColNo > 1; --ColNo)
542     OS << ' ';
543 
544   return true;
545 }
546 
HandleNewlinesInToken(const char * TokStr,unsigned Len)547 void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
548                                                      unsigned Len) {
549   unsigned NumNewlines = 0;
550   for (; Len; --Len, ++TokStr) {
551     if (*TokStr != '\n' &&
552         *TokStr != '\r')
553       continue;
554 
555     ++NumNewlines;
556 
557     // If we have \n\r or \r\n, skip both and count as one line.
558     if (Len != 1 &&
559         (TokStr[1] == '\n' || TokStr[1] == '\r') &&
560         TokStr[0] != TokStr[1]) {
561       ++TokStr;
562       --Len;
563     }
564   }
565 
566   if (NumNewlines == 0) return;
567 
568   CurLine += NumNewlines;
569 }
570 
571 
572 namespace {
573 struct UnknownPragmaHandler : public PragmaHandler {
574   const char *Prefix;
575   PrintPPOutputPPCallbacks *Callbacks;
576 
577   // Set to true if tokens should be expanded
578   bool ShouldExpandTokens;
579 
UnknownPragmaHandler__anon74fbf6570211::UnknownPragmaHandler580   UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks,
581                        bool RequireTokenExpansion)
582       : Prefix(prefix), Callbacks(callbacks),
583         ShouldExpandTokens(RequireTokenExpansion) {}
HandlePragma__anon74fbf6570211::UnknownPragmaHandler584   void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
585                     Token &PragmaTok) override {
586     // Figure out what line we went to and insert the appropriate number of
587     // newline characters.
588     Callbacks->startNewLineIfNeeded();
589     Callbacks->MoveToLine(PragmaTok.getLocation());
590     Callbacks->OS.write(Prefix, strlen(Prefix));
591 
592     if (ShouldExpandTokens) {
593       // The first token does not have expanded macros. Expand them, if
594       // required.
595       auto Toks = llvm::make_unique<Token[]>(1);
596       Toks[0] = PragmaTok;
597       PP.EnterTokenStream(std::move(Toks), /*NumToks=*/1,
598                           /*DisableMacroExpansion=*/false);
599       PP.Lex(PragmaTok);
600     }
601     Token PrevToken;
602     Token PrevPrevToken;
603     PrevToken.startToken();
604     PrevPrevToken.startToken();
605 
606     // Read and print all of the pragma tokens.
607     while (PragmaTok.isNot(tok::eod)) {
608       if (PragmaTok.hasLeadingSpace() ||
609           Callbacks->AvoidConcat(PrevPrevToken, PrevToken, PragmaTok))
610         Callbacks->OS << ' ';
611       std::string TokSpell = PP.getSpelling(PragmaTok);
612       Callbacks->OS.write(&TokSpell[0], TokSpell.size());
613 
614       PrevPrevToken = PrevToken;
615       PrevToken = PragmaTok;
616 
617       if (ShouldExpandTokens)
618         PP.Lex(PragmaTok);
619       else
620         PP.LexUnexpandedToken(PragmaTok);
621     }
622     Callbacks->setEmittedDirectiveOnThisLine();
623   }
624 };
625 } // end anonymous namespace
626 
627 
PrintPreprocessedTokens(Preprocessor & PP,Token & Tok,PrintPPOutputPPCallbacks * Callbacks,raw_ostream & OS)628 static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
629                                     PrintPPOutputPPCallbacks *Callbacks,
630                                     raw_ostream &OS) {
631   bool DropComments = PP.getLangOpts().TraditionalCPP &&
632                       !PP.getCommentRetentionState();
633 
634   char Buffer[256];
635   Token PrevPrevTok, PrevTok;
636   PrevPrevTok.startToken();
637   PrevTok.startToken();
638   while (1) {
639     if (Callbacks->hasEmittedDirectiveOnThisLine()) {
640       Callbacks->startNewLineIfNeeded();
641       Callbacks->MoveToLine(Tok.getLocation());
642     }
643 
644     // If this token is at the start of a line, emit newlines if needed.
645     if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
646       // done.
647     } else if (Tok.hasLeadingSpace() ||
648                // If we haven't emitted a token on this line yet, PrevTok isn't
649                // useful to look at and no concatenation could happen anyway.
650                (Callbacks->hasEmittedTokensOnThisLine() &&
651                 // Don't print "-" next to "-", it would form "--".
652                 Callbacks->AvoidConcat(PrevPrevTok, PrevTok, Tok))) {
653       OS << ' ';
654     }
655 
656     if (DropComments && Tok.is(tok::comment)) {
657       // Skip comments. Normally the preprocessor does not generate
658       // tok::comment nodes at all when not keeping comments, but under
659       // -traditional-cpp the lexer keeps /all/ whitespace, including comments.
660       SourceLocation StartLoc = Tok.getLocation();
661       Callbacks->MoveToLine(StartLoc.getLocWithOffset(Tok.getLength()));
662     } else if (Tok.is(tok::annot_module_include) ||
663                Tok.is(tok::annot_module_begin) ||
664                Tok.is(tok::annot_module_end)) {
665       // PrintPPOutputPPCallbacks::InclusionDirective handles producing
666       // appropriate output here. Ignore this token entirely.
667       PP.Lex(Tok);
668       continue;
669     } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
670       OS << II->getName();
671     } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
672                Tok.getLiteralData()) {
673       OS.write(Tok.getLiteralData(), Tok.getLength());
674     } else if (Tok.getLength() < 256) {
675       const char *TokPtr = Buffer;
676       unsigned Len = PP.getSpelling(Tok, TokPtr);
677       OS.write(TokPtr, Len);
678 
679       // Tokens that can contain embedded newlines need to adjust our current
680       // line number.
681       if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
682         Callbacks->HandleNewlinesInToken(TokPtr, Len);
683     } else {
684       std::string S = PP.getSpelling(Tok);
685       OS.write(&S[0], S.size());
686 
687       // Tokens that can contain embedded newlines need to adjust our current
688       // line number.
689       if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
690         Callbacks->HandleNewlinesInToken(&S[0], S.size());
691     }
692     Callbacks->setEmittedTokensOnThisLine();
693 
694     if (Tok.is(tok::eof)) break;
695 
696     PrevPrevTok = PrevTok;
697     PrevTok = Tok;
698     PP.Lex(Tok);
699   }
700 }
701 
702 typedef std::pair<const IdentifierInfo *, MacroInfo *> id_macro_pair;
MacroIDCompare(const id_macro_pair * LHS,const id_macro_pair * RHS)703 static int MacroIDCompare(const id_macro_pair *LHS, const id_macro_pair *RHS) {
704   return LHS->first->getName().compare(RHS->first->getName());
705 }
706 
DoPrintMacros(Preprocessor & PP,raw_ostream * OS)707 static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) {
708   // Ignore unknown pragmas.
709   PP.IgnorePragmas();
710 
711   // -dM mode just scans and ignores all tokens in the files, then dumps out
712   // the macro table at the end.
713   PP.EnterMainSourceFile();
714 
715   Token Tok;
716   do PP.Lex(Tok);
717   while (Tok.isNot(tok::eof));
718 
719   SmallVector<id_macro_pair, 128> MacrosByID;
720   for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
721        I != E; ++I) {
722     auto *MD = I->second.getLatest();
723     if (MD && MD->isDefined())
724       MacrosByID.push_back(id_macro_pair(I->first, MD->getMacroInfo()));
725   }
726   llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare);
727 
728   for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
729     MacroInfo &MI = *MacrosByID[i].second;
730     // Ignore computed macros like __LINE__ and friends.
731     if (MI.isBuiltinMacro()) continue;
732 
733     PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
734     *OS << '\n';
735   }
736 }
737 
738 /// DoPrintPreprocessedInput - This implements -E mode.
739 ///
DoPrintPreprocessedInput(Preprocessor & PP,raw_ostream * OS,const PreprocessorOutputOptions & Opts)740 void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
741                                      const PreprocessorOutputOptions &Opts) {
742   // Show macros with no output is handled specially.
743   if (!Opts.ShowCPP) {
744     assert(Opts.ShowMacros && "Not yet implemented!");
745     DoPrintMacros(PP, OS);
746     return;
747   }
748 
749   // Inform the preprocessor whether we want it to retain comments or not, due
750   // to -C or -CC.
751   PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
752 
753   PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(
754       PP, *OS, !Opts.ShowLineMarkers, Opts.ShowMacros, Opts.UseLineDirectives);
755 
756   // Expand macros in pragmas with -fms-extensions.  The assumption is that
757   // the majority of pragmas in such a file will be Microsoft pragmas.
758   PP.AddPragmaHandler(new UnknownPragmaHandler(
759       "#pragma", Callbacks,
760       /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt));
761   PP.AddPragmaHandler(
762       "GCC", new UnknownPragmaHandler(
763                  "#pragma GCC", Callbacks,
764                  /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt));
765   PP.AddPragmaHandler(
766       "clang", new UnknownPragmaHandler(
767                    "#pragma clang", Callbacks,
768                    /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt));
769 
770   // The tokens after pragma omp need to be expanded.
771   //
772   //  OpenMP [2.1, Directive format]
773   //  Preprocessing tokens following the #pragma omp are subject to macro
774   //  replacement.
775   PP.AddPragmaHandler("omp",
776                       new UnknownPragmaHandler("#pragma omp", Callbacks,
777                                                /*RequireTokenExpansion=*/true));
778 
779   PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
780 
781   // After we have configured the preprocessor, enter the main file.
782   PP.EnterMainSourceFile();
783 
784   // Consume all of the tokens that come from the predefines buffer.  Those
785   // should not be emitted into the output and are guaranteed to be at the
786   // start.
787   const SourceManager &SourceMgr = PP.getSourceManager();
788   Token Tok;
789   do {
790     PP.Lex(Tok);
791     if (Tok.is(tok::eof) || !Tok.getLocation().isFileID())
792       break;
793 
794     PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
795     if (PLoc.isInvalid())
796       break;
797 
798     if (strcmp(PLoc.getFilename(), "<built-in>"))
799       break;
800   } while (true);
801 
802   // Read all the preprocessed tokens, printing them out to the stream.
803   PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
804   *OS << '\n';
805 }
806