1 //===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
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 // This code simply runs the preprocessor on the input file and prints out the
10 // result.  This is the traditional behavior of the -E option.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Frontend/Utils.h"
15 #include "clang/Basic/CharInfo.h"
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/Frontend/PreprocessorOutputOptions.h"
19 #include "clang/Lex/MacroInfo.h"
20 #include "clang/Lex/PPCallbacks.h"
21 #include "clang/Lex/Pragma.h"
22 #include "clang/Lex/Preprocessor.h"
23 #include "clang/Lex/TokenConcatenation.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include <cstdio>
30 using namespace clang;
31 
32 /// PrintMacroDefinition - Print a macro definition in a form that will be
33 /// properly accepted back as a definition.
34 static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI,
35                                  Preprocessor &PP, raw_ostream &OS) {
36   OS << "#define " << II.getName();
37 
38   if (MI.isFunctionLike()) {
39     OS << '(';
40     if (!MI.param_empty()) {
41       MacroInfo::param_iterator AI = MI.param_begin(), E = MI.param_end();
42       for (; AI+1 != E; ++AI) {
43         OS << (*AI)->getName();
44         OS << ',';
45       }
46 
47       // Last argument.
48       if ((*AI)->getName() == "__VA_ARGS__")
49         OS << "...";
50       else
51         OS << (*AI)->getName();
52     }
53 
54     if (MI.isGNUVarargs())
55       OS << "...";  // #define foo(x...)
56 
57     OS << ')';
58   }
59 
60   // GCC always emits a space, even if the macro body is empty.  However, do not
61   // want to emit two spaces if the first token has a leading space.
62   if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace())
63     OS << ' ';
64 
65   SmallString<128> SpellingBuffer;
66   for (const auto &T : MI.tokens()) {
67     if (T.hasLeadingSpace())
68       OS << ' ';
69 
70     OS << PP.getSpelling(T, SpellingBuffer);
71   }
72 }
73 
74 //===----------------------------------------------------------------------===//
75 // Preprocessed token printer
76 //===----------------------------------------------------------------------===//
77 
78 namespace {
79 class PrintPPOutputPPCallbacks : public PPCallbacks {
80   Preprocessor &PP;
81   SourceManager &SM;
82   TokenConcatenation ConcatInfo;
83 public:
84   raw_ostream &OS;
85 private:
86   unsigned CurLine;
87 
88   bool EmittedTokensOnThisLine;
89   bool EmittedDirectiveOnThisLine;
90   SrcMgr::CharacteristicKind FileType;
91   SmallString<512> CurFilename;
92   bool Initialized;
93   bool DisableLineMarkers;
94   bool DumpDefines;
95   bool DumpIncludeDirectives;
96   bool UseLineDirectives;
97   bool IsFirstFileEntered;
98   bool MinimizeWhitespace;
99 
100   Token PrevTok;
101   Token PrevPrevTok;
102 
103 public:
104   PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os, bool lineMarkers,
105                            bool defines, bool DumpIncludeDirectives,
106                            bool UseLineDirectives, bool MinimizeWhitespace)
107       : PP(pp), SM(PP.getSourceManager()), ConcatInfo(PP), OS(os),
108         DisableLineMarkers(lineMarkers), DumpDefines(defines),
109         DumpIncludeDirectives(DumpIncludeDirectives),
110         UseLineDirectives(UseLineDirectives),
111         MinimizeWhitespace(MinimizeWhitespace) {
112     CurLine = 0;
113     CurFilename += "<uninit>";
114     EmittedTokensOnThisLine = false;
115     EmittedDirectiveOnThisLine = false;
116     FileType = SrcMgr::C_User;
117     Initialized = false;
118     IsFirstFileEntered = false;
119 
120     PrevTok.startToken();
121     PrevPrevTok.startToken();
122   }
123 
124   bool isMinimizeWhitespace() const { return MinimizeWhitespace; }
125 
126   void setEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
127   bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
128 
129   void setEmittedDirectiveOnThisLine() { EmittedDirectiveOnThisLine = true; }
130   bool hasEmittedDirectiveOnThisLine() const {
131     return EmittedDirectiveOnThisLine;
132   }
133 
134   /// Ensure that the output stream position is at the beginning of a new line
135   /// and inserts one if it does not. It is intended to ensure that directives
136   /// inserted by the directives not from the input source (such as #line) are
137   /// in the first column. To insert newlines that represent the input, use
138   /// MoveToLine(/*...*/, /*RequireStartOfLine=*/true).
139   void startNewLineIfNeeded();
140 
141   void FileChanged(SourceLocation Loc, FileChangeReason Reason,
142                    SrcMgr::CharacteristicKind FileType,
143                    FileID PrevFID) override;
144   void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
145                           StringRef FileName, bool IsAngled,
146                           CharSourceRange FilenameRange, const FileEntry *File,
147                           StringRef SearchPath, StringRef RelativePath,
148                           const Module *Imported,
149                           SrcMgr::CharacteristicKind FileType) override;
150   void Ident(SourceLocation Loc, StringRef str) override;
151   void PragmaMessage(SourceLocation Loc, StringRef Namespace,
152                      PragmaMessageKind Kind, StringRef Str) override;
153   void PragmaDebug(SourceLocation Loc, StringRef DebugType) override;
154   void PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) override;
155   void PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) override;
156   void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
157                         diag::Severity Map, StringRef Str) override;
158   void PragmaWarning(SourceLocation Loc, PragmaWarningSpecifier WarningSpec,
159                      ArrayRef<int> Ids) override;
160   void PragmaWarningPush(SourceLocation Loc, int Level) override;
161   void PragmaWarningPop(SourceLocation Loc) override;
162   void PragmaExecCharsetPush(SourceLocation Loc, StringRef Str) override;
163   void PragmaExecCharsetPop(SourceLocation Loc) override;
164   void PragmaAssumeNonNullBegin(SourceLocation Loc) override;
165   void PragmaAssumeNonNullEnd(SourceLocation Loc) override;
166 
167   /// Insert whitespace before emitting the next token.
168   ///
169   /// @param Tok             Next token to be emitted.
170   /// @param RequireSpace    Ensure at least one whitespace is emitted. Useful
171   ///                        if non-tokens have been emitted to the stream.
172   /// @param RequireSameLine Never emit newlines. Useful when semantics depend
173   ///                        on being on the same line, such as directives.
174   void HandleWhitespaceBeforeTok(const Token &Tok, bool RequireSpace,
175                                  bool RequireSameLine);
176 
177   /// Move to the line of the provided source location. This will
178   /// return true if a newline was inserted or if
179   /// the requested location is the first token on the first line.
180   /// In these cases the next output will be the first column on the line and
181   /// make it possible to insert indention. The newline was inserted
182   /// implicitly when at the beginning of the file.
183   ///
184   /// @param Tok                 Token where to move to.
185   /// @param RequireStartOfLine  Whether the next line depends on being in the
186   ///                            first column, such as a directive.
187   ///
188   /// @return Whether column adjustments are necessary.
189   bool MoveToLine(const Token &Tok, bool RequireStartOfLine) {
190     PresumedLoc PLoc = SM.getPresumedLoc(Tok.getLocation());
191     unsigned TargetLine = PLoc.isValid() ? PLoc.getLine() : CurLine;
192     bool IsFirstInFile = Tok.isAtStartOfLine() && PLoc.getLine() == 1;
193     return MoveToLine(TargetLine, RequireStartOfLine) || IsFirstInFile;
194   }
195 
196   /// Move to the line of the provided source location. Returns true if a new
197   /// line was inserted.
198   bool MoveToLine(SourceLocation Loc, bool RequireStartOfLine) {
199     PresumedLoc PLoc = SM.getPresumedLoc(Loc);
200     unsigned TargetLine = PLoc.isValid() ? PLoc.getLine() : CurLine;
201     return MoveToLine(TargetLine, RequireStartOfLine);
202   }
203   bool MoveToLine(unsigned LineNo, bool RequireStartOfLine);
204 
205   bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok,
206                    const Token &Tok) {
207     return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok);
208   }
209   void WriteLineInfo(unsigned LineNo, const char *Extra=nullptr,
210                      unsigned ExtraLen=0);
211   bool LineMarkersAreDisabled() const { return DisableLineMarkers; }
212   void HandleNewlinesInToken(const char *TokStr, unsigned Len);
213 
214   /// MacroDefined - This hook is called whenever a macro definition is seen.
215   void MacroDefined(const Token &MacroNameTok,
216                     const MacroDirective *MD) override;
217 
218   /// MacroUndefined - This hook is called whenever a macro #undef is seen.
219   void MacroUndefined(const Token &MacroNameTok,
220                       const MacroDefinition &MD,
221                       const MacroDirective *Undef) override;
222 
223   void BeginModule(const Module *M);
224   void EndModule(const Module *M);
225 };
226 }  // end anonymous namespace
227 
228 void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
229                                              const char *Extra,
230                                              unsigned ExtraLen) {
231   startNewLineIfNeeded();
232 
233   // Emit #line directives or GNU line markers depending on what mode we're in.
234   if (UseLineDirectives) {
235     OS << "#line" << ' ' << LineNo << ' ' << '"';
236     OS.write_escaped(CurFilename);
237     OS << '"';
238   } else {
239     OS << '#' << ' ' << LineNo << ' ' << '"';
240     OS.write_escaped(CurFilename);
241     OS << '"';
242 
243     if (ExtraLen)
244       OS.write(Extra, ExtraLen);
245 
246     if (FileType == SrcMgr::C_System)
247       OS.write(" 3", 2);
248     else if (FileType == SrcMgr::C_ExternCSystem)
249       OS.write(" 3 4", 4);
250   }
251   OS << '\n';
252 }
253 
254 /// MoveToLine - Move the output to the source line specified by the location
255 /// object.  We can do this by emitting some number of \n's, or be emitting a
256 /// #line directive.  This returns false if already at the specified line, true
257 /// if some newlines were emitted.
258 bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo,
259                                           bool RequireStartOfLine) {
260   // If it is required to start a new line or finish the current, insert
261   // vertical whitespace now and take it into account when moving to the
262   // expected line.
263   bool StartedNewLine = false;
264   if ((RequireStartOfLine && EmittedTokensOnThisLine) ||
265       EmittedDirectiveOnThisLine) {
266     OS << '\n';
267     StartedNewLine = true;
268     CurLine += 1;
269     EmittedTokensOnThisLine = false;
270     EmittedDirectiveOnThisLine = false;
271   }
272 
273   // If this line is "close enough" to the original line, just print newlines,
274   // otherwise print a #line directive.
275   if (CurLine == LineNo) {
276     // Nothing to do if we are already on the correct line.
277   } else if (MinimizeWhitespace && DisableLineMarkers) {
278     // With -E -P -fminimize-whitespace, don't emit anything if not necessary.
279   } else if (!StartedNewLine && LineNo - CurLine == 1) {
280     // Printing a single line has priority over printing a #line directive, even
281     // when minimizing whitespace which otherwise would print #line directives
282     // for every single line.
283     OS << '\n';
284     StartedNewLine = true;
285   } else if (!DisableLineMarkers) {
286     if (LineNo - CurLine <= 8) {
287       const char *NewLines = "\n\n\n\n\n\n\n\n";
288       OS.write(NewLines, LineNo - CurLine);
289     } else {
290       // Emit a #line or line marker.
291       WriteLineInfo(LineNo, nullptr, 0);
292     }
293     StartedNewLine = true;
294   } else if (EmittedTokensOnThisLine) {
295     // If we are not on the correct line and don't need to be line-correct,
296     // at least ensure we start on a new line.
297     OS << '\n';
298     StartedNewLine = true;
299   }
300 
301   if (StartedNewLine) {
302     EmittedTokensOnThisLine = false;
303     EmittedDirectiveOnThisLine = false;
304   }
305 
306   CurLine = LineNo;
307   return StartedNewLine;
308 }
309 
310 void PrintPPOutputPPCallbacks::startNewLineIfNeeded() {
311   if (EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) {
312     OS << '\n';
313     EmittedTokensOnThisLine = false;
314     EmittedDirectiveOnThisLine = false;
315   }
316 }
317 
318 /// FileChanged - Whenever the preprocessor enters or exits a #include file
319 /// it invokes this handler.  Update our conception of the current source
320 /// position.
321 void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
322                                            FileChangeReason Reason,
323                                        SrcMgr::CharacteristicKind NewFileType,
324                                        FileID PrevFID) {
325   // Unless we are exiting a #include, make sure to skip ahead to the line the
326   // #include directive was at.
327   SourceManager &SourceMgr = SM;
328 
329   PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc);
330   if (UserLoc.isInvalid())
331     return;
332 
333   unsigned NewLine = UserLoc.getLine();
334 
335   if (Reason == PPCallbacks::EnterFile) {
336     SourceLocation IncludeLoc = UserLoc.getIncludeLoc();
337     if (IncludeLoc.isValid())
338       MoveToLine(IncludeLoc, /*RequireStartOfLine=*/false);
339   } else if (Reason == PPCallbacks::SystemHeaderPragma) {
340     // GCC emits the # directive for this directive on the line AFTER the
341     // directive and emits a bunch of spaces that aren't needed. This is because
342     // otherwise we will emit a line marker for THIS line, which requires an
343     // extra blank line after the directive to avoid making all following lines
344     // off by one. We can do better by simply incrementing NewLine here.
345     NewLine += 1;
346   }
347 
348   CurLine = NewLine;
349 
350   CurFilename.clear();
351   CurFilename += UserLoc.getFilename();
352   FileType = NewFileType;
353 
354   if (DisableLineMarkers) {
355     if (!MinimizeWhitespace)
356       startNewLineIfNeeded();
357     return;
358   }
359 
360   if (!Initialized) {
361     WriteLineInfo(CurLine);
362     Initialized = true;
363   }
364 
365   // Do not emit an enter marker for the main file (which we expect is the first
366   // entered file). This matches gcc, and improves compatibility with some tools
367   // which track the # line markers as a way to determine when the preprocessed
368   // output is in the context of the main file.
369   if (Reason == PPCallbacks::EnterFile && !IsFirstFileEntered) {
370     IsFirstFileEntered = true;
371     return;
372   }
373 
374   switch (Reason) {
375   case PPCallbacks::EnterFile:
376     WriteLineInfo(CurLine, " 1", 2);
377     break;
378   case PPCallbacks::ExitFile:
379     WriteLineInfo(CurLine, " 2", 2);
380     break;
381   case PPCallbacks::SystemHeaderPragma:
382   case PPCallbacks::RenameFile:
383     WriteLineInfo(CurLine);
384     break;
385   }
386 }
387 
388 void PrintPPOutputPPCallbacks::InclusionDirective(
389     SourceLocation HashLoc,
390     const Token &IncludeTok,
391     StringRef FileName,
392     bool IsAngled,
393     CharSourceRange FilenameRange,
394     const FileEntry *File,
395     StringRef SearchPath,
396     StringRef RelativePath,
397     const Module *Imported,
398     SrcMgr::CharacteristicKind FileType) {
399   // In -dI mode, dump #include directives prior to dumping their content or
400   // interpretation.
401   if (DumpIncludeDirectives) {
402     MoveToLine(HashLoc, /*RequireStartOfLine=*/true);
403     const std::string TokenText = PP.getSpelling(IncludeTok);
404     assert(!TokenText.empty());
405     OS << "#" << TokenText << " "
406        << (IsAngled ? '<' : '"') << FileName << (IsAngled ? '>' : '"')
407        << " /* clang -E -dI */";
408     setEmittedDirectiveOnThisLine();
409   }
410 
411   // When preprocessing, turn implicit imports into module import pragmas.
412   if (Imported) {
413     switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
414     case tok::pp_include:
415     case tok::pp_import:
416     case tok::pp_include_next:
417       MoveToLine(HashLoc, /*RequireStartOfLine=*/true);
418       OS << "#pragma clang module import " << Imported->getFullModuleName(true)
419          << " /* clang -E: implicit import for "
420          << "#" << PP.getSpelling(IncludeTok) << " "
421          << (IsAngled ? '<' : '"') << FileName << (IsAngled ? '>' : '"')
422          << " */";
423       setEmittedDirectiveOnThisLine();
424       break;
425 
426     case tok::pp___include_macros:
427       // #__include_macros has no effect on a user of a preprocessed source
428       // file; the only effect is on preprocessing.
429       //
430       // FIXME: That's not *quite* true: it causes the module in question to
431       // be loaded, which can affect downstream diagnostics.
432       break;
433 
434     default:
435       llvm_unreachable("unknown include directive kind");
436       break;
437     }
438   }
439 }
440 
441 /// Handle entering the scope of a module during a module compilation.
442 void PrintPPOutputPPCallbacks::BeginModule(const Module *M) {
443   startNewLineIfNeeded();
444   OS << "#pragma clang module begin " << M->getFullModuleName(true);
445   setEmittedDirectiveOnThisLine();
446 }
447 
448 /// Handle leaving the scope of a module during a module compilation.
449 void PrintPPOutputPPCallbacks::EndModule(const Module *M) {
450   startNewLineIfNeeded();
451   OS << "#pragma clang module end /*" << M->getFullModuleName(true) << "*/";
452   setEmittedDirectiveOnThisLine();
453 }
454 
455 /// Ident - Handle #ident directives when read by the preprocessor.
456 ///
457 void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, StringRef S) {
458   MoveToLine(Loc, /*RequireStartOfLine=*/true);
459 
460   OS.write("#ident ", strlen("#ident "));
461   OS.write(S.begin(), S.size());
462   setEmittedTokensOnThisLine();
463 }
464 
465 /// MacroDefined - This hook is called whenever a macro definition is seen.
466 void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok,
467                                             const MacroDirective *MD) {
468   const MacroInfo *MI = MD->getMacroInfo();
469   // Only print out macro definitions in -dD mode.
470   if (!DumpDefines ||
471       // Ignore __FILE__ etc.
472       MI->isBuiltinMacro()) return;
473 
474   MoveToLine(MI->getDefinitionLoc(), /*RequireStartOfLine=*/true);
475   PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS);
476   setEmittedDirectiveOnThisLine();
477 }
478 
479 void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok,
480                                               const MacroDefinition &MD,
481                                               const MacroDirective *Undef) {
482   // Only print out macro definitions in -dD mode.
483   if (!DumpDefines) return;
484 
485   MoveToLine(MacroNameTok.getLocation(), /*RequireStartOfLine=*/true);
486   OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName();
487   setEmittedDirectiveOnThisLine();
488 }
489 
490 static void outputPrintable(raw_ostream &OS, StringRef Str) {
491   for (unsigned char Char : Str) {
492     if (isPrintable(Char) && Char != '\\' && Char != '"')
493       OS << (char)Char;
494     else // Output anything hard as an octal escape.
495       OS << '\\'
496          << (char)('0' + ((Char >> 6) & 7))
497          << (char)('0' + ((Char >> 3) & 7))
498          << (char)('0' + ((Char >> 0) & 7));
499   }
500 }
501 
502 void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc,
503                                              StringRef Namespace,
504                                              PragmaMessageKind Kind,
505                                              StringRef Str) {
506   MoveToLine(Loc, /*RequireStartOfLine=*/true);
507   OS << "#pragma ";
508   if (!Namespace.empty())
509     OS << Namespace << ' ';
510   switch (Kind) {
511     case PMK_Message:
512       OS << "message(\"";
513       break;
514     case PMK_Warning:
515       OS << "warning \"";
516       break;
517     case PMK_Error:
518       OS << "error \"";
519       break;
520   }
521 
522   outputPrintable(OS, Str);
523   OS << '"';
524   if (Kind == PMK_Message)
525     OS << ')';
526   setEmittedDirectiveOnThisLine();
527 }
528 
529 void PrintPPOutputPPCallbacks::PragmaDebug(SourceLocation Loc,
530                                            StringRef DebugType) {
531   MoveToLine(Loc, /*RequireStartOfLine=*/true);
532 
533   OS << "#pragma clang __debug ";
534   OS << DebugType;
535 
536   setEmittedDirectiveOnThisLine();
537 }
538 
539 void PrintPPOutputPPCallbacks::
540 PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) {
541   MoveToLine(Loc, /*RequireStartOfLine=*/true);
542   OS << "#pragma " << Namespace << " diagnostic push";
543   setEmittedDirectiveOnThisLine();
544 }
545 
546 void PrintPPOutputPPCallbacks::
547 PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) {
548   MoveToLine(Loc, /*RequireStartOfLine=*/true);
549   OS << "#pragma " << Namespace << " diagnostic pop";
550   setEmittedDirectiveOnThisLine();
551 }
552 
553 void PrintPPOutputPPCallbacks::PragmaDiagnostic(SourceLocation Loc,
554                                                 StringRef Namespace,
555                                                 diag::Severity Map,
556                                                 StringRef Str) {
557   MoveToLine(Loc, /*RequireStartOfLine=*/true);
558   OS << "#pragma " << Namespace << " diagnostic ";
559   switch (Map) {
560   case diag::Severity::Remark:
561     OS << "remark";
562     break;
563   case diag::Severity::Warning:
564     OS << "warning";
565     break;
566   case diag::Severity::Error:
567     OS << "error";
568     break;
569   case diag::Severity::Ignored:
570     OS << "ignored";
571     break;
572   case diag::Severity::Fatal:
573     OS << "fatal";
574     break;
575   }
576   OS << " \"" << Str << '"';
577   setEmittedDirectiveOnThisLine();
578 }
579 
580 void PrintPPOutputPPCallbacks::PragmaWarning(SourceLocation Loc,
581                                              PragmaWarningSpecifier WarningSpec,
582                                              ArrayRef<int> Ids) {
583   MoveToLine(Loc, /*RequireStartOfLine=*/true);
584 
585   OS << "#pragma warning(";
586   switch(WarningSpec) {
587     case PWS_Default:  OS << "default"; break;
588     case PWS_Disable:  OS << "disable"; break;
589     case PWS_Error:    OS << "error"; break;
590     case PWS_Once:     OS << "once"; break;
591     case PWS_Suppress: OS << "suppress"; break;
592     case PWS_Level1:   OS << '1'; break;
593     case PWS_Level2:   OS << '2'; break;
594     case PWS_Level3:   OS << '3'; break;
595     case PWS_Level4:   OS << '4'; break;
596   }
597   OS << ':';
598 
599   for (ArrayRef<int>::iterator I = Ids.begin(), E = Ids.end(); I != E; ++I)
600     OS << ' ' << *I;
601   OS << ')';
602   setEmittedDirectiveOnThisLine();
603 }
604 
605 void PrintPPOutputPPCallbacks::PragmaWarningPush(SourceLocation Loc,
606                                                  int Level) {
607   MoveToLine(Loc, /*RequireStartOfLine=*/true);
608   OS << "#pragma warning(push";
609   if (Level >= 0)
610     OS << ", " << Level;
611   OS << ')';
612   setEmittedDirectiveOnThisLine();
613 }
614 
615 void PrintPPOutputPPCallbacks::PragmaWarningPop(SourceLocation Loc) {
616   MoveToLine(Loc, /*RequireStartOfLine=*/true);
617   OS << "#pragma warning(pop)";
618   setEmittedDirectiveOnThisLine();
619 }
620 
621 void PrintPPOutputPPCallbacks::PragmaExecCharsetPush(SourceLocation Loc,
622                                                      StringRef Str) {
623   MoveToLine(Loc, /*RequireStartOfLine=*/true);
624   OS << "#pragma character_execution_set(push";
625   if (!Str.empty())
626     OS << ", " << Str;
627   OS << ')';
628   setEmittedDirectiveOnThisLine();
629 }
630 
631 void PrintPPOutputPPCallbacks::PragmaExecCharsetPop(SourceLocation Loc) {
632   MoveToLine(Loc, /*RequireStartOfLine=*/true);
633   OS << "#pragma character_execution_set(pop)";
634   setEmittedDirectiveOnThisLine();
635 }
636 
637 void PrintPPOutputPPCallbacks::
638 PragmaAssumeNonNullBegin(SourceLocation Loc) {
639   MoveToLine(Loc, /*RequireStartOfLine=*/true);
640   OS << "#pragma clang assume_nonnull begin";
641   setEmittedDirectiveOnThisLine();
642 }
643 
644 void PrintPPOutputPPCallbacks::
645 PragmaAssumeNonNullEnd(SourceLocation Loc) {
646   MoveToLine(Loc, /*RequireStartOfLine=*/true);
647   OS << "#pragma clang assume_nonnull end";
648   setEmittedDirectiveOnThisLine();
649 }
650 
651 void PrintPPOutputPPCallbacks::HandleWhitespaceBeforeTok(const Token &Tok,
652                                                          bool RequireSpace,
653                                                          bool RequireSameLine) {
654   // These tokens are not expanded to anything and don't need whitespace before
655   // them.
656   if (Tok.is(tok::eof) ||
657       (Tok.isAnnotation() && !Tok.is(tok::annot_header_unit) &&
658        !Tok.is(tok::annot_module_begin) && !Tok.is(tok::annot_module_end)))
659     return;
660 
661   // EmittedDirectiveOnThisLine takes priority over RequireSameLine.
662   if ((!RequireSameLine || EmittedDirectiveOnThisLine) &&
663       MoveToLine(Tok, /*RequireStartOfLine=*/EmittedDirectiveOnThisLine)) {
664     if (MinimizeWhitespace) {
665       // Avoid interpreting hash as a directive under -fpreprocessed.
666       if (Tok.is(tok::hash))
667         OS << ' ';
668     } else {
669       // Print out space characters so that the first token on a line is
670       // indented for easy reading.
671       unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation());
672 
673       // The first token on a line can have a column number of 1, yet still
674       // expect leading white space, if a macro expansion in column 1 starts
675       // with an empty macro argument, or an empty nested macro expansion. In
676       // this case, move the token to column 2.
677       if (ColNo == 1 && Tok.hasLeadingSpace())
678         ColNo = 2;
679 
680       // This hack prevents stuff like:
681       // #define HASH #
682       // HASH define foo bar
683       // From having the # character end up at column 1, which makes it so it
684       // is not handled as a #define next time through the preprocessor if in
685       // -fpreprocessed mode.
686       if (ColNo <= 1 && Tok.is(tok::hash))
687         OS << ' ';
688 
689       // Otherwise, indent the appropriate number of spaces.
690       for (; ColNo > 1; --ColNo)
691         OS << ' ';
692     }
693   } else {
694     // Insert whitespace between the previous and next token if either
695     // - The caller requires it
696     // - The input had whitespace between them and we are not in
697     //   whitespace-minimization mode
698     // - The whitespace is necessary to keep the tokens apart and there is not
699     //   already a newline between them
700     if (RequireSpace || (!MinimizeWhitespace && Tok.hasLeadingSpace()) ||
701         ((EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) &&
702          AvoidConcat(PrevPrevTok, PrevTok, Tok)))
703       OS << ' ';
704   }
705 
706   PrevPrevTok = PrevTok;
707   PrevTok = Tok;
708 }
709 
710 void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
711                                                      unsigned Len) {
712   unsigned NumNewlines = 0;
713   for (; Len; --Len, ++TokStr) {
714     if (*TokStr != '\n' &&
715         *TokStr != '\r')
716       continue;
717 
718     ++NumNewlines;
719 
720     // If we have \n\r or \r\n, skip both and count as one line.
721     if (Len != 1 &&
722         (TokStr[1] == '\n' || TokStr[1] == '\r') &&
723         TokStr[0] != TokStr[1]) {
724       ++TokStr;
725       --Len;
726     }
727   }
728 
729   if (NumNewlines == 0) return;
730 
731   CurLine += NumNewlines;
732 }
733 
734 
735 namespace {
736 struct UnknownPragmaHandler : public PragmaHandler {
737   const char *Prefix;
738   PrintPPOutputPPCallbacks *Callbacks;
739 
740   // Set to true if tokens should be expanded
741   bool ShouldExpandTokens;
742 
743   UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks,
744                        bool RequireTokenExpansion)
745       : Prefix(prefix), Callbacks(callbacks),
746         ShouldExpandTokens(RequireTokenExpansion) {}
747   void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
748                     Token &PragmaTok) override {
749     // Figure out what line we went to and insert the appropriate number of
750     // newline characters.
751     Callbacks->MoveToLine(PragmaTok.getLocation(), /*RequireStartOfLine=*/true);
752     Callbacks->OS.write(Prefix, strlen(Prefix));
753     Callbacks->setEmittedTokensOnThisLine();
754 
755     if (ShouldExpandTokens) {
756       // The first token does not have expanded macros. Expand them, if
757       // required.
758       auto Toks = std::make_unique<Token[]>(1);
759       Toks[0] = PragmaTok;
760       PP.EnterTokenStream(std::move(Toks), /*NumToks=*/1,
761                           /*DisableMacroExpansion=*/false,
762                           /*IsReinject=*/false);
763       PP.Lex(PragmaTok);
764     }
765 
766     // Read and print all of the pragma tokens.
767     bool IsFirst = true;
768     while (PragmaTok.isNot(tok::eod)) {
769       Callbacks->HandleWhitespaceBeforeTok(PragmaTok, /*RequireSpace=*/IsFirst,
770                                            /*RequireSameLine=*/true);
771       IsFirst = false;
772       std::string TokSpell = PP.getSpelling(PragmaTok);
773       Callbacks->OS.write(&TokSpell[0], TokSpell.size());
774       Callbacks->setEmittedTokensOnThisLine();
775 
776       if (ShouldExpandTokens)
777         PP.Lex(PragmaTok);
778       else
779         PP.LexUnexpandedToken(PragmaTok);
780     }
781     Callbacks->setEmittedDirectiveOnThisLine();
782   }
783 };
784 } // end anonymous namespace
785 
786 
787 static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
788                                     PrintPPOutputPPCallbacks *Callbacks,
789                                     raw_ostream &OS) {
790   bool DropComments = PP.getLangOpts().TraditionalCPP &&
791                       !PP.getCommentRetentionState();
792 
793   bool IsStartOfLine = false;
794   char Buffer[256];
795   while (true) {
796     // Two lines joined with line continuation ('\' as last character on the
797     // line) must be emitted as one line even though Tok.getLine() returns two
798     // different values. In this situation Tok.isAtStartOfLine() is false even
799     // though it may be the first token on the lexical line. When
800     // dropping/skipping a token that is at the start of a line, propagate the
801     // start-of-line-ness to the next token to not append it to the previous
802     // line.
803     IsStartOfLine = IsStartOfLine || Tok.isAtStartOfLine();
804 
805     Callbacks->HandleWhitespaceBeforeTok(Tok, /*RequireSpace=*/false,
806                                          /*RequireSameLine=*/!IsStartOfLine);
807 
808     if (DropComments && Tok.is(tok::comment)) {
809       // Skip comments. Normally the preprocessor does not generate
810       // tok::comment nodes at all when not keeping comments, but under
811       // -traditional-cpp the lexer keeps /all/ whitespace, including comments.
812       PP.Lex(Tok);
813       continue;
814     } else if (Tok.is(tok::eod)) {
815       // Don't print end of directive tokens, since they are typically newlines
816       // that mess up our line tracking. These come from unknown pre-processor
817       // directives or hash-prefixed comments in standalone assembly files.
818       PP.Lex(Tok);
819       // FIXME: The token on the next line after #include should have
820       // Tok.isAtStartOfLine() set.
821       IsStartOfLine = true;
822       continue;
823     } else if (Tok.is(tok::annot_module_include)) {
824       // PrintPPOutputPPCallbacks::InclusionDirective handles producing
825       // appropriate output here. Ignore this token entirely.
826       PP.Lex(Tok);
827       IsStartOfLine = true;
828       continue;
829     } else if (Tok.is(tok::annot_module_begin)) {
830       // FIXME: We retrieve this token after the FileChanged callback, and
831       // retrieve the module_end token before the FileChanged callback, so
832       // we render this within the file and render the module end outside the
833       // file, but this is backwards from the token locations: the module_begin
834       // token is at the include location (outside the file) and the module_end
835       // token is at the EOF location (within the file).
836       Callbacks->BeginModule(
837           reinterpret_cast<Module *>(Tok.getAnnotationValue()));
838       PP.Lex(Tok);
839       IsStartOfLine = true;
840       continue;
841     } else if (Tok.is(tok::annot_module_end)) {
842       Callbacks->EndModule(
843           reinterpret_cast<Module *>(Tok.getAnnotationValue()));
844       PP.Lex(Tok);
845       IsStartOfLine = true;
846       continue;
847     } else if (Tok.is(tok::annot_header_unit)) {
848       // This is a header-name that has been (effectively) converted into a
849       // module-name.
850       // FIXME: The module name could contain non-identifier module name
851       // components. We don't have a good way to round-trip those.
852       Module *M = reinterpret_cast<Module *>(Tok.getAnnotationValue());
853       std::string Name = M->getFullModuleName();
854       OS.write(Name.data(), Name.size());
855       Callbacks->HandleNewlinesInToken(Name.data(), Name.size());
856     } else if (Tok.isAnnotation()) {
857       // Ignore annotation tokens created by pragmas - the pragmas themselves
858       // will be reproduced in the preprocessed output.
859       PP.Lex(Tok);
860       continue;
861     } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
862       OS << II->getName();
863     } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
864                Tok.getLiteralData()) {
865       OS.write(Tok.getLiteralData(), Tok.getLength());
866     } else if (Tok.getLength() < llvm::array_lengthof(Buffer)) {
867       const char *TokPtr = Buffer;
868       unsigned Len = PP.getSpelling(Tok, TokPtr);
869       OS.write(TokPtr, Len);
870 
871       // Tokens that can contain embedded newlines need to adjust our current
872       // line number.
873       // FIXME: The token may end with a newline in which case
874       // setEmittedDirectiveOnThisLine/setEmittedTokensOnThisLine afterwards is
875       // wrong.
876       if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
877         Callbacks->HandleNewlinesInToken(TokPtr, Len);
878       if (Tok.is(tok::comment) && Len >= 2 && TokPtr[0] == '/' &&
879           TokPtr[1] == '/') {
880         // It's a line comment;
881         // Ensure that we don't concatenate anything behind it.
882         Callbacks->setEmittedDirectiveOnThisLine();
883       }
884     } else {
885       std::string S = PP.getSpelling(Tok);
886       OS.write(S.data(), S.size());
887 
888       // Tokens that can contain embedded newlines need to adjust our current
889       // line number.
890       if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
891         Callbacks->HandleNewlinesInToken(S.data(), S.size());
892       if (Tok.is(tok::comment) && S.size() >= 2 && S[0] == '/' && S[1] == '/') {
893         // It's a line comment;
894         // Ensure that we don't concatenate anything behind it.
895         Callbacks->setEmittedDirectiveOnThisLine();
896       }
897     }
898     Callbacks->setEmittedTokensOnThisLine();
899     IsStartOfLine = false;
900 
901     if (Tok.is(tok::eof)) break;
902 
903     PP.Lex(Tok);
904   }
905 }
906 
907 typedef std::pair<const IdentifierInfo *, MacroInfo *> id_macro_pair;
908 static int MacroIDCompare(const id_macro_pair *LHS, const id_macro_pair *RHS) {
909   return LHS->first->getName().compare(RHS->first->getName());
910 }
911 
912 static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) {
913   // Ignore unknown pragmas.
914   PP.IgnorePragmas();
915 
916   // -dM mode just scans and ignores all tokens in the files, then dumps out
917   // the macro table at the end.
918   PP.EnterMainSourceFile();
919 
920   Token Tok;
921   do PP.Lex(Tok);
922   while (Tok.isNot(tok::eof));
923 
924   SmallVector<id_macro_pair, 128> MacrosByID;
925   for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
926        I != E; ++I) {
927     auto *MD = I->second.getLatest();
928     if (MD && MD->isDefined())
929       MacrosByID.push_back(id_macro_pair(I->first, MD->getMacroInfo()));
930   }
931   llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare);
932 
933   for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
934     MacroInfo &MI = *MacrosByID[i].second;
935     // Ignore computed macros like __LINE__ and friends.
936     if (MI.isBuiltinMacro()) continue;
937 
938     PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
939     *OS << '\n';
940   }
941 }
942 
943 /// DoPrintPreprocessedInput - This implements -E mode.
944 ///
945 void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
946                                      const PreprocessorOutputOptions &Opts) {
947   // Show macros with no output is handled specially.
948   if (!Opts.ShowCPP) {
949     assert(Opts.ShowMacros && "Not yet implemented!");
950     DoPrintMacros(PP, OS);
951     return;
952   }
953 
954   // Inform the preprocessor whether we want it to retain comments or not, due
955   // to -C or -CC.
956   PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
957 
958   PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(
959       PP, *OS, !Opts.ShowLineMarkers, Opts.ShowMacros,
960       Opts.ShowIncludeDirectives, Opts.UseLineDirectives,
961       Opts.MinimizeWhitespace);
962 
963   // Expand macros in pragmas with -fms-extensions.  The assumption is that
964   // the majority of pragmas in such a file will be Microsoft pragmas.
965   // Remember the handlers we will add so that we can remove them later.
966   std::unique_ptr<UnknownPragmaHandler> MicrosoftExtHandler(
967       new UnknownPragmaHandler(
968           "#pragma", Callbacks,
969           /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt));
970 
971   std::unique_ptr<UnknownPragmaHandler> GCCHandler(new UnknownPragmaHandler(
972       "#pragma GCC", Callbacks,
973       /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt));
974 
975   std::unique_ptr<UnknownPragmaHandler> ClangHandler(new UnknownPragmaHandler(
976       "#pragma clang", Callbacks,
977       /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt));
978 
979   PP.AddPragmaHandler(MicrosoftExtHandler.get());
980   PP.AddPragmaHandler("GCC", GCCHandler.get());
981   PP.AddPragmaHandler("clang", ClangHandler.get());
982 
983   // The tokens after pragma omp need to be expanded.
984   //
985   //  OpenMP [2.1, Directive format]
986   //  Preprocessing tokens following the #pragma omp are subject to macro
987   //  replacement.
988   std::unique_ptr<UnknownPragmaHandler> OpenMPHandler(
989       new UnknownPragmaHandler("#pragma omp", Callbacks,
990                                /*RequireTokenExpansion=*/true));
991   PP.AddPragmaHandler("omp", OpenMPHandler.get());
992 
993   PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
994 
995   // After we have configured the preprocessor, enter the main file.
996   PP.EnterMainSourceFile();
997 
998   // Consume all of the tokens that come from the predefines buffer.  Those
999   // should not be emitted into the output and are guaranteed to be at the
1000   // start.
1001   const SourceManager &SourceMgr = PP.getSourceManager();
1002   Token Tok;
1003   do {
1004     PP.Lex(Tok);
1005     if (Tok.is(tok::eof) || !Tok.getLocation().isFileID())
1006       break;
1007 
1008     PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
1009     if (PLoc.isInvalid())
1010       break;
1011 
1012     if (strcmp(PLoc.getFilename(), "<built-in>"))
1013       break;
1014   } while (true);
1015 
1016   // Read all the preprocessed tokens, printing them out to the stream.
1017   PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
1018   *OS << '\n';
1019 
1020   // Remove the handlers we just added to leave the preprocessor in a sane state
1021   // so that it can be reused (for example by a clang::Parser instance).
1022   PP.RemovePragmaHandler(MicrosoftExtHandler.get());
1023   PP.RemovePragmaHandler("GCC", GCCHandler.get());
1024   PP.RemovePragmaHandler("clang", ClangHandler.get());
1025   PP.RemovePragmaHandler("omp", OpenMPHandler.get());
1026 }
1027