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 UseLineDirectives;
97 bool IsFirstFileEntered;
99 PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os, bool lineMarkers,
100 bool defines, bool UseLineDirectives)
101 : PP(pp), SM(PP.getSourceManager()), ConcatInfo(PP), OS(os),
102 DisableLineMarkers(lineMarkers), DumpDefines(defines),
103 UseLineDirectives(UseLineDirectives) {
105 CurFilename += "<uninit>";
106 EmittedTokensOnThisLine = false;
107 EmittedDirectiveOnThisLine = false;
108 FileType = SrcMgr::C_User;
110 IsFirstFileEntered = false;
113 void setEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
114 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
116 void setEmittedDirectiveOnThisLine() { EmittedDirectiveOnThisLine = true; }
117 bool hasEmittedDirectiveOnThisLine() const {
118 return EmittedDirectiveOnThisLine;
121 bool startNewLineIfNeeded(bool ShouldUpdateCurrentLine = true);
123 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
124 SrcMgr::CharacteristicKind FileType,
125 FileID PrevFID) override;
126 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
127 StringRef FileName, bool IsAngled,
128 CharSourceRange FilenameRange, const FileEntry *File,
129 StringRef SearchPath, StringRef RelativePath,
130 const Module *Imported) override;
131 void Ident(SourceLocation Loc, StringRef str) override;
132 void PragmaMessage(SourceLocation Loc, StringRef Namespace,
133 PragmaMessageKind Kind, StringRef Str) override;
134 void PragmaDebug(SourceLocation Loc, StringRef DebugType) override;
135 void PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) override;
136 void PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) override;
137 void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
138 diag::Severity Map, StringRef Str) override;
139 void PragmaWarning(SourceLocation Loc, StringRef WarningSpec,
140 ArrayRef<int> Ids) override;
141 void PragmaWarningPush(SourceLocation Loc, int Level) override;
142 void PragmaWarningPop(SourceLocation Loc) override;
144 bool HandleFirstTokOnLine(Token &Tok);
146 /// Move to the line of the provided source location. This will
147 /// return true if the output stream required adjustment or if
148 /// the requested location is on the first line.
149 bool MoveToLine(SourceLocation Loc) {
150 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
151 if (PLoc.isInvalid())
153 return MoveToLine(PLoc.getLine()) || (PLoc.getLine() == 1);
155 bool MoveToLine(unsigned LineNo);
157 bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok,
159 return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok);
161 void WriteLineInfo(unsigned LineNo, const char *Extra=nullptr,
162 unsigned ExtraLen=0);
163 bool LineMarkersAreDisabled() const { return DisableLineMarkers; }
164 void HandleNewlinesInToken(const char *TokStr, unsigned Len);
166 /// MacroDefined - This hook is called whenever a macro definition is seen.
167 void MacroDefined(const Token &MacroNameTok,
168 const MacroDirective *MD) override;
170 /// MacroUndefined - This hook is called whenever a macro #undef is seen.
171 void MacroUndefined(const Token &MacroNameTok,
172 const MacroDefinition &MD) override;
174 } // end anonymous namespace
176 void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
179 startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
181 // Emit #line directives or GNU line markers depending on what mode we're in.
182 if (UseLineDirectives) {
183 OS << "#line" << ' ' << LineNo << ' ' << '"';
184 OS.write_escaped(CurFilename);
187 OS << '#' << ' ' << LineNo << ' ' << '"';
188 OS.write_escaped(CurFilename);
192 OS.write(Extra, ExtraLen);
194 if (FileType == SrcMgr::C_System)
196 else if (FileType == SrcMgr::C_ExternCSystem)
202 /// MoveToLine - Move the output to the source line specified by the location
203 /// object. We can do this by emitting some number of \n's, or be emitting a
204 /// #line directive. This returns false if already at the specified line, true
205 /// if some newlines were emitted.
206 bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo) {
207 // If this line is "close enough" to the original line, just print newlines,
208 // otherwise print a #line directive.
209 if (LineNo-CurLine <= 8) {
210 if (LineNo-CurLine == 1)
212 else if (LineNo == CurLine)
213 return false; // Spelling line moved, but expansion line didn't.
215 const char *NewLines = "\n\n\n\n\n\n\n\n";
216 OS.write(NewLines, LineNo-CurLine);
218 } else if (!DisableLineMarkers) {
219 // Emit a #line or line marker.
220 WriteLineInfo(LineNo, nullptr, 0);
222 // Okay, we're in -P mode, which turns off line markers. However, we still
223 // need to emit a newline between tokens on different lines.
224 startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
232 PrintPPOutputPPCallbacks::startNewLineIfNeeded(bool ShouldUpdateCurrentLine) {
233 if (EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) {
235 EmittedTokensOnThisLine = false;
236 EmittedDirectiveOnThisLine = false;
237 if (ShouldUpdateCurrentLine)
245 /// FileChanged - Whenever the preprocessor enters or exits a #include file
246 /// it invokes this handler. Update our conception of the current source
248 void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
249 FileChangeReason Reason,
250 SrcMgr::CharacteristicKind NewFileType,
252 // Unless we are exiting a #include, make sure to skip ahead to the line the
253 // #include directive was at.
254 SourceManager &SourceMgr = SM;
256 PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc);
257 if (UserLoc.isInvalid())
260 unsigned NewLine = UserLoc.getLine();
262 if (Reason == PPCallbacks::EnterFile) {
263 SourceLocation IncludeLoc = UserLoc.getIncludeLoc();
264 if (IncludeLoc.isValid())
265 MoveToLine(IncludeLoc);
266 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
267 // GCC emits the # directive for this directive on the line AFTER the
268 // directive and emits a bunch of spaces that aren't needed. This is because
269 // otherwise we will emit a line marker for THIS line, which requires an
270 // extra blank line after the directive to avoid making all following lines
271 // off by one. We can do better by simply incrementing NewLine here.
278 CurFilename += UserLoc.getFilename();
279 FileType = NewFileType;
281 if (DisableLineMarkers) {
282 startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
287 WriteLineInfo(CurLine);
291 // Do not emit an enter marker for the main file (which we expect is the first
292 // entered file). This matches gcc, and improves compatibility with some tools
293 // which track the # line markers as a way to determine when the preprocessed
294 // output is in the context of the main file.
295 if (Reason == PPCallbacks::EnterFile && !IsFirstFileEntered) {
296 IsFirstFileEntered = true;
301 case PPCallbacks::EnterFile:
302 WriteLineInfo(CurLine, " 1", 2);
304 case PPCallbacks::ExitFile:
305 WriteLineInfo(CurLine, " 2", 2);
307 case PPCallbacks::SystemHeaderPragma:
308 case PPCallbacks::RenameFile:
309 WriteLineInfo(CurLine);
314 void PrintPPOutputPPCallbacks::InclusionDirective(SourceLocation HashLoc,
315 const Token &IncludeTok,
318 CharSourceRange FilenameRange,
319 const FileEntry *File,
320 StringRef SearchPath,
321 StringRef RelativePath,
322 const Module *Imported) {
323 // When preprocessing, turn implicit imports into @imports.
324 // FIXME: This is a stop-gap until a more comprehensive "preprocessing with
325 // modules" solution is introduced.
327 startNewLineIfNeeded();
329 OS << "@import " << Imported->getFullModuleName() << ";"
330 << " /* clang -E: implicit import for \"" << File->getName() << "\" */";
331 // Since we want a newline after the @import, but not a #<line>, start a new
333 EmittedTokensOnThisLine = true;
334 startNewLineIfNeeded();
338 /// Ident - Handle #ident directives when read by the preprocessor.
340 void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, StringRef S) {
343 OS.write("#ident ", strlen("#ident "));
344 OS.write(S.begin(), S.size());
345 EmittedTokensOnThisLine = true;
348 /// MacroDefined - This hook is called whenever a macro definition is seen.
349 void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok,
350 const MacroDirective *MD) {
351 const MacroInfo *MI = MD->getMacroInfo();
352 // Only print out macro definitions in -dD mode.
354 // Ignore __FILE__ etc.
355 MI->isBuiltinMacro()) return;
357 MoveToLine(MI->getDefinitionLoc());
358 PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS);
359 setEmittedDirectiveOnThisLine();
362 void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok,
363 const MacroDefinition &MD) {
364 // Only print out macro definitions in -dD mode.
365 if (!DumpDefines) return;
367 MoveToLine(MacroNameTok.getLocation());
368 OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName();
369 setEmittedDirectiveOnThisLine();
372 static void outputPrintable(llvm::raw_ostream& OS,
373 const std::string &Str) {
374 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
375 unsigned char Char = Str[i];
376 if (isPrintable(Char) && Char != '\\' && Char != '"')
378 else // Output anything hard as an octal escape.
380 << (char)('0'+ ((Char >> 6) & 7))
381 << (char)('0'+ ((Char >> 3) & 7))
382 << (char)('0'+ ((Char >> 0) & 7));
386 void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc,
388 PragmaMessageKind Kind,
390 startNewLineIfNeeded();
393 if (!Namespace.empty())
394 OS << Namespace << ' ';
407 outputPrintable(OS, Str);
409 if (Kind == PMK_Message)
411 setEmittedDirectiveOnThisLine();
414 void PrintPPOutputPPCallbacks::PragmaDebug(SourceLocation Loc,
415 StringRef DebugType) {
416 startNewLineIfNeeded();
419 OS << "#pragma clang __debug ";
422 setEmittedDirectiveOnThisLine();
425 void PrintPPOutputPPCallbacks::
426 PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) {
427 startNewLineIfNeeded();
429 OS << "#pragma " << Namespace << " diagnostic push";
430 setEmittedDirectiveOnThisLine();
433 void PrintPPOutputPPCallbacks::
434 PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) {
435 startNewLineIfNeeded();
437 OS << "#pragma " << Namespace << " diagnostic pop";
438 setEmittedDirectiveOnThisLine();
441 void PrintPPOutputPPCallbacks::PragmaDiagnostic(SourceLocation Loc,
445 startNewLineIfNeeded();
447 OS << "#pragma " << Namespace << " diagnostic ";
449 case diag::Severity::Remark:
452 case diag::Severity::Warning:
455 case diag::Severity::Error:
458 case diag::Severity::Ignored:
461 case diag::Severity::Fatal:
465 OS << " \"" << Str << '"';
466 setEmittedDirectiveOnThisLine();
469 void PrintPPOutputPPCallbacks::PragmaWarning(SourceLocation Loc,
470 StringRef WarningSpec,
472 startNewLineIfNeeded();
474 OS << "#pragma warning(" << WarningSpec << ':';
475 for (ArrayRef<int>::iterator I = Ids.begin(), E = Ids.end(); I != E; ++I)
478 setEmittedDirectiveOnThisLine();
481 void PrintPPOutputPPCallbacks::PragmaWarningPush(SourceLocation Loc,
483 startNewLineIfNeeded();
485 OS << "#pragma warning(push";
489 setEmittedDirectiveOnThisLine();
492 void PrintPPOutputPPCallbacks::PragmaWarningPop(SourceLocation Loc) {
493 startNewLineIfNeeded();
495 OS << "#pragma warning(pop)";
496 setEmittedDirectiveOnThisLine();
499 /// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
500 /// is called for the first token on each new line. If this really is the start
501 /// of a new logical line, handle it and return true, otherwise return false.
502 /// This may not be the start of a logical line because the "start of line"
503 /// marker is set for spelling lines, not expansion ones.
504 bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
505 // Figure out what line we went to and insert the appropriate number of
506 // newline characters.
507 if (!MoveToLine(Tok.getLocation()))
510 // Print out space characters so that the first token on a line is
511 // indented for easy reading.
512 unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation());
514 // The first token on a line can have a column number of 1, yet still expect
515 // leading white space, if a macro expansion in column 1 starts with an empty
516 // macro argument, or an empty nested macro expansion. In this case, move the
517 // token to column 2.
518 if (ColNo == 1 && Tok.hasLeadingSpace())
521 // This hack prevents stuff like:
523 // HASH define foo bar
524 // From having the # character end up at column 1, which makes it so it
525 // is not handled as a #define next time through the preprocessor if in
526 // -fpreprocessed mode.
527 if (ColNo <= 1 && Tok.is(tok::hash))
530 // Otherwise, indent the appropriate number of spaces.
531 for (; ColNo > 1; --ColNo)
537 void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
539 unsigned NumNewlines = 0;
540 for (; Len; --Len, ++TokStr) {
541 if (*TokStr != '\n' &&
547 // If we have \n\r or \r\n, skip both and count as one line.
549 (TokStr[1] == '\n' || TokStr[1] == '\r') &&
550 TokStr[0] != TokStr[1])
554 if (NumNewlines == 0) return;
556 CurLine += NumNewlines;
561 struct UnknownPragmaHandler : public PragmaHandler {
563 PrintPPOutputPPCallbacks *Callbacks;
565 // Set to true if tokens should be expanded
566 bool ShouldExpandTokens;
568 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks,
569 bool RequireTokenExpansion)
570 : Prefix(prefix), Callbacks(callbacks),
571 ShouldExpandTokens(RequireTokenExpansion) {}
572 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
573 Token &PragmaTok) override {
574 // Figure out what line we went to and insert the appropriate number of
575 // newline characters.
576 Callbacks->startNewLineIfNeeded();
577 Callbacks->MoveToLine(PragmaTok.getLocation());
578 Callbacks->OS.write(Prefix, strlen(Prefix));
582 PrevToken.startToken();
583 PrevPrevToken.startToken();
585 // Read and print all of the pragma tokens.
586 while (PragmaTok.isNot(tok::eod)) {
587 if (PragmaTok.hasLeadingSpace() ||
588 Callbacks->AvoidConcat(PrevPrevToken, PrevToken, PragmaTok))
589 Callbacks->OS << ' ';
590 std::string TokSpell = PP.getSpelling(PragmaTok);
591 Callbacks->OS.write(&TokSpell[0], TokSpell.size());
593 PrevPrevToken = PrevToken;
594 PrevToken = PragmaTok;
596 if (ShouldExpandTokens)
599 PP.LexUnexpandedToken(PragmaTok);
601 Callbacks->setEmittedDirectiveOnThisLine();
604 } // end anonymous namespace
607 static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
608 PrintPPOutputPPCallbacks *Callbacks,
610 bool DropComments = PP.getLangOpts().TraditionalCPP &&
611 !PP.getCommentRetentionState();
614 Token PrevPrevTok, PrevTok;
615 PrevPrevTok.startToken();
616 PrevTok.startToken();
618 if (Callbacks->hasEmittedDirectiveOnThisLine()) {
619 Callbacks->startNewLineIfNeeded();
620 Callbacks->MoveToLine(Tok.getLocation());
623 // If this token is at the start of a line, emit newlines if needed.
624 if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
626 } else if (Tok.hasLeadingSpace() ||
627 // If we haven't emitted a token on this line yet, PrevTok isn't
628 // useful to look at and no concatenation could happen anyway.
629 (Callbacks->hasEmittedTokensOnThisLine() &&
630 // Don't print "-" next to "-", it would form "--".
631 Callbacks->AvoidConcat(PrevPrevTok, PrevTok, Tok))) {
635 if (DropComments && Tok.is(tok::comment)) {
636 // Skip comments. Normally the preprocessor does not generate
637 // tok::comment nodes at all when not keeping comments, but under
638 // -traditional-cpp the lexer keeps /all/ whitespace, including comments.
639 SourceLocation StartLoc = Tok.getLocation();
640 Callbacks->MoveToLine(StartLoc.getLocWithOffset(Tok.getLength()));
641 } else if (Tok.is(tok::annot_module_include) ||
642 Tok.is(tok::annot_module_begin) ||
643 Tok.is(tok::annot_module_end)) {
644 // PrintPPOutputPPCallbacks::InclusionDirective handles producing
645 // appropriate output here. Ignore this token entirely.
648 } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
650 } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
651 Tok.getLiteralData()) {
652 OS.write(Tok.getLiteralData(), Tok.getLength());
653 } else if (Tok.getLength() < 256) {
654 const char *TokPtr = Buffer;
655 unsigned Len = PP.getSpelling(Tok, TokPtr);
656 OS.write(TokPtr, Len);
658 // Tokens that can contain embedded newlines need to adjust our current
660 if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
661 Callbacks->HandleNewlinesInToken(TokPtr, Len);
663 std::string S = PP.getSpelling(Tok);
664 OS.write(&S[0], S.size());
666 // Tokens that can contain embedded newlines need to adjust our current
668 if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
669 Callbacks->HandleNewlinesInToken(&S[0], S.size());
671 Callbacks->setEmittedTokensOnThisLine();
673 if (Tok.is(tok::eof)) break;
675 PrevPrevTok = PrevTok;
681 typedef std::pair<const IdentifierInfo *, MacroInfo *> id_macro_pair;
682 static int MacroIDCompare(const id_macro_pair *LHS, const id_macro_pair *RHS) {
683 return LHS->first->getName().compare(RHS->first->getName());
686 static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) {
687 // Ignore unknown pragmas.
690 // -dM mode just scans and ignores all tokens in the files, then dumps out
691 // the macro table at the end.
692 PP.EnterMainSourceFile();
696 while (Tok.isNot(tok::eof));
698 SmallVector<id_macro_pair, 128> MacrosByID;
699 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
701 auto *MD = I->second.getLatest();
702 if (MD && MD->isDefined())
703 MacrosByID.push_back(id_macro_pair(I->first, MD->getMacroInfo()));
705 llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare);
707 for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
708 MacroInfo &MI = *MacrosByID[i].second;
709 // Ignore computed macros like __LINE__ and friends.
710 if (MI.isBuiltinMacro()) continue;
712 PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
717 /// DoPrintPreprocessedInput - This implements -E mode.
719 void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
720 const PreprocessorOutputOptions &Opts) {
721 // Show macros with no output is handled specially.
723 assert(Opts.ShowMacros && "Not yet implemented!");
724 DoPrintMacros(PP, OS);
728 // Inform the preprocessor whether we want it to retain comments or not, due
730 PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
732 PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(
733 PP, *OS, !Opts.ShowLineMarkers, Opts.ShowMacros, Opts.UseLineDirectives);
735 // Expand macros in pragmas with -fms-extensions. The assumption is that
736 // the majority of pragmas in such a file will be Microsoft pragmas.
737 PP.AddPragmaHandler(new UnknownPragmaHandler(
738 "#pragma", Callbacks,
739 /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt));
741 "GCC", new UnknownPragmaHandler(
742 "#pragma GCC", Callbacks,
743 /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt));
745 "clang", new UnknownPragmaHandler(
746 "#pragma clang", Callbacks,
747 /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt));
749 // The tokens after pragma omp need to be expanded.
751 // OpenMP [2.1, Directive format]
752 // Preprocessing tokens following the #pragma omp are subject to macro
754 PP.AddPragmaHandler("omp",
755 new UnknownPragmaHandler("#pragma omp", Callbacks,
756 /*RequireTokenExpansion=*/true));
758 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
760 // After we have configured the preprocessor, enter the main file.
761 PP.EnterMainSourceFile();
763 // Consume all of the tokens that come from the predefines buffer. Those
764 // should not be emitted into the output and are guaranteed to be at the
766 const SourceManager &SourceMgr = PP.getSourceManager();
770 if (Tok.is(tok::eof) || !Tok.getLocation().isFileID())
773 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
774 if (PLoc.isInvalid())
777 if (strcmp(PLoc.getFilename(), "<built-in>"))
781 // Read all the preprocessed tokens, printing them out to the stream.
782 PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);