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