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 (MacroInfo::tokens_iterator I = MI.tokens_begin(), E = MI.tokens_end();
69 if (I->hasLeadingSpace())
72 OS << PP.getSpelling(*I, SpellingBuffer);
76 //===----------------------------------------------------------------------===//
77 // Preprocessed token printer
78 //===----------------------------------------------------------------------===//
81 class PrintPPOutputPPCallbacks : public PPCallbacks {
84 TokenConcatenation ConcatInfo;
90 bool EmittedTokensOnThisLine;
91 bool EmittedDirectiveOnThisLine;
92 SrcMgr::CharacteristicKind FileType;
93 SmallString<512> CurFilename;
95 bool DisableLineMarkers;
97 bool UseLineDirective;
98 bool IsFirstFileEntered;
100 PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os,
101 bool lineMarkers, bool defines)
102 : PP(pp), SM(PP.getSourceManager()),
103 ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers),
104 DumpDefines(defines) {
106 CurFilename += "<uninit>";
107 EmittedTokensOnThisLine = false;
108 EmittedDirectiveOnThisLine = false;
109 FileType = SrcMgr::C_User;
111 IsFirstFileEntered = false;
113 // If we're in microsoft mode, use normal #line instead of line markers.
114 UseLineDirective = PP.getLangOpts().MicrosoftExt;
117 void setEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
118 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
120 void setEmittedDirectiveOnThisLine() { EmittedDirectiveOnThisLine = true; }
121 bool hasEmittedDirectiveOnThisLine() const {
122 return EmittedDirectiveOnThisLine;
125 bool startNewLineIfNeeded(bool ShouldUpdateCurrentLine = true);
127 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
128 SrcMgr::CharacteristicKind FileType,
130 virtual void InclusionDirective(SourceLocation HashLoc,
131 const Token &IncludeTok,
134 CharSourceRange FilenameRange,
135 const FileEntry *File,
136 StringRef SearchPath,
137 StringRef RelativePath,
138 const Module *Imported);
139 virtual void Ident(SourceLocation Loc, const std::string &str);
140 virtual void PragmaCaptured(SourceLocation Loc, StringRef Str);
141 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
142 const std::string &Str);
143 virtual void PragmaDetectMismatch(SourceLocation Loc,
144 const std::string &Name,
145 const std::string &Value);
146 virtual void PragmaMessage(SourceLocation Loc, StringRef Namespace,
147 PragmaMessageKind Kind, StringRef Str);
148 virtual void PragmaDebug(SourceLocation Loc, StringRef DebugType);
149 virtual void PragmaDiagnosticPush(SourceLocation Loc,
150 StringRef Namespace);
151 virtual void PragmaDiagnosticPop(SourceLocation Loc,
152 StringRef Namespace);
153 virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
154 diag::Mapping Map, StringRef Str);
155 virtual void PragmaWarning(SourceLocation Loc, StringRef WarningSpec,
157 virtual void PragmaWarningPush(SourceLocation Loc, int Level);
158 virtual void PragmaWarningPop(SourceLocation Loc);
160 bool HandleFirstTokOnLine(Token &Tok);
162 /// Move to the line of the provided source location. This will
163 /// return true if the output stream required adjustment or if
164 /// the requested location is on the first line.
165 bool MoveToLine(SourceLocation Loc) {
166 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
167 if (PLoc.isInvalid())
169 return MoveToLine(PLoc.getLine()) || (PLoc.getLine() == 1);
171 bool MoveToLine(unsigned LineNo);
173 bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok,
175 return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok);
177 void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0);
178 bool LineMarkersAreDisabled() const { return DisableLineMarkers; }
179 void HandleNewlinesInToken(const char *TokStr, unsigned Len);
181 /// MacroDefined - This hook is called whenever a macro definition is seen.
182 void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD);
184 /// MacroUndefined - This hook is called whenever a macro #undef is seen.
185 void MacroUndefined(const Token &MacroNameTok, const MacroDirective *MD);
187 } // end anonymous namespace
189 void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
192 startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
194 // Emit #line directives or GNU line markers depending on what mode we're in.
195 if (UseLineDirective) {
196 OS << "#line" << ' ' << LineNo << ' ' << '"';
197 OS.write_escaped(CurFilename);
200 OS << '#' << ' ' << LineNo << ' ' << '"';
201 OS.write_escaped(CurFilename);
205 OS.write(Extra, ExtraLen);
207 if (FileType == SrcMgr::C_System)
209 else if (FileType == SrcMgr::C_ExternCSystem)
215 /// MoveToLine - Move the output to the source line specified by the location
216 /// object. We can do this by emitting some number of \n's, or be emitting a
217 /// #line directive. This returns false if already at the specified line, true
218 /// if some newlines were emitted.
219 bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo) {
220 // If this line is "close enough" to the original line, just print newlines,
221 // otherwise print a #line directive.
222 if (LineNo-CurLine <= 8) {
223 if (LineNo-CurLine == 1)
225 else if (LineNo == CurLine)
226 return false; // Spelling line moved, but expansion line didn't.
228 const char *NewLines = "\n\n\n\n\n\n\n\n";
229 OS.write(NewLines, LineNo-CurLine);
231 } else if (!DisableLineMarkers) {
232 // Emit a #line or line marker.
233 WriteLineInfo(LineNo, 0, 0);
235 // Okay, we're in -P mode, which turns off line markers. However, we still
236 // need to emit a newline between tokens on different lines.
237 startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
245 PrintPPOutputPPCallbacks::startNewLineIfNeeded(bool ShouldUpdateCurrentLine) {
246 if (EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) {
248 EmittedTokensOnThisLine = false;
249 EmittedDirectiveOnThisLine = false;
250 if (ShouldUpdateCurrentLine)
258 /// FileChanged - Whenever the preprocessor enters or exits a #include file
259 /// it invokes this handler. Update our conception of the current source
261 void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
262 FileChangeReason Reason,
263 SrcMgr::CharacteristicKind NewFileType,
265 // Unless we are exiting a #include, make sure to skip ahead to the line the
266 // #include directive was at.
267 SourceManager &SourceMgr = SM;
269 PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc);
270 if (UserLoc.isInvalid())
273 unsigned NewLine = UserLoc.getLine();
275 if (Reason == PPCallbacks::EnterFile) {
276 SourceLocation IncludeLoc = UserLoc.getIncludeLoc();
277 if (IncludeLoc.isValid())
278 MoveToLine(IncludeLoc);
279 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
280 // GCC emits the # directive for this directive on the line AFTER the
281 // directive and emits a bunch of spaces that aren't needed. This is because
282 // otherwise we will emit a line marker for THIS line, which requires an
283 // extra blank line after the directive to avoid making all following lines
284 // off by one. We can do better by simply incrementing NewLine here.
291 CurFilename += UserLoc.getFilename();
292 FileType = NewFileType;
294 if (DisableLineMarkers) {
295 startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
300 WriteLineInfo(CurLine);
304 // Do not emit an enter marker for the main file (which we expect is the first
305 // entered file). This matches gcc, and improves compatibility with some tools
306 // which track the # line markers as a way to determine when the preprocessed
307 // output is in the context of the main file.
308 if (Reason == PPCallbacks::EnterFile && !IsFirstFileEntered) {
309 IsFirstFileEntered = true;
314 case PPCallbacks::EnterFile:
315 WriteLineInfo(CurLine, " 1", 2);
317 case PPCallbacks::ExitFile:
318 WriteLineInfo(CurLine, " 2", 2);
320 case PPCallbacks::SystemHeaderPragma:
321 case PPCallbacks::RenameFile:
322 WriteLineInfo(CurLine);
327 void PrintPPOutputPPCallbacks::InclusionDirective(SourceLocation HashLoc,
328 const Token &IncludeTok,
331 CharSourceRange FilenameRange,
332 const FileEntry *File,
333 StringRef SearchPath,
334 StringRef RelativePath,
335 const Module *Imported) {
336 // When preprocessing, turn implicit imports into @imports.
337 // FIXME: This is a stop-gap until a more comprehensive "preprocessing with
338 // modules" solution is introduced.
340 startNewLineIfNeeded();
342 OS << "@import " << Imported->getFullModuleName() << ";"
343 << " /* clang -E: implicit import for \"" << File->getName() << "\" */";
344 EmittedTokensOnThisLine = true;
348 /// Ident - Handle #ident directives when read by the preprocessor.
350 void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
353 OS.write("#ident ", strlen("#ident "));
354 OS.write(&S[0], S.size());
355 EmittedTokensOnThisLine = true;
358 void PrintPPOutputPPCallbacks::PragmaCaptured(SourceLocation Loc,
360 startNewLineIfNeeded();
362 OS << "#pragma captured";
364 setEmittedDirectiveOnThisLine();
367 /// MacroDefined - This hook is called whenever a macro definition is seen.
368 void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok,
369 const MacroDirective *MD) {
370 const MacroInfo *MI = MD->getMacroInfo();
371 // Only print out macro definitions in -dD mode.
373 // Ignore __FILE__ etc.
374 MI->isBuiltinMacro()) return;
376 MoveToLine(MI->getDefinitionLoc());
377 PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS);
378 setEmittedDirectiveOnThisLine();
381 void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok,
382 const MacroDirective *MD) {
383 // Only print out macro definitions in -dD mode.
384 if (!DumpDefines) return;
386 MoveToLine(MacroNameTok.getLocation());
387 OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName();
388 setEmittedDirectiveOnThisLine();
391 static void outputPrintable(llvm::raw_ostream& OS,
392 const std::string &Str) {
393 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
394 unsigned char Char = Str[i];
395 if (isPrintable(Char) && Char != '\\' && Char != '"')
397 else // Output anything hard as an octal escape.
399 << (char)('0'+ ((Char >> 6) & 7))
400 << (char)('0'+ ((Char >> 3) & 7))
401 << (char)('0'+ ((Char >> 0) & 7));
405 void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc,
406 const IdentifierInfo *Kind,
407 const std::string &Str) {
408 startNewLineIfNeeded();
410 OS << "#pragma comment(" << Kind->getName();
414 outputPrintable(OS, Str);
419 setEmittedDirectiveOnThisLine();
422 void PrintPPOutputPPCallbacks::PragmaDetectMismatch(SourceLocation Loc,
423 const std::string &Name,
424 const std::string &Value) {
425 startNewLineIfNeeded();
427 OS << "#pragma detect_mismatch(\"" << Name << '"';
428 outputPrintable(OS, Name);
430 outputPrintable(OS, Value);
432 setEmittedDirectiveOnThisLine();
435 void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc,
437 PragmaMessageKind Kind,
439 startNewLineIfNeeded();
442 if (!Namespace.empty())
443 OS << Namespace << ' ';
456 outputPrintable(OS, Str);
458 if (Kind == PMK_Message)
460 setEmittedDirectiveOnThisLine();
463 void PrintPPOutputPPCallbacks::PragmaDebug(SourceLocation Loc,
464 StringRef DebugType) {
465 startNewLineIfNeeded();
468 OS << "#pragma clang __debug ";
471 setEmittedDirectiveOnThisLine();
474 void PrintPPOutputPPCallbacks::
475 PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) {
476 startNewLineIfNeeded();
478 OS << "#pragma " << Namespace << " diagnostic push";
479 setEmittedDirectiveOnThisLine();
482 void PrintPPOutputPPCallbacks::
483 PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) {
484 startNewLineIfNeeded();
486 OS << "#pragma " << Namespace << " diagnostic pop";
487 setEmittedDirectiveOnThisLine();
490 void PrintPPOutputPPCallbacks::
491 PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
492 diag::Mapping Map, StringRef Str) {
493 startNewLineIfNeeded();
495 OS << "#pragma " << Namespace << " diagnostic ";
497 case diag::MAP_WARNING:
500 case diag::MAP_ERROR:
503 case diag::MAP_IGNORE:
506 case diag::MAP_FATAL:
510 OS << " \"" << Str << '"';
511 setEmittedDirectiveOnThisLine();
514 void PrintPPOutputPPCallbacks::PragmaWarning(SourceLocation Loc,
515 StringRef WarningSpec,
517 startNewLineIfNeeded();
519 OS << "#pragma warning(" << WarningSpec << ':';
520 for (ArrayRef<int>::iterator I = Ids.begin(), E = Ids.end(); I != E; ++I)
523 setEmittedDirectiveOnThisLine();
526 void PrintPPOutputPPCallbacks::PragmaWarningPush(SourceLocation Loc,
528 startNewLineIfNeeded();
530 OS << "#pragma warning(push";
534 setEmittedDirectiveOnThisLine();
537 void PrintPPOutputPPCallbacks::PragmaWarningPop(SourceLocation Loc) {
538 startNewLineIfNeeded();
540 OS << "#pragma warning(pop)";
541 setEmittedDirectiveOnThisLine();
544 /// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
545 /// is called for the first token on each new line. If this really is the start
546 /// of a new logical line, handle it and return true, otherwise return false.
547 /// This may not be the start of a logical line because the "start of line"
548 /// marker is set for spelling lines, not expansion ones.
549 bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
550 // Figure out what line we went to and insert the appropriate number of
551 // newline characters.
552 if (!MoveToLine(Tok.getLocation()))
555 // Print out space characters so that the first token on a line is
556 // indented for easy reading.
557 unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation());
559 // This hack prevents stuff like:
561 // HASH define foo bar
562 // From having the # character end up at column 1, which makes it so it
563 // is not handled as a #define next time through the preprocessor if in
564 // -fpreprocessed mode.
565 if (ColNo <= 1 && Tok.is(tok::hash))
568 // Otherwise, indent the appropriate number of spaces.
569 for (; ColNo > 1; --ColNo)
575 void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
577 unsigned NumNewlines = 0;
578 for (; Len; --Len, ++TokStr) {
579 if (*TokStr != '\n' &&
585 // If we have \n\r or \r\n, skip both and count as one line.
587 (TokStr[1] == '\n' || TokStr[1] == '\r') &&
588 TokStr[0] != TokStr[1])
592 if (NumNewlines == 0) return;
594 CurLine += NumNewlines;
599 struct UnknownPragmaHandler : public PragmaHandler {
601 PrintPPOutputPPCallbacks *Callbacks;
603 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
604 : Prefix(prefix), Callbacks(callbacks) {}
605 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
607 // Figure out what line we went to and insert the appropriate number of
608 // newline characters.
609 Callbacks->startNewLineIfNeeded();
610 Callbacks->MoveToLine(PragmaTok.getLocation());
611 Callbacks->OS.write(Prefix, strlen(Prefix));
612 // Read and print all of the pragma tokens.
613 while (PragmaTok.isNot(tok::eod)) {
614 if (PragmaTok.hasLeadingSpace())
615 Callbacks->OS << ' ';
616 std::string TokSpell = PP.getSpelling(PragmaTok);
617 Callbacks->OS.write(&TokSpell[0], TokSpell.size());
618 PP.LexUnexpandedToken(PragmaTok);
620 Callbacks->setEmittedDirectiveOnThisLine();
623 } // end anonymous namespace
626 static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
627 PrintPPOutputPPCallbacks *Callbacks,
629 bool DropComments = PP.getLangOpts().TraditionalCPP &&
630 !PP.getCommentRetentionState();
633 Token PrevPrevTok, PrevTok;
634 PrevPrevTok.startToken();
635 PrevTok.startToken();
637 if (Callbacks->hasEmittedDirectiveOnThisLine()) {
638 Callbacks->startNewLineIfNeeded();
639 Callbacks->MoveToLine(Tok.getLocation());
642 // If this token is at the start of a line, emit newlines if needed.
643 if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
645 } else if (Tok.hasLeadingSpace() ||
646 // If we haven't emitted a token on this line yet, PrevTok isn't
647 // useful to look at and no concatenation could happen anyway.
648 (Callbacks->hasEmittedTokensOnThisLine() &&
649 // Don't print "-" next to "-", it would form "--".
650 Callbacks->AvoidConcat(PrevPrevTok, PrevTok, Tok))) {
654 if (DropComments && Tok.is(tok::comment)) {
655 // Skip comments. Normally the preprocessor does not generate
656 // tok::comment nodes at all when not keeping comments, but under
657 // -traditional-cpp the lexer keeps /all/ whitespace, including comments.
658 SourceLocation StartLoc = Tok.getLocation();
659 Callbacks->MoveToLine(StartLoc.getLocWithOffset(Tok.getLength()));
660 } else if (Tok.is(tok::annot_module_include)) {
661 // PrintPPOutputPPCallbacks::InclusionDirective handles producing
662 // appropriate output here. Ignore this token entirely.
665 } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
667 } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
668 Tok.getLiteralData()) {
669 OS.write(Tok.getLiteralData(), Tok.getLength());
670 } else if (Tok.getLength() < 256) {
671 const char *TokPtr = Buffer;
672 unsigned Len = PP.getSpelling(Tok, TokPtr);
673 OS.write(TokPtr, Len);
675 // Tokens that can contain embedded newlines need to adjust our current
677 if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
678 Callbacks->HandleNewlinesInToken(TokPtr, Len);
680 std::string S = PP.getSpelling(Tok);
681 OS.write(&S[0], S.size());
683 // Tokens that can contain embedded newlines need to adjust our current
685 if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
686 Callbacks->HandleNewlinesInToken(&S[0], S.size());
688 Callbacks->setEmittedTokensOnThisLine();
690 if (Tok.is(tok::eof)) break;
692 PrevPrevTok = PrevTok;
698 typedef std::pair<const IdentifierInfo *, MacroInfo *> id_macro_pair;
699 static int MacroIDCompare(const id_macro_pair *LHS, const id_macro_pair *RHS) {
700 return LHS->first->getName().compare(RHS->first->getName());
703 static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) {
704 // Ignore unknown pragmas.
705 PP.AddPragmaHandler(new EmptyPragmaHandler());
707 // -dM mode just scans and ignores all tokens in the files, then dumps out
708 // the macro table at the end.
709 PP.EnterMainSourceFile();
713 while (Tok.isNot(tok::eof));
715 SmallVector<id_macro_pair, 128> MacrosByID;
716 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
718 if (I->first->hasMacroDefinition())
719 MacrosByID.push_back(id_macro_pair(I->first, I->second->getMacroInfo()));
721 llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare);
723 for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
724 MacroInfo &MI = *MacrosByID[i].second;
725 // Ignore computed macros like __LINE__ and friends.
726 if (MI.isBuiltinMacro()) continue;
728 PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
733 /// DoPrintPreprocessedInput - This implements -E mode.
735 void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
736 const PreprocessorOutputOptions &Opts) {
737 // Show macros with no output is handled specially.
739 assert(Opts.ShowMacros && "Not yet implemented!");
740 DoPrintMacros(PP, OS);
744 // Inform the preprocessor whether we want it to retain comments or not, due
746 PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
748 PrintPPOutputPPCallbacks *Callbacks =
749 new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers,
751 PP.AddPragmaHandler(new UnknownPragmaHandler("#pragma", Callbacks));
752 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks));
753 PP.AddPragmaHandler("clang",
754 new UnknownPragmaHandler("#pragma clang", Callbacks));
756 PP.addPPCallbacks(Callbacks);
758 // After we have configured the preprocessor, enter the main file.
759 PP.EnterMainSourceFile();
761 // Consume all of the tokens that come from the predefines buffer. Those
762 // should not be emitted into the output and are guaranteed to be at the
764 const SourceManager &SourceMgr = PP.getSourceManager();
768 if (Tok.is(tok::eof) || !Tok.getLocation().isFileID())
771 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
772 if (PLoc.isInvalid())
775 if (strcmp(PLoc.getFilename(), "<built-in>"))
779 // Read all the preprocessed tokens, printing them out to the stream.
780 PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);