10b57cec5SDimitry Andric //===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This code simply runs the preprocessor on the input file and prints out the 100b57cec5SDimitry Andric // result. This is the traditional behavior of the -E option. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "clang/Frontend/Utils.h" 150b57cec5SDimitry Andric #include "clang/Basic/CharInfo.h" 160b57cec5SDimitry Andric #include "clang/Basic/Diagnostic.h" 170b57cec5SDimitry Andric #include "clang/Basic/SourceManager.h" 180b57cec5SDimitry Andric #include "clang/Frontend/PreprocessorOutputOptions.h" 190b57cec5SDimitry Andric #include "clang/Lex/MacroInfo.h" 200b57cec5SDimitry Andric #include "clang/Lex/PPCallbacks.h" 210b57cec5SDimitry Andric #include "clang/Lex/Pragma.h" 220b57cec5SDimitry Andric #include "clang/Lex/Preprocessor.h" 230b57cec5SDimitry Andric #include "clang/Lex/TokenConcatenation.h" 240b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 250b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 260b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 270b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 280b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 290b57cec5SDimitry Andric #include <cstdio> 300b57cec5SDimitry Andric using namespace clang; 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric /// PrintMacroDefinition - Print a macro definition in a form that will be 330b57cec5SDimitry Andric /// properly accepted back as a definition. 340b57cec5SDimitry Andric static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI, 350b57cec5SDimitry Andric Preprocessor &PP, raw_ostream &OS) { 360b57cec5SDimitry Andric OS << "#define " << II.getName(); 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric if (MI.isFunctionLike()) { 390b57cec5SDimitry Andric OS << '('; 400b57cec5SDimitry Andric if (!MI.param_empty()) { 410b57cec5SDimitry Andric MacroInfo::param_iterator AI = MI.param_begin(), E = MI.param_end(); 420b57cec5SDimitry Andric for (; AI+1 != E; ++AI) { 430b57cec5SDimitry Andric OS << (*AI)->getName(); 440b57cec5SDimitry Andric OS << ','; 450b57cec5SDimitry Andric } 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric // Last argument. 480b57cec5SDimitry Andric if ((*AI)->getName() == "__VA_ARGS__") 490b57cec5SDimitry Andric OS << "..."; 500b57cec5SDimitry Andric else 510b57cec5SDimitry Andric OS << (*AI)->getName(); 520b57cec5SDimitry Andric } 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric if (MI.isGNUVarargs()) 550b57cec5SDimitry Andric OS << "..."; // #define foo(x...) 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric OS << ')'; 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric // GCC always emits a space, even if the macro body is empty. However, do not 610b57cec5SDimitry Andric // want to emit two spaces if the first token has a leading space. 620b57cec5SDimitry Andric if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace()) 630b57cec5SDimitry Andric OS << ' '; 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric SmallString<128> SpellingBuffer; 660b57cec5SDimitry Andric for (const auto &T : MI.tokens()) { 670b57cec5SDimitry Andric if (T.hasLeadingSpace()) 680b57cec5SDimitry Andric OS << ' '; 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric OS << PP.getSpelling(T, SpellingBuffer); 710b57cec5SDimitry Andric } 720b57cec5SDimitry Andric } 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 750b57cec5SDimitry Andric // Preprocessed token printer 760b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric namespace { 790b57cec5SDimitry Andric class PrintPPOutputPPCallbacks : public PPCallbacks { 800b57cec5SDimitry Andric Preprocessor &PP; 810b57cec5SDimitry Andric SourceManager &SM; 820b57cec5SDimitry Andric TokenConcatenation ConcatInfo; 830b57cec5SDimitry Andric public: 840b57cec5SDimitry Andric raw_ostream &OS; 850b57cec5SDimitry Andric private: 860b57cec5SDimitry Andric unsigned CurLine; 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric bool EmittedTokensOnThisLine; 890b57cec5SDimitry Andric bool EmittedDirectiveOnThisLine; 900b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType; 910b57cec5SDimitry Andric SmallString<512> CurFilename; 920b57cec5SDimitry Andric bool Initialized; 930b57cec5SDimitry Andric bool DisableLineMarkers; 940b57cec5SDimitry Andric bool DumpDefines; 950b57cec5SDimitry Andric bool DumpIncludeDirectives; 960b57cec5SDimitry Andric bool UseLineDirectives; 970b57cec5SDimitry Andric bool IsFirstFileEntered; 98349cc55cSDimitry Andric bool MinimizeWhitespace; 99349cc55cSDimitry Andric 100349cc55cSDimitry Andric Token PrevTok; 101349cc55cSDimitry Andric Token PrevPrevTok; 102349cc55cSDimitry Andric 1030b57cec5SDimitry Andric public: 1040b57cec5SDimitry Andric PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os, bool lineMarkers, 1050b57cec5SDimitry Andric bool defines, bool DumpIncludeDirectives, 106349cc55cSDimitry Andric bool UseLineDirectives, bool MinimizeWhitespace) 1070b57cec5SDimitry Andric : PP(pp), SM(PP.getSourceManager()), ConcatInfo(PP), OS(os), 1080b57cec5SDimitry Andric DisableLineMarkers(lineMarkers), DumpDefines(defines), 1090b57cec5SDimitry Andric DumpIncludeDirectives(DumpIncludeDirectives), 110349cc55cSDimitry Andric UseLineDirectives(UseLineDirectives), 111349cc55cSDimitry Andric MinimizeWhitespace(MinimizeWhitespace) { 1120b57cec5SDimitry Andric CurLine = 0; 1130b57cec5SDimitry Andric CurFilename += "<uninit>"; 1140b57cec5SDimitry Andric EmittedTokensOnThisLine = false; 1150b57cec5SDimitry Andric EmittedDirectiveOnThisLine = false; 1160b57cec5SDimitry Andric FileType = SrcMgr::C_User; 1170b57cec5SDimitry Andric Initialized = false; 1180b57cec5SDimitry Andric IsFirstFileEntered = false; 119349cc55cSDimitry Andric 120349cc55cSDimitry Andric PrevTok.startToken(); 121349cc55cSDimitry Andric PrevPrevTok.startToken(); 1220b57cec5SDimitry Andric } 1230b57cec5SDimitry Andric 124349cc55cSDimitry Andric bool isMinimizeWhitespace() const { return MinimizeWhitespace; } 125349cc55cSDimitry Andric 1260b57cec5SDimitry Andric void setEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; } 1270b57cec5SDimitry Andric bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; } 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric void setEmittedDirectiveOnThisLine() { EmittedDirectiveOnThisLine = true; } 1300b57cec5SDimitry Andric bool hasEmittedDirectiveOnThisLine() const { 1310b57cec5SDimitry Andric return EmittedDirectiveOnThisLine; 1320b57cec5SDimitry Andric } 1330b57cec5SDimitry Andric 134349cc55cSDimitry Andric /// Ensure that the output stream position is at the beginning of a new line 135349cc55cSDimitry Andric /// and inserts one if it does not. It is intended to ensure that directives 136349cc55cSDimitry Andric /// inserted by the directives not from the input source (such as #line) are 137349cc55cSDimitry Andric /// in the first column. To insert newlines that represent the input, use 138349cc55cSDimitry Andric /// MoveToLine(/*...*/, /*RequireStartOfLine=*/true). 139349cc55cSDimitry Andric void startNewLineIfNeeded(); 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric void FileChanged(SourceLocation Loc, FileChangeReason Reason, 1420b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType, 1430b57cec5SDimitry Andric FileID PrevFID) override; 1440b57cec5SDimitry Andric void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, 1450b57cec5SDimitry Andric StringRef FileName, bool IsAngled, 1460b57cec5SDimitry Andric CharSourceRange FilenameRange, const FileEntry *File, 1470b57cec5SDimitry Andric StringRef SearchPath, StringRef RelativePath, 1480b57cec5SDimitry Andric const Module *Imported, 1490b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType) override; 1500b57cec5SDimitry Andric void Ident(SourceLocation Loc, StringRef str) override; 1510b57cec5SDimitry Andric void PragmaMessage(SourceLocation Loc, StringRef Namespace, 1520b57cec5SDimitry Andric PragmaMessageKind Kind, StringRef Str) override; 1530b57cec5SDimitry Andric void PragmaDebug(SourceLocation Loc, StringRef DebugType) override; 1540b57cec5SDimitry Andric void PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) override; 1550b57cec5SDimitry Andric void PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) override; 1560b57cec5SDimitry Andric void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace, 1570b57cec5SDimitry Andric diag::Severity Map, StringRef Str) override; 158349cc55cSDimitry Andric void PragmaWarning(SourceLocation Loc, PragmaWarningSpecifier WarningSpec, 1590b57cec5SDimitry Andric ArrayRef<int> Ids) override; 1600b57cec5SDimitry Andric void PragmaWarningPush(SourceLocation Loc, int Level) override; 1610b57cec5SDimitry Andric void PragmaWarningPop(SourceLocation Loc) override; 1620b57cec5SDimitry Andric void PragmaExecCharsetPush(SourceLocation Loc, StringRef Str) override; 1630b57cec5SDimitry Andric void PragmaExecCharsetPop(SourceLocation Loc) override; 1640b57cec5SDimitry Andric void PragmaAssumeNonNullBegin(SourceLocation Loc) override; 1650b57cec5SDimitry Andric void PragmaAssumeNonNullEnd(SourceLocation Loc) override; 1660b57cec5SDimitry Andric 167349cc55cSDimitry Andric /// Insert whitespace before emitting the next token. 168349cc55cSDimitry Andric /// 169349cc55cSDimitry Andric /// @param Tok Next token to be emitted. 170349cc55cSDimitry Andric /// @param RequireSpace Ensure at least one whitespace is emitted. Useful 171349cc55cSDimitry Andric /// if non-tokens have been emitted to the stream. 172349cc55cSDimitry Andric /// @param RequireSameLine Never emit newlines. Useful when semantics depend 173349cc55cSDimitry Andric /// on being on the same line, such as directives. 174349cc55cSDimitry Andric void HandleWhitespaceBeforeTok(const Token &Tok, bool RequireSpace, 175349cc55cSDimitry Andric bool RequireSameLine); 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric /// Move to the line of the provided source location. This will 178349cc55cSDimitry Andric /// return true if a newline was inserted or if 179349cc55cSDimitry Andric /// the requested location is the first token on the first line. 180349cc55cSDimitry Andric /// In these cases the next output will be the first column on the line and 181349cc55cSDimitry Andric /// make it possible to insert indention. The newline was inserted 182349cc55cSDimitry Andric /// implicitly when at the beginning of the file. 183349cc55cSDimitry Andric /// 184349cc55cSDimitry Andric /// @param Tok Token where to move to. 185349cc55cSDimitry Andric /// @param RequireStartOfLine Whether the next line depends on being in the 186349cc55cSDimitry Andric /// first column, such as a directive. 187349cc55cSDimitry Andric /// 188349cc55cSDimitry Andric /// @return Whether column adjustments are necessary. 189349cc55cSDimitry Andric bool MoveToLine(const Token &Tok, bool RequireStartOfLine) { 190349cc55cSDimitry Andric PresumedLoc PLoc = SM.getPresumedLoc(Tok.getLocation()); 191349cc55cSDimitry Andric unsigned TargetLine = PLoc.isValid() ? PLoc.getLine() : CurLine; 192349cc55cSDimitry Andric bool IsFirstInFile = Tok.isAtStartOfLine() && PLoc.getLine() == 1; 193349cc55cSDimitry Andric return MoveToLine(TargetLine, RequireStartOfLine) || IsFirstInFile; 1940b57cec5SDimitry Andric } 195349cc55cSDimitry Andric 196349cc55cSDimitry Andric /// Move to the line of the provided source location. Returns true if a new 197349cc55cSDimitry Andric /// line was inserted. 198349cc55cSDimitry Andric bool MoveToLine(SourceLocation Loc, bool RequireStartOfLine) { 199349cc55cSDimitry Andric PresumedLoc PLoc = SM.getPresumedLoc(Loc); 200349cc55cSDimitry Andric unsigned TargetLine = PLoc.isValid() ? PLoc.getLine() : CurLine; 201349cc55cSDimitry Andric return MoveToLine(TargetLine, RequireStartOfLine); 202349cc55cSDimitry Andric } 203349cc55cSDimitry Andric bool MoveToLine(unsigned LineNo, bool RequireStartOfLine); 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok, 2060b57cec5SDimitry Andric const Token &Tok) { 2070b57cec5SDimitry Andric return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok); 2080b57cec5SDimitry Andric } 2090b57cec5SDimitry Andric void WriteLineInfo(unsigned LineNo, const char *Extra=nullptr, 2100b57cec5SDimitry Andric unsigned ExtraLen=0); 2110b57cec5SDimitry Andric bool LineMarkersAreDisabled() const { return DisableLineMarkers; } 2120b57cec5SDimitry Andric void HandleNewlinesInToken(const char *TokStr, unsigned Len); 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric /// MacroDefined - This hook is called whenever a macro definition is seen. 2150b57cec5SDimitry Andric void MacroDefined(const Token &MacroNameTok, 2160b57cec5SDimitry Andric const MacroDirective *MD) override; 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric /// MacroUndefined - This hook is called whenever a macro #undef is seen. 2190b57cec5SDimitry Andric void MacroUndefined(const Token &MacroNameTok, 2200b57cec5SDimitry Andric const MacroDefinition &MD, 2210b57cec5SDimitry Andric const MacroDirective *Undef) override; 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric void BeginModule(const Module *M); 2240b57cec5SDimitry Andric void EndModule(const Module *M); 2250b57cec5SDimitry Andric }; 2260b57cec5SDimitry Andric } // end anonymous namespace 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo, 2290b57cec5SDimitry Andric const char *Extra, 2300b57cec5SDimitry Andric unsigned ExtraLen) { 231349cc55cSDimitry Andric startNewLineIfNeeded(); 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric // Emit #line directives or GNU line markers depending on what mode we're in. 2340b57cec5SDimitry Andric if (UseLineDirectives) { 2350b57cec5SDimitry Andric OS << "#line" << ' ' << LineNo << ' ' << '"'; 2360b57cec5SDimitry Andric OS.write_escaped(CurFilename); 2370b57cec5SDimitry Andric OS << '"'; 2380b57cec5SDimitry Andric } else { 2390b57cec5SDimitry Andric OS << '#' << ' ' << LineNo << ' ' << '"'; 2400b57cec5SDimitry Andric OS.write_escaped(CurFilename); 2410b57cec5SDimitry Andric OS << '"'; 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric if (ExtraLen) 2440b57cec5SDimitry Andric OS.write(Extra, ExtraLen); 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric if (FileType == SrcMgr::C_System) 2470b57cec5SDimitry Andric OS.write(" 3", 2); 2480b57cec5SDimitry Andric else if (FileType == SrcMgr::C_ExternCSystem) 2490b57cec5SDimitry Andric OS.write(" 3 4", 4); 2500b57cec5SDimitry Andric } 2510b57cec5SDimitry Andric OS << '\n'; 2520b57cec5SDimitry Andric } 2530b57cec5SDimitry Andric 2540b57cec5SDimitry Andric /// MoveToLine - Move the output to the source line specified by the location 2550b57cec5SDimitry Andric /// object. We can do this by emitting some number of \n's, or be emitting a 2560b57cec5SDimitry Andric /// #line directive. This returns false if already at the specified line, true 2570b57cec5SDimitry Andric /// if some newlines were emitted. 258349cc55cSDimitry Andric bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo, 259349cc55cSDimitry Andric bool RequireStartOfLine) { 260349cc55cSDimitry Andric // If it is required to start a new line or finish the current, insert 261349cc55cSDimitry Andric // vertical whitespace now and take it into account when moving to the 262349cc55cSDimitry Andric // expected line. 263349cc55cSDimitry Andric bool StartedNewLine = false; 264349cc55cSDimitry Andric if ((RequireStartOfLine && EmittedTokensOnThisLine) || 265349cc55cSDimitry Andric EmittedDirectiveOnThisLine) { 266349cc55cSDimitry Andric OS << '\n'; 267349cc55cSDimitry Andric StartedNewLine = true; 268349cc55cSDimitry Andric CurLine += 1; 269349cc55cSDimitry Andric EmittedTokensOnThisLine = false; 270349cc55cSDimitry Andric EmittedDirectiveOnThisLine = false; 271349cc55cSDimitry Andric } 272349cc55cSDimitry Andric 2730b57cec5SDimitry Andric // If this line is "close enough" to the original line, just print newlines, 2740b57cec5SDimitry Andric // otherwise print a #line directive. 275349cc55cSDimitry Andric if (CurLine == LineNo) { 276349cc55cSDimitry Andric // Nothing to do if we are already on the correct line. 277349cc55cSDimitry Andric } else if (MinimizeWhitespace && DisableLineMarkers) { 278349cc55cSDimitry Andric // With -E -P -fminimize-whitespace, don't emit anything if not necessary. 279349cc55cSDimitry Andric } else if (!StartedNewLine && LineNo - CurLine == 1) { 280349cc55cSDimitry Andric // Printing a single line has priority over printing a #line directive, even 281349cc55cSDimitry Andric // when minimizing whitespace which otherwise would print #line directives 282349cc55cSDimitry Andric // for every single line. 2830b57cec5SDimitry Andric OS << '\n'; 284349cc55cSDimitry Andric StartedNewLine = true; 285349cc55cSDimitry Andric } else if (!DisableLineMarkers) { 286349cc55cSDimitry Andric if (LineNo - CurLine <= 8) { 2870b57cec5SDimitry Andric const char *NewLines = "\n\n\n\n\n\n\n\n"; 2880b57cec5SDimitry Andric OS.write(NewLines, LineNo - CurLine); 289349cc55cSDimitry Andric } else { 2900b57cec5SDimitry Andric // Emit a #line or line marker. 2910b57cec5SDimitry Andric WriteLineInfo(LineNo, nullptr, 0); 292349cc55cSDimitry Andric } 293349cc55cSDimitry Andric StartedNewLine = true; 294349cc55cSDimitry Andric } else if (EmittedTokensOnThisLine) { 295349cc55cSDimitry Andric // If we are not on the correct line and don't need to be line-correct, 296349cc55cSDimitry Andric // at least ensure we start on a new line. 297349cc55cSDimitry Andric OS << '\n'; 298349cc55cSDimitry Andric StartedNewLine = true; 299349cc55cSDimitry Andric } 300349cc55cSDimitry Andric 301349cc55cSDimitry Andric if (StartedNewLine) { 302349cc55cSDimitry Andric EmittedTokensOnThisLine = false; 303349cc55cSDimitry Andric EmittedDirectiveOnThisLine = false; 3040b57cec5SDimitry Andric } 3050b57cec5SDimitry Andric 3060b57cec5SDimitry Andric CurLine = LineNo; 307349cc55cSDimitry Andric return StartedNewLine; 3080b57cec5SDimitry Andric } 3090b57cec5SDimitry Andric 310349cc55cSDimitry Andric void PrintPPOutputPPCallbacks::startNewLineIfNeeded() { 3110b57cec5SDimitry Andric if (EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) { 3120b57cec5SDimitry Andric OS << '\n'; 3130b57cec5SDimitry Andric EmittedTokensOnThisLine = false; 3140b57cec5SDimitry Andric EmittedDirectiveOnThisLine = false; 3150b57cec5SDimitry Andric } 3160b57cec5SDimitry Andric } 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric /// FileChanged - Whenever the preprocessor enters or exits a #include file 3190b57cec5SDimitry Andric /// it invokes this handler. Update our conception of the current source 3200b57cec5SDimitry Andric /// position. 3210b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc, 3220b57cec5SDimitry Andric FileChangeReason Reason, 3230b57cec5SDimitry Andric SrcMgr::CharacteristicKind NewFileType, 3240b57cec5SDimitry Andric FileID PrevFID) { 3250b57cec5SDimitry Andric // Unless we are exiting a #include, make sure to skip ahead to the line the 3260b57cec5SDimitry Andric // #include directive was at. 3270b57cec5SDimitry Andric SourceManager &SourceMgr = SM; 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc); 3300b57cec5SDimitry Andric if (UserLoc.isInvalid()) 3310b57cec5SDimitry Andric return; 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric unsigned NewLine = UserLoc.getLine(); 3340b57cec5SDimitry Andric 3350b57cec5SDimitry Andric if (Reason == PPCallbacks::EnterFile) { 3360b57cec5SDimitry Andric SourceLocation IncludeLoc = UserLoc.getIncludeLoc(); 3370b57cec5SDimitry Andric if (IncludeLoc.isValid()) 338349cc55cSDimitry Andric MoveToLine(IncludeLoc, /*RequireStartOfLine=*/false); 3390b57cec5SDimitry Andric } else if (Reason == PPCallbacks::SystemHeaderPragma) { 3400b57cec5SDimitry Andric // GCC emits the # directive for this directive on the line AFTER the 3410b57cec5SDimitry Andric // directive and emits a bunch of spaces that aren't needed. This is because 3420b57cec5SDimitry Andric // otherwise we will emit a line marker for THIS line, which requires an 3430b57cec5SDimitry Andric // extra blank line after the directive to avoid making all following lines 3440b57cec5SDimitry Andric // off by one. We can do better by simply incrementing NewLine here. 3450b57cec5SDimitry Andric NewLine += 1; 3460b57cec5SDimitry Andric } 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric CurLine = NewLine; 3490b57cec5SDimitry Andric 3500b57cec5SDimitry Andric CurFilename.clear(); 3510b57cec5SDimitry Andric CurFilename += UserLoc.getFilename(); 3520b57cec5SDimitry Andric FileType = NewFileType; 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric if (DisableLineMarkers) { 355349cc55cSDimitry Andric if (!MinimizeWhitespace) 356349cc55cSDimitry Andric startNewLineIfNeeded(); 3570b57cec5SDimitry Andric return; 3580b57cec5SDimitry Andric } 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andric if (!Initialized) { 3610b57cec5SDimitry Andric WriteLineInfo(CurLine); 3620b57cec5SDimitry Andric Initialized = true; 3630b57cec5SDimitry Andric } 3640b57cec5SDimitry Andric 3650b57cec5SDimitry Andric // Do not emit an enter marker for the main file (which we expect is the first 3660b57cec5SDimitry Andric // entered file). This matches gcc, and improves compatibility with some tools 3670b57cec5SDimitry Andric // which track the # line markers as a way to determine when the preprocessed 3680b57cec5SDimitry Andric // output is in the context of the main file. 3690b57cec5SDimitry Andric if (Reason == PPCallbacks::EnterFile && !IsFirstFileEntered) { 3700b57cec5SDimitry Andric IsFirstFileEntered = true; 3710b57cec5SDimitry Andric return; 3720b57cec5SDimitry Andric } 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric switch (Reason) { 3750b57cec5SDimitry Andric case PPCallbacks::EnterFile: 3760b57cec5SDimitry Andric WriteLineInfo(CurLine, " 1", 2); 3770b57cec5SDimitry Andric break; 3780b57cec5SDimitry Andric case PPCallbacks::ExitFile: 3790b57cec5SDimitry Andric WriteLineInfo(CurLine, " 2", 2); 3800b57cec5SDimitry Andric break; 3810b57cec5SDimitry Andric case PPCallbacks::SystemHeaderPragma: 3820b57cec5SDimitry Andric case PPCallbacks::RenameFile: 3830b57cec5SDimitry Andric WriteLineInfo(CurLine); 3840b57cec5SDimitry Andric break; 3850b57cec5SDimitry Andric } 3860b57cec5SDimitry Andric } 3870b57cec5SDimitry Andric 3880b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::InclusionDirective( 3890b57cec5SDimitry Andric SourceLocation HashLoc, 3900b57cec5SDimitry Andric const Token &IncludeTok, 3910b57cec5SDimitry Andric StringRef FileName, 3920b57cec5SDimitry Andric bool IsAngled, 3930b57cec5SDimitry Andric CharSourceRange FilenameRange, 3940b57cec5SDimitry Andric const FileEntry *File, 3950b57cec5SDimitry Andric StringRef SearchPath, 3960b57cec5SDimitry Andric StringRef RelativePath, 3970b57cec5SDimitry Andric const Module *Imported, 3980b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType) { 3990b57cec5SDimitry Andric // In -dI mode, dump #include directives prior to dumping their content or 4000b57cec5SDimitry Andric // interpretation. 4010b57cec5SDimitry Andric if (DumpIncludeDirectives) { 402349cc55cSDimitry Andric MoveToLine(HashLoc, /*RequireStartOfLine=*/true); 4030b57cec5SDimitry Andric const std::string TokenText = PP.getSpelling(IncludeTok); 4040b57cec5SDimitry Andric assert(!TokenText.empty()); 4050b57cec5SDimitry Andric OS << "#" << TokenText << " " 4060b57cec5SDimitry Andric << (IsAngled ? '<' : '"') << FileName << (IsAngled ? '>' : '"') 4070b57cec5SDimitry Andric << " /* clang -E -dI */"; 4080b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4090b57cec5SDimitry Andric } 4100b57cec5SDimitry Andric 4110b57cec5SDimitry Andric // When preprocessing, turn implicit imports into module import pragmas. 4120b57cec5SDimitry Andric if (Imported) { 4130b57cec5SDimitry Andric switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) { 4140b57cec5SDimitry Andric case tok::pp_include: 4150b57cec5SDimitry Andric case tok::pp_import: 4160b57cec5SDimitry Andric case tok::pp_include_next: 417349cc55cSDimitry Andric MoveToLine(HashLoc, /*RequireStartOfLine=*/true); 4180b57cec5SDimitry Andric OS << "#pragma clang module import " << Imported->getFullModuleName(true) 4190b57cec5SDimitry Andric << " /* clang -E: implicit import for " 4200b57cec5SDimitry Andric << "#" << PP.getSpelling(IncludeTok) << " " 4210b57cec5SDimitry Andric << (IsAngled ? '<' : '"') << FileName << (IsAngled ? '>' : '"') 4220b57cec5SDimitry Andric << " */"; 423349cc55cSDimitry Andric setEmittedDirectiveOnThisLine(); 4240b57cec5SDimitry Andric break; 4250b57cec5SDimitry Andric 4260b57cec5SDimitry Andric case tok::pp___include_macros: 4270b57cec5SDimitry Andric // #__include_macros has no effect on a user of a preprocessed source 4280b57cec5SDimitry Andric // file; the only effect is on preprocessing. 4290b57cec5SDimitry Andric // 4300b57cec5SDimitry Andric // FIXME: That's not *quite* true: it causes the module in question to 4310b57cec5SDimitry Andric // be loaded, which can affect downstream diagnostics. 4320b57cec5SDimitry Andric break; 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric default: 4350b57cec5SDimitry Andric llvm_unreachable("unknown include directive kind"); 4360b57cec5SDimitry Andric break; 4370b57cec5SDimitry Andric } 4380b57cec5SDimitry Andric } 4390b57cec5SDimitry Andric } 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric /// Handle entering the scope of a module during a module compilation. 4420b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::BeginModule(const Module *M) { 4430b57cec5SDimitry Andric startNewLineIfNeeded(); 4440b57cec5SDimitry Andric OS << "#pragma clang module begin " << M->getFullModuleName(true); 4450b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4460b57cec5SDimitry Andric } 4470b57cec5SDimitry Andric 4480b57cec5SDimitry Andric /// Handle leaving the scope of a module during a module compilation. 4490b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::EndModule(const Module *M) { 4500b57cec5SDimitry Andric startNewLineIfNeeded(); 4510b57cec5SDimitry Andric OS << "#pragma clang module end /*" << M->getFullModuleName(true) << "*/"; 4520b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4530b57cec5SDimitry Andric } 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andric /// Ident - Handle #ident directives when read by the preprocessor. 4560b57cec5SDimitry Andric /// 4570b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, StringRef S) { 458349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 4590b57cec5SDimitry Andric 4600b57cec5SDimitry Andric OS.write("#ident ", strlen("#ident ")); 4610b57cec5SDimitry Andric OS.write(S.begin(), S.size()); 462349cc55cSDimitry Andric setEmittedTokensOnThisLine(); 4630b57cec5SDimitry Andric } 4640b57cec5SDimitry Andric 4650b57cec5SDimitry Andric /// MacroDefined - This hook is called whenever a macro definition is seen. 4660b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok, 4670b57cec5SDimitry Andric const MacroDirective *MD) { 4680b57cec5SDimitry Andric const MacroInfo *MI = MD->getMacroInfo(); 4690b57cec5SDimitry Andric // Only print out macro definitions in -dD mode. 4700b57cec5SDimitry Andric if (!DumpDefines || 4710b57cec5SDimitry Andric // Ignore __FILE__ etc. 4720b57cec5SDimitry Andric MI->isBuiltinMacro()) return; 4730b57cec5SDimitry Andric 474349cc55cSDimitry Andric MoveToLine(MI->getDefinitionLoc(), /*RequireStartOfLine=*/true); 4750b57cec5SDimitry Andric PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS); 4760b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4770b57cec5SDimitry Andric } 4780b57cec5SDimitry Andric 4790b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok, 4800b57cec5SDimitry Andric const MacroDefinition &MD, 4810b57cec5SDimitry Andric const MacroDirective *Undef) { 4820b57cec5SDimitry Andric // Only print out macro definitions in -dD mode. 4830b57cec5SDimitry Andric if (!DumpDefines) return; 4840b57cec5SDimitry Andric 485349cc55cSDimitry Andric MoveToLine(MacroNameTok.getLocation(), /*RequireStartOfLine=*/true); 4860b57cec5SDimitry Andric OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName(); 4870b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4880b57cec5SDimitry Andric } 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andric static void outputPrintable(raw_ostream &OS, StringRef Str) { 4910b57cec5SDimitry Andric for (unsigned char Char : Str) { 4920b57cec5SDimitry Andric if (isPrintable(Char) && Char != '\\' && Char != '"') 4930b57cec5SDimitry Andric OS << (char)Char; 4940b57cec5SDimitry Andric else // Output anything hard as an octal escape. 4950b57cec5SDimitry Andric OS << '\\' 4960b57cec5SDimitry Andric << (char)('0' + ((Char >> 6) & 7)) 4970b57cec5SDimitry Andric << (char)('0' + ((Char >> 3) & 7)) 4980b57cec5SDimitry Andric << (char)('0' + ((Char >> 0) & 7)); 4990b57cec5SDimitry Andric } 5000b57cec5SDimitry Andric } 5010b57cec5SDimitry Andric 5020b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc, 5030b57cec5SDimitry Andric StringRef Namespace, 5040b57cec5SDimitry Andric PragmaMessageKind Kind, 5050b57cec5SDimitry Andric StringRef Str) { 506349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 5070b57cec5SDimitry Andric OS << "#pragma "; 5080b57cec5SDimitry Andric if (!Namespace.empty()) 5090b57cec5SDimitry Andric OS << Namespace << ' '; 5100b57cec5SDimitry Andric switch (Kind) { 5110b57cec5SDimitry Andric case PMK_Message: 5120b57cec5SDimitry Andric OS << "message(\""; 5130b57cec5SDimitry Andric break; 5140b57cec5SDimitry Andric case PMK_Warning: 5150b57cec5SDimitry Andric OS << "warning \""; 5160b57cec5SDimitry Andric break; 5170b57cec5SDimitry Andric case PMK_Error: 5180b57cec5SDimitry Andric OS << "error \""; 5190b57cec5SDimitry Andric break; 5200b57cec5SDimitry Andric } 5210b57cec5SDimitry Andric 5220b57cec5SDimitry Andric outputPrintable(OS, Str); 5230b57cec5SDimitry Andric OS << '"'; 5240b57cec5SDimitry Andric if (Kind == PMK_Message) 5250b57cec5SDimitry Andric OS << ')'; 5260b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5270b57cec5SDimitry Andric } 5280b57cec5SDimitry Andric 5290b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaDebug(SourceLocation Loc, 5300b57cec5SDimitry Andric StringRef DebugType) { 531349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 5320b57cec5SDimitry Andric 5330b57cec5SDimitry Andric OS << "#pragma clang __debug "; 5340b57cec5SDimitry Andric OS << DebugType; 5350b57cec5SDimitry Andric 5360b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5370b57cec5SDimitry Andric } 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric void PrintPPOutputPPCallbacks:: 5400b57cec5SDimitry Andric PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) { 541349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 5420b57cec5SDimitry Andric OS << "#pragma " << Namespace << " diagnostic push"; 5430b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5440b57cec5SDimitry Andric } 5450b57cec5SDimitry Andric 5460b57cec5SDimitry Andric void PrintPPOutputPPCallbacks:: 5470b57cec5SDimitry Andric PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) { 548349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 5490b57cec5SDimitry Andric OS << "#pragma " << Namespace << " diagnostic pop"; 5500b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5510b57cec5SDimitry Andric } 5520b57cec5SDimitry Andric 5530b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaDiagnostic(SourceLocation Loc, 5540b57cec5SDimitry Andric StringRef Namespace, 5550b57cec5SDimitry Andric diag::Severity Map, 5560b57cec5SDimitry Andric StringRef Str) { 557349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 5580b57cec5SDimitry Andric OS << "#pragma " << Namespace << " diagnostic "; 5590b57cec5SDimitry Andric switch (Map) { 5600b57cec5SDimitry Andric case diag::Severity::Remark: 5610b57cec5SDimitry Andric OS << "remark"; 5620b57cec5SDimitry Andric break; 5630b57cec5SDimitry Andric case diag::Severity::Warning: 5640b57cec5SDimitry Andric OS << "warning"; 5650b57cec5SDimitry Andric break; 5660b57cec5SDimitry Andric case diag::Severity::Error: 5670b57cec5SDimitry Andric OS << "error"; 5680b57cec5SDimitry Andric break; 5690b57cec5SDimitry Andric case diag::Severity::Ignored: 5700b57cec5SDimitry Andric OS << "ignored"; 5710b57cec5SDimitry Andric break; 5720b57cec5SDimitry Andric case diag::Severity::Fatal: 5730b57cec5SDimitry Andric OS << "fatal"; 5740b57cec5SDimitry Andric break; 5750b57cec5SDimitry Andric } 5760b57cec5SDimitry Andric OS << " \"" << Str << '"'; 5770b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5780b57cec5SDimitry Andric } 5790b57cec5SDimitry Andric 5800b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaWarning(SourceLocation Loc, 581349cc55cSDimitry Andric PragmaWarningSpecifier WarningSpec, 5820b57cec5SDimitry Andric ArrayRef<int> Ids) { 583349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 584349cc55cSDimitry Andric 585349cc55cSDimitry Andric OS << "#pragma warning("; 586349cc55cSDimitry Andric switch(WarningSpec) { 587349cc55cSDimitry Andric case PWS_Default: OS << "default"; break; 588349cc55cSDimitry Andric case PWS_Disable: OS << "disable"; break; 589349cc55cSDimitry Andric case PWS_Error: OS << "error"; break; 590349cc55cSDimitry Andric case PWS_Once: OS << "once"; break; 591349cc55cSDimitry Andric case PWS_Suppress: OS << "suppress"; break; 592349cc55cSDimitry Andric case PWS_Level1: OS << '1'; break; 593349cc55cSDimitry Andric case PWS_Level2: OS << '2'; break; 594349cc55cSDimitry Andric case PWS_Level3: OS << '3'; break; 595349cc55cSDimitry Andric case PWS_Level4: OS << '4'; break; 596349cc55cSDimitry Andric } 597349cc55cSDimitry Andric OS << ':'; 598349cc55cSDimitry Andric 5990b57cec5SDimitry Andric for (ArrayRef<int>::iterator I = Ids.begin(), E = Ids.end(); I != E; ++I) 6000b57cec5SDimitry Andric OS << ' ' << *I; 6010b57cec5SDimitry Andric OS << ')'; 6020b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6030b57cec5SDimitry Andric } 6040b57cec5SDimitry Andric 6050b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaWarningPush(SourceLocation Loc, 6060b57cec5SDimitry Andric int Level) { 607349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6080b57cec5SDimitry Andric OS << "#pragma warning(push"; 6090b57cec5SDimitry Andric if (Level >= 0) 6100b57cec5SDimitry Andric OS << ", " << Level; 6110b57cec5SDimitry Andric OS << ')'; 6120b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6130b57cec5SDimitry Andric } 6140b57cec5SDimitry Andric 6150b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaWarningPop(SourceLocation Loc) { 616349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6170b57cec5SDimitry Andric OS << "#pragma warning(pop)"; 6180b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6190b57cec5SDimitry Andric } 6200b57cec5SDimitry Andric 6210b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaExecCharsetPush(SourceLocation Loc, 6220b57cec5SDimitry Andric StringRef Str) { 623349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6240b57cec5SDimitry Andric OS << "#pragma character_execution_set(push"; 6250b57cec5SDimitry Andric if (!Str.empty()) 6260b57cec5SDimitry Andric OS << ", " << Str; 6270b57cec5SDimitry Andric OS << ')'; 6280b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6290b57cec5SDimitry Andric } 6300b57cec5SDimitry Andric 6310b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaExecCharsetPop(SourceLocation Loc) { 632349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6330b57cec5SDimitry Andric OS << "#pragma character_execution_set(pop)"; 6340b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6350b57cec5SDimitry Andric } 6360b57cec5SDimitry Andric 6370b57cec5SDimitry Andric void PrintPPOutputPPCallbacks:: 6380b57cec5SDimitry Andric PragmaAssumeNonNullBegin(SourceLocation Loc) { 639349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6400b57cec5SDimitry Andric OS << "#pragma clang assume_nonnull begin"; 6410b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6420b57cec5SDimitry Andric } 6430b57cec5SDimitry Andric 6440b57cec5SDimitry Andric void PrintPPOutputPPCallbacks:: 6450b57cec5SDimitry Andric PragmaAssumeNonNullEnd(SourceLocation Loc) { 646349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 6470b57cec5SDimitry Andric OS << "#pragma clang assume_nonnull end"; 6480b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6490b57cec5SDimitry Andric } 6500b57cec5SDimitry Andric 651349cc55cSDimitry Andric void PrintPPOutputPPCallbacks::HandleWhitespaceBeforeTok(const Token &Tok, 652349cc55cSDimitry Andric bool RequireSpace, 653349cc55cSDimitry Andric bool RequireSameLine) { 654349cc55cSDimitry Andric // These tokens are not expanded to anything and don't need whitespace before 655349cc55cSDimitry Andric // them. 656349cc55cSDimitry Andric if (Tok.is(tok::eof) || 657349cc55cSDimitry Andric (Tok.isAnnotation() && !Tok.is(tok::annot_header_unit) && 658349cc55cSDimitry Andric !Tok.is(tok::annot_module_begin) && !Tok.is(tok::annot_module_end))) 659349cc55cSDimitry Andric return; 6600b57cec5SDimitry Andric 661349cc55cSDimitry Andric // EmittedDirectiveOnThisLine takes priority over RequireSameLine. 662349cc55cSDimitry Andric if ((!RequireSameLine || EmittedDirectiveOnThisLine) && 663349cc55cSDimitry Andric MoveToLine(Tok, /*RequireStartOfLine=*/EmittedDirectiveOnThisLine)) { 664349cc55cSDimitry Andric if (MinimizeWhitespace) { 665349cc55cSDimitry Andric // Avoid interpreting hash as a directive under -fpreprocessed. 666349cc55cSDimitry Andric if (Tok.is(tok::hash)) 667349cc55cSDimitry Andric OS << ' '; 668349cc55cSDimitry Andric } else { 6690b57cec5SDimitry Andric // Print out space characters so that the first token on a line is 6700b57cec5SDimitry Andric // indented for easy reading. 6710b57cec5SDimitry Andric unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation()); 6720b57cec5SDimitry Andric 673349cc55cSDimitry Andric // The first token on a line can have a column number of 1, yet still 674349cc55cSDimitry Andric // expect leading white space, if a macro expansion in column 1 starts 675349cc55cSDimitry Andric // with an empty macro argument, or an empty nested macro expansion. In 676349cc55cSDimitry Andric // this case, move the token to column 2. 6770b57cec5SDimitry Andric if (ColNo == 1 && Tok.hasLeadingSpace()) 6780b57cec5SDimitry Andric ColNo = 2; 6790b57cec5SDimitry Andric 6800b57cec5SDimitry Andric // This hack prevents stuff like: 6810b57cec5SDimitry Andric // #define HASH # 6820b57cec5SDimitry Andric // HASH define foo bar 6830b57cec5SDimitry Andric // From having the # character end up at column 1, which makes it so it 6840b57cec5SDimitry Andric // is not handled as a #define next time through the preprocessor if in 6850b57cec5SDimitry Andric // -fpreprocessed mode. 6860b57cec5SDimitry Andric if (ColNo <= 1 && Tok.is(tok::hash)) 6870b57cec5SDimitry Andric OS << ' '; 6880b57cec5SDimitry Andric 6890b57cec5SDimitry Andric // Otherwise, indent the appropriate number of spaces. 6900b57cec5SDimitry Andric for (; ColNo > 1; --ColNo) 6910b57cec5SDimitry Andric OS << ' '; 692349cc55cSDimitry Andric } 693349cc55cSDimitry Andric } else { 694349cc55cSDimitry Andric // Insert whitespace between the previous and next token if either 695349cc55cSDimitry Andric // - The caller requires it 696349cc55cSDimitry Andric // - The input had whitespace between them and we are not in 697349cc55cSDimitry Andric // whitespace-minimization mode 698349cc55cSDimitry Andric // - The whitespace is necessary to keep the tokens apart and there is not 699349cc55cSDimitry Andric // already a newline between them 700349cc55cSDimitry Andric if (RequireSpace || (!MinimizeWhitespace && Tok.hasLeadingSpace()) || 701349cc55cSDimitry Andric ((EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) && 702349cc55cSDimitry Andric AvoidConcat(PrevPrevTok, PrevTok, Tok))) 703349cc55cSDimitry Andric OS << ' '; 704349cc55cSDimitry Andric } 7050b57cec5SDimitry Andric 706349cc55cSDimitry Andric PrevPrevTok = PrevTok; 707349cc55cSDimitry Andric PrevTok = Tok; 7080b57cec5SDimitry Andric } 7090b57cec5SDimitry Andric 7100b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr, 7110b57cec5SDimitry Andric unsigned Len) { 7120b57cec5SDimitry Andric unsigned NumNewlines = 0; 7130b57cec5SDimitry Andric for (; Len; --Len, ++TokStr) { 7140b57cec5SDimitry Andric if (*TokStr != '\n' && 7150b57cec5SDimitry Andric *TokStr != '\r') 7160b57cec5SDimitry Andric continue; 7170b57cec5SDimitry Andric 7180b57cec5SDimitry Andric ++NumNewlines; 7190b57cec5SDimitry Andric 7200b57cec5SDimitry Andric // If we have \n\r or \r\n, skip both and count as one line. 7210b57cec5SDimitry Andric if (Len != 1 && 7220b57cec5SDimitry Andric (TokStr[1] == '\n' || TokStr[1] == '\r') && 7230b57cec5SDimitry Andric TokStr[0] != TokStr[1]) { 7240b57cec5SDimitry Andric ++TokStr; 7250b57cec5SDimitry Andric --Len; 7260b57cec5SDimitry Andric } 7270b57cec5SDimitry Andric } 7280b57cec5SDimitry Andric 7290b57cec5SDimitry Andric if (NumNewlines == 0) return; 7300b57cec5SDimitry Andric 7310b57cec5SDimitry Andric CurLine += NumNewlines; 7320b57cec5SDimitry Andric } 7330b57cec5SDimitry Andric 7340b57cec5SDimitry Andric 7350b57cec5SDimitry Andric namespace { 7360b57cec5SDimitry Andric struct UnknownPragmaHandler : public PragmaHandler { 7370b57cec5SDimitry Andric const char *Prefix; 7380b57cec5SDimitry Andric PrintPPOutputPPCallbacks *Callbacks; 7390b57cec5SDimitry Andric 7400b57cec5SDimitry Andric // Set to true if tokens should be expanded 7410b57cec5SDimitry Andric bool ShouldExpandTokens; 7420b57cec5SDimitry Andric 7430b57cec5SDimitry Andric UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks, 7440b57cec5SDimitry Andric bool RequireTokenExpansion) 7450b57cec5SDimitry Andric : Prefix(prefix), Callbacks(callbacks), 7460b57cec5SDimitry Andric ShouldExpandTokens(RequireTokenExpansion) {} 7470b57cec5SDimitry Andric void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 7480b57cec5SDimitry Andric Token &PragmaTok) override { 7490b57cec5SDimitry Andric // Figure out what line we went to and insert the appropriate number of 7500b57cec5SDimitry Andric // newline characters. 751349cc55cSDimitry Andric Callbacks->MoveToLine(PragmaTok.getLocation(), /*RequireStartOfLine=*/true); 7520b57cec5SDimitry Andric Callbacks->OS.write(Prefix, strlen(Prefix)); 753349cc55cSDimitry Andric Callbacks->setEmittedTokensOnThisLine(); 7540b57cec5SDimitry Andric 7550b57cec5SDimitry Andric if (ShouldExpandTokens) { 7560b57cec5SDimitry Andric // The first token does not have expanded macros. Expand them, if 7570b57cec5SDimitry Andric // required. 758a7dea167SDimitry Andric auto Toks = std::make_unique<Token[]>(1); 7590b57cec5SDimitry Andric Toks[0] = PragmaTok; 7600b57cec5SDimitry Andric PP.EnterTokenStream(std::move(Toks), /*NumToks=*/1, 7610b57cec5SDimitry Andric /*DisableMacroExpansion=*/false, 7620b57cec5SDimitry Andric /*IsReinject=*/false); 7630b57cec5SDimitry Andric PP.Lex(PragmaTok); 7640b57cec5SDimitry Andric } 7650b57cec5SDimitry Andric 7660b57cec5SDimitry Andric // Read and print all of the pragma tokens. 767349cc55cSDimitry Andric bool IsFirst = true; 7680b57cec5SDimitry Andric while (PragmaTok.isNot(tok::eod)) { 769349cc55cSDimitry Andric Callbacks->HandleWhitespaceBeforeTok(PragmaTok, /*RequireSpace=*/IsFirst, 770349cc55cSDimitry Andric /*RequireSameLine=*/true); 771349cc55cSDimitry Andric IsFirst = false; 7720b57cec5SDimitry Andric std::string TokSpell = PP.getSpelling(PragmaTok); 7730b57cec5SDimitry Andric Callbacks->OS.write(&TokSpell[0], TokSpell.size()); 774349cc55cSDimitry Andric Callbacks->setEmittedTokensOnThisLine(); 7750b57cec5SDimitry Andric 7760b57cec5SDimitry Andric if (ShouldExpandTokens) 7770b57cec5SDimitry Andric PP.Lex(PragmaTok); 7780b57cec5SDimitry Andric else 7790b57cec5SDimitry Andric PP.LexUnexpandedToken(PragmaTok); 7800b57cec5SDimitry Andric } 7810b57cec5SDimitry Andric Callbacks->setEmittedDirectiveOnThisLine(); 7820b57cec5SDimitry Andric } 7830b57cec5SDimitry Andric }; 7840b57cec5SDimitry Andric } // end anonymous namespace 7850b57cec5SDimitry Andric 7860b57cec5SDimitry Andric 7870b57cec5SDimitry Andric static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok, 7880b57cec5SDimitry Andric PrintPPOutputPPCallbacks *Callbacks, 7890b57cec5SDimitry Andric raw_ostream &OS) { 7900b57cec5SDimitry Andric bool DropComments = PP.getLangOpts().TraditionalCPP && 7910b57cec5SDimitry Andric !PP.getCommentRetentionState(); 7920b57cec5SDimitry Andric 793349cc55cSDimitry Andric bool IsStartOfLine = false; 7940b57cec5SDimitry Andric char Buffer[256]; 795*04eeddc0SDimitry Andric while (true) { 796349cc55cSDimitry Andric // Two lines joined with line continuation ('\' as last character on the 797349cc55cSDimitry Andric // line) must be emitted as one line even though Tok.getLine() returns two 798349cc55cSDimitry Andric // different values. In this situation Tok.isAtStartOfLine() is false even 799349cc55cSDimitry Andric // though it may be the first token on the lexical line. When 800349cc55cSDimitry Andric // dropping/skipping a token that is at the start of a line, propagate the 801349cc55cSDimitry Andric // start-of-line-ness to the next token to not append it to the previous 802349cc55cSDimitry Andric // line. 803349cc55cSDimitry Andric IsStartOfLine = IsStartOfLine || Tok.isAtStartOfLine(); 8040b57cec5SDimitry Andric 805349cc55cSDimitry Andric Callbacks->HandleWhitespaceBeforeTok(Tok, /*RequireSpace=*/false, 806349cc55cSDimitry Andric /*RequireSameLine=*/!IsStartOfLine); 8070b57cec5SDimitry Andric 8080b57cec5SDimitry Andric if (DropComments && Tok.is(tok::comment)) { 8090b57cec5SDimitry Andric // Skip comments. Normally the preprocessor does not generate 8100b57cec5SDimitry Andric // tok::comment nodes at all when not keeping comments, but under 8110b57cec5SDimitry Andric // -traditional-cpp the lexer keeps /all/ whitespace, including comments. 812349cc55cSDimitry Andric PP.Lex(Tok); 813349cc55cSDimitry Andric continue; 8140b57cec5SDimitry Andric } else if (Tok.is(tok::eod)) { 8150b57cec5SDimitry Andric // Don't print end of directive tokens, since they are typically newlines 8160b57cec5SDimitry Andric // that mess up our line tracking. These come from unknown pre-processor 8170b57cec5SDimitry Andric // directives or hash-prefixed comments in standalone assembly files. 8180b57cec5SDimitry Andric PP.Lex(Tok); 819349cc55cSDimitry Andric // FIXME: The token on the next line after #include should have 820349cc55cSDimitry Andric // Tok.isAtStartOfLine() set. 821349cc55cSDimitry Andric IsStartOfLine = true; 8220b57cec5SDimitry Andric continue; 8230b57cec5SDimitry Andric } else if (Tok.is(tok::annot_module_include)) { 8240b57cec5SDimitry Andric // PrintPPOutputPPCallbacks::InclusionDirective handles producing 8250b57cec5SDimitry Andric // appropriate output here. Ignore this token entirely. 8260b57cec5SDimitry Andric PP.Lex(Tok); 827349cc55cSDimitry Andric IsStartOfLine = true; 8280b57cec5SDimitry Andric continue; 8290b57cec5SDimitry Andric } else if (Tok.is(tok::annot_module_begin)) { 8300b57cec5SDimitry Andric // FIXME: We retrieve this token after the FileChanged callback, and 8310b57cec5SDimitry Andric // retrieve the module_end token before the FileChanged callback, so 8320b57cec5SDimitry Andric // we render this within the file and render the module end outside the 8330b57cec5SDimitry Andric // file, but this is backwards from the token locations: the module_begin 8340b57cec5SDimitry Andric // token is at the include location (outside the file) and the module_end 8350b57cec5SDimitry Andric // token is at the EOF location (within the file). 8360b57cec5SDimitry Andric Callbacks->BeginModule( 8370b57cec5SDimitry Andric reinterpret_cast<Module *>(Tok.getAnnotationValue())); 8380b57cec5SDimitry Andric PP.Lex(Tok); 839349cc55cSDimitry Andric IsStartOfLine = true; 8400b57cec5SDimitry Andric continue; 8410b57cec5SDimitry Andric } else if (Tok.is(tok::annot_module_end)) { 8420b57cec5SDimitry Andric Callbacks->EndModule( 8430b57cec5SDimitry Andric reinterpret_cast<Module *>(Tok.getAnnotationValue())); 8440b57cec5SDimitry Andric PP.Lex(Tok); 845349cc55cSDimitry Andric IsStartOfLine = true; 8460b57cec5SDimitry Andric continue; 8470b57cec5SDimitry Andric } else if (Tok.is(tok::annot_header_unit)) { 8480b57cec5SDimitry Andric // This is a header-name that has been (effectively) converted into a 8490b57cec5SDimitry Andric // module-name. 8500b57cec5SDimitry Andric // FIXME: The module name could contain non-identifier module name 8510b57cec5SDimitry Andric // components. We don't have a good way to round-trip those. 8520b57cec5SDimitry Andric Module *M = reinterpret_cast<Module *>(Tok.getAnnotationValue()); 8530b57cec5SDimitry Andric std::string Name = M->getFullModuleName(); 8540b57cec5SDimitry Andric OS.write(Name.data(), Name.size()); 8550b57cec5SDimitry Andric Callbacks->HandleNewlinesInToken(Name.data(), Name.size()); 8560b57cec5SDimitry Andric } else if (Tok.isAnnotation()) { 8570b57cec5SDimitry Andric // Ignore annotation tokens created by pragmas - the pragmas themselves 8580b57cec5SDimitry Andric // will be reproduced in the preprocessed output. 8590b57cec5SDimitry Andric PP.Lex(Tok); 8600b57cec5SDimitry Andric continue; 8610b57cec5SDimitry Andric } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) { 8620b57cec5SDimitry Andric OS << II->getName(); 8630b57cec5SDimitry Andric } else if (Tok.isLiteral() && !Tok.needsCleaning() && 8640b57cec5SDimitry Andric Tok.getLiteralData()) { 8650b57cec5SDimitry Andric OS.write(Tok.getLiteralData(), Tok.getLength()); 8660b57cec5SDimitry Andric } else if (Tok.getLength() < llvm::array_lengthof(Buffer)) { 8670b57cec5SDimitry Andric const char *TokPtr = Buffer; 8680b57cec5SDimitry Andric unsigned Len = PP.getSpelling(Tok, TokPtr); 8690b57cec5SDimitry Andric OS.write(TokPtr, Len); 8700b57cec5SDimitry Andric 8710b57cec5SDimitry Andric // Tokens that can contain embedded newlines need to adjust our current 8720b57cec5SDimitry Andric // line number. 873349cc55cSDimitry Andric // FIXME: The token may end with a newline in which case 874349cc55cSDimitry Andric // setEmittedDirectiveOnThisLine/setEmittedTokensOnThisLine afterwards is 875349cc55cSDimitry Andric // wrong. 8760b57cec5SDimitry Andric if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown) 8770b57cec5SDimitry Andric Callbacks->HandleNewlinesInToken(TokPtr, Len); 878349cc55cSDimitry Andric if (Tok.is(tok::comment) && Len >= 2 && TokPtr[0] == '/' && 879349cc55cSDimitry Andric TokPtr[1] == '/') { 880349cc55cSDimitry Andric // It's a line comment; 881349cc55cSDimitry Andric // Ensure that we don't concatenate anything behind it. 882349cc55cSDimitry Andric Callbacks->setEmittedDirectiveOnThisLine(); 883349cc55cSDimitry Andric } 8840b57cec5SDimitry Andric } else { 8850b57cec5SDimitry Andric std::string S = PP.getSpelling(Tok); 8860b57cec5SDimitry Andric OS.write(S.data(), S.size()); 8870b57cec5SDimitry Andric 8880b57cec5SDimitry Andric // Tokens that can contain embedded newlines need to adjust our current 8890b57cec5SDimitry Andric // line number. 8900b57cec5SDimitry Andric if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown) 8910b57cec5SDimitry Andric Callbacks->HandleNewlinesInToken(S.data(), S.size()); 892349cc55cSDimitry Andric if (Tok.is(tok::comment) && S.size() >= 2 && S[0] == '/' && S[1] == '/') { 893349cc55cSDimitry Andric // It's a line comment; 894349cc55cSDimitry Andric // Ensure that we don't concatenate anything behind it. 895349cc55cSDimitry Andric Callbacks->setEmittedDirectiveOnThisLine(); 896349cc55cSDimitry Andric } 8970b57cec5SDimitry Andric } 8980b57cec5SDimitry Andric Callbacks->setEmittedTokensOnThisLine(); 899349cc55cSDimitry Andric IsStartOfLine = false; 9000b57cec5SDimitry Andric 9010b57cec5SDimitry Andric if (Tok.is(tok::eof)) break; 9020b57cec5SDimitry Andric 9030b57cec5SDimitry Andric PP.Lex(Tok); 9040b57cec5SDimitry Andric } 9050b57cec5SDimitry Andric } 9060b57cec5SDimitry Andric 9070b57cec5SDimitry Andric typedef std::pair<const IdentifierInfo *, MacroInfo *> id_macro_pair; 9080b57cec5SDimitry Andric static int MacroIDCompare(const id_macro_pair *LHS, const id_macro_pair *RHS) { 9090b57cec5SDimitry Andric return LHS->first->getName().compare(RHS->first->getName()); 9100b57cec5SDimitry Andric } 9110b57cec5SDimitry Andric 9120b57cec5SDimitry Andric static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) { 9130b57cec5SDimitry Andric // Ignore unknown pragmas. 9140b57cec5SDimitry Andric PP.IgnorePragmas(); 9150b57cec5SDimitry Andric 9160b57cec5SDimitry Andric // -dM mode just scans and ignores all tokens in the files, then dumps out 9170b57cec5SDimitry Andric // the macro table at the end. 9180b57cec5SDimitry Andric PP.EnterMainSourceFile(); 9190b57cec5SDimitry Andric 9200b57cec5SDimitry Andric Token Tok; 9210b57cec5SDimitry Andric do PP.Lex(Tok); 9220b57cec5SDimitry Andric while (Tok.isNot(tok::eof)); 9230b57cec5SDimitry Andric 9240b57cec5SDimitry Andric SmallVector<id_macro_pair, 128> MacrosByID; 9250b57cec5SDimitry Andric for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); 9260b57cec5SDimitry Andric I != E; ++I) { 9270b57cec5SDimitry Andric auto *MD = I->second.getLatest(); 9280b57cec5SDimitry Andric if (MD && MD->isDefined()) 9290b57cec5SDimitry Andric MacrosByID.push_back(id_macro_pair(I->first, MD->getMacroInfo())); 9300b57cec5SDimitry Andric } 9310b57cec5SDimitry Andric llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare); 9320b57cec5SDimitry Andric 9330b57cec5SDimitry Andric for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) { 9340b57cec5SDimitry Andric MacroInfo &MI = *MacrosByID[i].second; 9350b57cec5SDimitry Andric // Ignore computed macros like __LINE__ and friends. 9360b57cec5SDimitry Andric if (MI.isBuiltinMacro()) continue; 9370b57cec5SDimitry Andric 9380b57cec5SDimitry Andric PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS); 9390b57cec5SDimitry Andric *OS << '\n'; 9400b57cec5SDimitry Andric } 9410b57cec5SDimitry Andric } 9420b57cec5SDimitry Andric 9430b57cec5SDimitry Andric /// DoPrintPreprocessedInput - This implements -E mode. 9440b57cec5SDimitry Andric /// 9450b57cec5SDimitry Andric void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, 9460b57cec5SDimitry Andric const PreprocessorOutputOptions &Opts) { 9470b57cec5SDimitry Andric // Show macros with no output is handled specially. 9480b57cec5SDimitry Andric if (!Opts.ShowCPP) { 9490b57cec5SDimitry Andric assert(Opts.ShowMacros && "Not yet implemented!"); 9500b57cec5SDimitry Andric DoPrintMacros(PP, OS); 9510b57cec5SDimitry Andric return; 9520b57cec5SDimitry Andric } 9530b57cec5SDimitry Andric 9540b57cec5SDimitry Andric // Inform the preprocessor whether we want it to retain comments or not, due 9550b57cec5SDimitry Andric // to -C or -CC. 9560b57cec5SDimitry Andric PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments); 9570b57cec5SDimitry Andric 9580b57cec5SDimitry Andric PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks( 9590b57cec5SDimitry Andric PP, *OS, !Opts.ShowLineMarkers, Opts.ShowMacros, 960349cc55cSDimitry Andric Opts.ShowIncludeDirectives, Opts.UseLineDirectives, 961349cc55cSDimitry Andric Opts.MinimizeWhitespace); 9620b57cec5SDimitry Andric 9630b57cec5SDimitry Andric // Expand macros in pragmas with -fms-extensions. The assumption is that 9640b57cec5SDimitry Andric // the majority of pragmas in such a file will be Microsoft pragmas. 9650b57cec5SDimitry Andric // Remember the handlers we will add so that we can remove them later. 9660b57cec5SDimitry Andric std::unique_ptr<UnknownPragmaHandler> MicrosoftExtHandler( 9670b57cec5SDimitry Andric new UnknownPragmaHandler( 9680b57cec5SDimitry Andric "#pragma", Callbacks, 9690b57cec5SDimitry Andric /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt)); 9700b57cec5SDimitry Andric 9710b57cec5SDimitry Andric std::unique_ptr<UnknownPragmaHandler> GCCHandler(new UnknownPragmaHandler( 9720b57cec5SDimitry Andric "#pragma GCC", Callbacks, 9730b57cec5SDimitry Andric /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt)); 9740b57cec5SDimitry Andric 9750b57cec5SDimitry Andric std::unique_ptr<UnknownPragmaHandler> ClangHandler(new UnknownPragmaHandler( 9760b57cec5SDimitry Andric "#pragma clang", Callbacks, 9770b57cec5SDimitry Andric /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt)); 9780b57cec5SDimitry Andric 9790b57cec5SDimitry Andric PP.AddPragmaHandler(MicrosoftExtHandler.get()); 9800b57cec5SDimitry Andric PP.AddPragmaHandler("GCC", GCCHandler.get()); 9810b57cec5SDimitry Andric PP.AddPragmaHandler("clang", ClangHandler.get()); 9820b57cec5SDimitry Andric 9830b57cec5SDimitry Andric // The tokens after pragma omp need to be expanded. 9840b57cec5SDimitry Andric // 9850b57cec5SDimitry Andric // OpenMP [2.1, Directive format] 9860b57cec5SDimitry Andric // Preprocessing tokens following the #pragma omp are subject to macro 9870b57cec5SDimitry Andric // replacement. 9880b57cec5SDimitry Andric std::unique_ptr<UnknownPragmaHandler> OpenMPHandler( 9890b57cec5SDimitry Andric new UnknownPragmaHandler("#pragma omp", Callbacks, 9900b57cec5SDimitry Andric /*RequireTokenExpansion=*/true)); 9910b57cec5SDimitry Andric PP.AddPragmaHandler("omp", OpenMPHandler.get()); 9920b57cec5SDimitry Andric 9930b57cec5SDimitry Andric PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks)); 9940b57cec5SDimitry Andric 9950b57cec5SDimitry Andric // After we have configured the preprocessor, enter the main file. 9960b57cec5SDimitry Andric PP.EnterMainSourceFile(); 9970b57cec5SDimitry Andric 9980b57cec5SDimitry Andric // Consume all of the tokens that come from the predefines buffer. Those 9990b57cec5SDimitry Andric // should not be emitted into the output and are guaranteed to be at the 10000b57cec5SDimitry Andric // start. 10010b57cec5SDimitry Andric const SourceManager &SourceMgr = PP.getSourceManager(); 10020b57cec5SDimitry Andric Token Tok; 10030b57cec5SDimitry Andric do { 10040b57cec5SDimitry Andric PP.Lex(Tok); 10050b57cec5SDimitry Andric if (Tok.is(tok::eof) || !Tok.getLocation().isFileID()) 10060b57cec5SDimitry Andric break; 10070b57cec5SDimitry Andric 10080b57cec5SDimitry Andric PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); 10090b57cec5SDimitry Andric if (PLoc.isInvalid()) 10100b57cec5SDimitry Andric break; 10110b57cec5SDimitry Andric 10120b57cec5SDimitry Andric if (strcmp(PLoc.getFilename(), "<built-in>")) 10130b57cec5SDimitry Andric break; 10140b57cec5SDimitry Andric } while (true); 10150b57cec5SDimitry Andric 10160b57cec5SDimitry Andric // Read all the preprocessed tokens, printing them out to the stream. 10170b57cec5SDimitry Andric PrintPreprocessedTokens(PP, Tok, Callbacks, *OS); 10180b57cec5SDimitry Andric *OS << '\n'; 10190b57cec5SDimitry Andric 10200b57cec5SDimitry Andric // Remove the handlers we just added to leave the preprocessor in a sane state 10210b57cec5SDimitry Andric // so that it can be reused (for example by a clang::Parser instance). 10220b57cec5SDimitry Andric PP.RemovePragmaHandler(MicrosoftExtHandler.get()); 10230b57cec5SDimitry Andric PP.RemovePragmaHandler("GCC", GCCHandler.get()); 10240b57cec5SDimitry Andric PP.RemovePragmaHandler("clang", ClangHandler.get()); 10250b57cec5SDimitry Andric PP.RemovePragmaHandler("omp", OpenMPHandler.get()); 10260b57cec5SDimitry Andric } 1027