1 //===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
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"
31 using namespace clang;
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();
39 if (MI.isFunctionLike()) {
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();
49 if ((*AI)->getName() == "__VA_ARGS__")
52 OS << (*AI)->getName();
55 if (MI.isGNUVarargs())
56 OS << "..."; // #define foo(x...)
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())
66 SmallString<128> SpellingBuffer;
67 for (const auto &T : MI.tokens()) {
68 if (T.hasLeadingSpace())
71 OS << PP.getSpelling(T, SpellingBuffer);
75 //===----------------------------------------------------------------------===//
76 // Preprocessed token printer
77 //===----------------------------------------------------------------------===//
80 class PrintPPOutputPPCallbacks : public PPCallbacks {
83 TokenConcatenation ConcatInfo;
89 bool EmittedTokensOnThisLine;
90 bool EmittedDirectiveOnThisLine;
91 SrcMgr::CharacteristicKind FileType;
92 SmallString<512> CurFilename;
94 bool DisableLineMarkers;
96 bool DumpIncludeDirectives;
97 bool UseLineDirectives;
98 bool IsFirstFileEntered;
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) {
108 CurFilename += "<uninit>";
109 EmittedTokensOnThisLine = false;
110 EmittedDirectiveOnThisLine = false;
111 FileType = SrcMgr::C_User;
113 IsFirstFileEntered = false;
116 void setEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
117 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
119 void setEmittedDirectiveOnThisLine() { EmittedDirectiveOnThisLine = true; }
120 bool hasEmittedDirectiveOnThisLine() const {
121 return EmittedDirectiveOnThisLine;
124 bool startNewLineIfNeeded(bool ShouldUpdateCurrentLine = true);
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;
147 bool HandleFirstTokOnLine(Token &Tok);
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())
156 return MoveToLine(PLoc.getLine()) || (PLoc.getLine() == 1);
158 bool MoveToLine(unsigned LineNo);
160 bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok,
162 return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok);
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);
169 /// MacroDefined - This hook is called whenever a macro definition is seen.
170 void MacroDefined(const Token &MacroNameTok,
171 const MacroDirective *MD) override;
173 /// MacroUndefined - This hook is called whenever a macro #undef is seen.
174 void MacroUndefined(const Token &MacroNameTok,
175 const MacroDefinition &MD) override;
177 } // end anonymous namespace
179 void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
182 startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
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);
190 OS << '#' << ' ' << LineNo << ' ' << '"';
191 OS.write_escaped(CurFilename);
195 OS.write(Extra, ExtraLen);
197 if (FileType == SrcMgr::C_System)
199 else if (FileType == SrcMgr::C_ExternCSystem)
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)
215 else if (LineNo == CurLine)
216 return false; // Spelling line moved, but expansion line didn't.
218 const char *NewLines = "\n\n\n\n\n\n\n\n";
219 OS.write(NewLines, LineNo-CurLine);
221 } else if (!DisableLineMarkers) {
222 // Emit a #line or line marker.
223 WriteLineInfo(LineNo, nullptr, 0);
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);
235 PrintPPOutputPPCallbacks::startNewLineIfNeeded(bool ShouldUpdateCurrentLine) {
236 if (EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) {
238 EmittedTokensOnThisLine = false;
239 EmittedDirectiveOnThisLine = false;
240 if (ShouldUpdateCurrentLine)
248 /// FileChanged - Whenever the preprocessor enters or exits a #include file
249 /// it invokes this handler. Update our conception of the current source
251 void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
252 FileChangeReason Reason,
253 SrcMgr::CharacteristicKind NewFileType,
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;
259 PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc);
260 if (UserLoc.isInvalid())
263 unsigned NewLine = UserLoc.getLine();
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.
281 CurFilename += UserLoc.getFilename();
282 FileType = NewFileType;
284 if (DisableLineMarkers) {
285 startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
290 WriteLineInfo(CurLine);
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;
304 case PPCallbacks::EnterFile:
305 WriteLineInfo(CurLine, " 1", 2);
307 case PPCallbacks::ExitFile:
308 WriteLineInfo(CurLine, " 2", 2);
310 case PPCallbacks::SystemHeaderPragma:
311 case PPCallbacks::RenameFile:
312 WriteLineInfo(CurLine);
317 void PrintPPOutputPPCallbacks::InclusionDirective(SourceLocation HashLoc,
318 const Token &IncludeTok,
321 CharSourceRange FilenameRange,
322 const FileEntry *File,
323 StringRef SearchPath,
324 StringRef RelativePath,
325 const Module *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();
332 if (PP.getLangOpts().ObjC2) {
333 OS << "@import " << Imported->getFullModuleName() << ";"
334 << " /* clang -E: implicit import for \"" << File->getName()
337 const std::string TokenText = PP.getSpelling(IncludeTok);
338 assert(!TokenText.empty());
339 OS << "#" << TokenText << " "
340 << (IsAngled ? '<' : '"')
342 << (IsAngled ? '>' : '"')
343 << " /* clang -E: implicit import for module "
344 << Imported->getFullModuleName() << " */";
346 // Since we want a newline after the @import, but not a #<line>, start a new
348 EmittedTokensOnThisLine = true;
349 startNewLineIfNeeded();
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();
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();
367 /// Ident - Handle #ident directives when read by the preprocessor.
369 void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, StringRef S) {
372 OS.write("#ident ", strlen("#ident "));
373 OS.write(S.begin(), S.size());
374 EmittedTokensOnThisLine = true;
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.
383 // Ignore __FILE__ etc.
384 MI->isBuiltinMacro()) return;
386 MoveToLine(MI->getDefinitionLoc());
387 PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS);
388 setEmittedDirectiveOnThisLine();
391 void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok,
392 const MacroDefinition &MD) {
393 // Only print out macro definitions in -dD mode.
394 if (!DumpDefines) return;
396 MoveToLine(MacroNameTok.getLocation());
397 OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName();
398 setEmittedDirectiveOnThisLine();
401 static void outputPrintable(raw_ostream &OS, StringRef Str) {
402 for (unsigned char Char : Str) {
403 if (isPrintable(Char) && Char != '\\' && Char != '"')
405 else // Output anything hard as an octal escape.
407 << (char)('0' + ((Char >> 6) & 7))
408 << (char)('0' + ((Char >> 3) & 7))
409 << (char)('0' + ((Char >> 0) & 7));
413 void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc,
415 PragmaMessageKind Kind,
417 startNewLineIfNeeded();
420 if (!Namespace.empty())
421 OS << Namespace << ' ';
434 outputPrintable(OS, Str);
436 if (Kind == PMK_Message)
438 setEmittedDirectiveOnThisLine();
441 void PrintPPOutputPPCallbacks::PragmaDebug(SourceLocation Loc,
442 StringRef DebugType) {
443 startNewLineIfNeeded();
446 OS << "#pragma clang __debug ";
449 setEmittedDirectiveOnThisLine();
452 void PrintPPOutputPPCallbacks::
453 PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) {
454 startNewLineIfNeeded();
456 OS << "#pragma " << Namespace << " diagnostic push";
457 setEmittedDirectiveOnThisLine();
460 void PrintPPOutputPPCallbacks::
461 PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) {
462 startNewLineIfNeeded();
464 OS << "#pragma " << Namespace << " diagnostic pop";
465 setEmittedDirectiveOnThisLine();
468 void PrintPPOutputPPCallbacks::PragmaDiagnostic(SourceLocation Loc,
472 startNewLineIfNeeded();
474 OS << "#pragma " << Namespace << " diagnostic ";
476 case diag::Severity::Remark:
479 case diag::Severity::Warning:
482 case diag::Severity::Error:
485 case diag::Severity::Ignored:
488 case diag::Severity::Fatal:
492 OS << " \"" << Str << '"';
493 setEmittedDirectiveOnThisLine();
496 void PrintPPOutputPPCallbacks::PragmaWarning(SourceLocation Loc,
497 StringRef WarningSpec,
499 startNewLineIfNeeded();
501 OS << "#pragma warning(" << WarningSpec << ':';
502 for (ArrayRef<int>::iterator I = Ids.begin(), E = Ids.end(); I != E; ++I)
505 setEmittedDirectiveOnThisLine();
508 void PrintPPOutputPPCallbacks::PragmaWarningPush(SourceLocation Loc,
510 startNewLineIfNeeded();
512 OS << "#pragma warning(push";
516 setEmittedDirectiveOnThisLine();
519 void PrintPPOutputPPCallbacks::PragmaWarningPop(SourceLocation Loc) {
520 startNewLineIfNeeded();
522 OS << "#pragma warning(pop)";
523 setEmittedDirectiveOnThisLine();
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()))
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());
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())
548 // This hack prevents stuff like:
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))
557 // Otherwise, indent the appropriate number of spaces.
558 for (; ColNo > 1; --ColNo)
564 void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
566 unsigned NumNewlines = 0;
567 for (; Len; --Len, ++TokStr) {
568 if (*TokStr != '\n' &&
574 // If we have \n\r or \r\n, skip both and count as one line.
576 (TokStr[1] == '\n' || TokStr[1] == '\r') &&
577 TokStr[0] != TokStr[1]) {
583 if (NumNewlines == 0) return;
585 CurLine += NumNewlines;
590 struct UnknownPragmaHandler : public PragmaHandler {
592 PrintPPOutputPPCallbacks *Callbacks;
594 // Set to true if tokens should be expanded
595 bool ShouldExpandTokens;
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));
609 if (ShouldExpandTokens) {
610 // The first token does not have expanded macros. Expand them, if
612 auto Toks = llvm::make_unique<Token[]>(1);
614 PP.EnterTokenStream(std::move(Toks), /*NumToks=*/1,
615 /*DisableMacroExpansion=*/false);
620 PrevToken.startToken();
621 PrevPrevToken.startToken();
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());
631 PrevPrevToken = PrevToken;
632 PrevToken = PragmaTok;
634 if (ShouldExpandTokens)
637 PP.LexUnexpandedToken(PragmaTok);
639 Callbacks->setEmittedDirectiveOnThisLine();
642 } // end anonymous namespace
645 static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
646 PrintPPOutputPPCallbacks *Callbacks,
648 bool DropComments = PP.getLangOpts().TraditionalCPP &&
649 !PP.getCommentRetentionState();
652 Token PrevPrevTok, PrevTok;
653 PrevPrevTok.startToken();
654 PrevTok.startToken();
656 if (Callbacks->hasEmittedDirectiveOnThisLine()) {
657 Callbacks->startNewLineIfNeeded();
658 Callbacks->MoveToLine(Tok.getLocation());
661 // If this token is at the start of a line, emit newlines if needed.
662 if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
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))) {
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.
686 } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
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);
696 // Tokens that can contain embedded newlines need to adjust our current
698 if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
699 Callbacks->HandleNewlinesInToken(TokPtr, Len);
701 std::string S = PP.getSpelling(Tok);
702 OS.write(&S[0], S.size());
704 // Tokens that can contain embedded newlines need to adjust our current
706 if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
707 Callbacks->HandleNewlinesInToken(&S[0], S.size());
709 Callbacks->setEmittedTokensOnThisLine();
711 if (Tok.is(tok::eof)) break;
713 PrevPrevTok = PrevTok;
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());
724 static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) {
725 // Ignore unknown pragmas.
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();
734 while (Tok.isNot(tok::eof));
736 SmallVector<id_macro_pair, 128> MacrosByID;
737 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
739 auto *MD = I->second.getLatest();
740 if (MD && MD->isDefined())
741 MacrosByID.push_back(id_macro_pair(I->first, MD->getMacroInfo()));
743 llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare);
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;
750 PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
755 /// DoPrintPreprocessedInput - This implements -E mode.
757 void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
758 const PreprocessorOutputOptions &Opts) {
759 // Show macros with no output is handled specially.
761 assert(Opts.ShowMacros && "Not yet implemented!");
762 DoPrintMacros(PP, OS);
766 // Inform the preprocessor whether we want it to retain comments or not, due
768 PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
770 PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(
771 PP, *OS, !Opts.ShowLineMarkers, Opts.ShowMacros,
772 Opts.ShowIncludeDirectives, Opts.UseLineDirectives);
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));
780 "GCC", new UnknownPragmaHandler(
781 "#pragma GCC", Callbacks,
782 /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt));
784 "clang", new UnknownPragmaHandler(
785 "#pragma clang", Callbacks,
786 /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt));
788 // The tokens after pragma omp need to be expanded.
790 // OpenMP [2.1, Directive format]
791 // Preprocessing tokens following the #pragma omp are subject to macro
793 PP.AddPragmaHandler("omp",
794 new UnknownPragmaHandler("#pragma omp", Callbacks,
795 /*RequireTokenExpansion=*/true));
797 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
799 // After we have configured the preprocessor, enter the main file.
800 PP.EnterMainSourceFile();
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
805 const SourceManager &SourceMgr = PP.getSourceManager();
809 if (Tok.is(tok::eof) || !Tok.getLocation().isFileID())
812 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
813 if (PLoc.isInvalid())
816 if (strcmp(PLoc.getFilename(), "<built-in>"))
820 // Read all the preprocessed tokens, printing them out to the stream.
821 PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);