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, 35*5f757f3fSDimitry Andric Preprocessor &PP, raw_ostream *OS) { 36*5f757f3fSDimitry Andric *OS << "#define " << II.getName(); 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric if (MI.isFunctionLike()) { 39*5f757f3fSDimitry 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) { 43*5f757f3fSDimitry Andric *OS << (*AI)->getName(); 44*5f757f3fSDimitry Andric *OS << ','; 450b57cec5SDimitry Andric } 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric // Last argument. 480b57cec5SDimitry Andric if ((*AI)->getName() == "__VA_ARGS__") 49*5f757f3fSDimitry Andric *OS << "..."; 500b57cec5SDimitry Andric else 51*5f757f3fSDimitry Andric *OS << (*AI)->getName(); 520b57cec5SDimitry Andric } 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric if (MI.isGNUVarargs()) 55*5f757f3fSDimitry Andric *OS << "..."; // #define foo(x...) 560b57cec5SDimitry Andric 57*5f757f3fSDimitry 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()) 63*5f757f3fSDimitry Andric *OS << ' '; 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric SmallString<128> SpellingBuffer; 660b57cec5SDimitry Andric for (const auto &T : MI.tokens()) { 670b57cec5SDimitry Andric if (T.hasLeadingSpace()) 68*5f757f3fSDimitry Andric *OS << ' '; 690b57cec5SDimitry Andric 70*5f757f3fSDimitry 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: 84*5f757f3fSDimitry 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; 9981ad6265SDimitry Andric bool DirectivesOnly; 100*5f757f3fSDimitry Andric bool KeepSystemIncludes; 101*5f757f3fSDimitry Andric raw_ostream *OrigOS; 102*5f757f3fSDimitry Andric std::unique_ptr<llvm::raw_null_ostream> NullOS; 103349cc55cSDimitry Andric 104349cc55cSDimitry Andric Token PrevTok; 105349cc55cSDimitry Andric Token PrevPrevTok; 106349cc55cSDimitry Andric 1070b57cec5SDimitry Andric public: 108*5f757f3fSDimitry Andric PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream *os, bool lineMarkers, 1090b57cec5SDimitry Andric bool defines, bool DumpIncludeDirectives, 11081ad6265SDimitry Andric bool UseLineDirectives, bool MinimizeWhitespace, 111*5f757f3fSDimitry Andric bool DirectivesOnly, bool KeepSystemIncludes) 1120b57cec5SDimitry Andric : PP(pp), SM(PP.getSourceManager()), ConcatInfo(PP), OS(os), 1130b57cec5SDimitry Andric DisableLineMarkers(lineMarkers), DumpDefines(defines), 1140b57cec5SDimitry Andric DumpIncludeDirectives(DumpIncludeDirectives), 115349cc55cSDimitry Andric UseLineDirectives(UseLineDirectives), 116*5f757f3fSDimitry Andric MinimizeWhitespace(MinimizeWhitespace), DirectivesOnly(DirectivesOnly), 117*5f757f3fSDimitry Andric KeepSystemIncludes(KeepSystemIncludes), OrigOS(os) { 1180b57cec5SDimitry Andric CurLine = 0; 1190b57cec5SDimitry Andric CurFilename += "<uninit>"; 1200b57cec5SDimitry Andric EmittedTokensOnThisLine = false; 1210b57cec5SDimitry Andric EmittedDirectiveOnThisLine = false; 1220b57cec5SDimitry Andric FileType = SrcMgr::C_User; 1230b57cec5SDimitry Andric Initialized = false; 1240b57cec5SDimitry Andric IsFirstFileEntered = false; 125*5f757f3fSDimitry Andric if (KeepSystemIncludes) 126*5f757f3fSDimitry Andric NullOS = std::make_unique<llvm::raw_null_ostream>(); 127349cc55cSDimitry Andric 128349cc55cSDimitry Andric PrevTok.startToken(); 129349cc55cSDimitry Andric PrevPrevTok.startToken(); 1300b57cec5SDimitry Andric } 1310b57cec5SDimitry Andric 132349cc55cSDimitry Andric bool isMinimizeWhitespace() const { return MinimizeWhitespace; } 133349cc55cSDimitry Andric 1340b57cec5SDimitry Andric void setEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; } 1350b57cec5SDimitry Andric bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; } 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric void setEmittedDirectiveOnThisLine() { EmittedDirectiveOnThisLine = true; } 1380b57cec5SDimitry Andric bool hasEmittedDirectiveOnThisLine() const { 1390b57cec5SDimitry Andric return EmittedDirectiveOnThisLine; 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric 142349cc55cSDimitry Andric /// Ensure that the output stream position is at the beginning of a new line 143349cc55cSDimitry Andric /// and inserts one if it does not. It is intended to ensure that directives 144349cc55cSDimitry Andric /// inserted by the directives not from the input source (such as #line) are 145349cc55cSDimitry Andric /// in the first column. To insert newlines that represent the input, use 146349cc55cSDimitry Andric /// MoveToLine(/*...*/, /*RequireStartOfLine=*/true). 147349cc55cSDimitry Andric void startNewLineIfNeeded(); 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric void FileChanged(SourceLocation Loc, FileChangeReason Reason, 1500b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType, 1510b57cec5SDimitry Andric FileID PrevFID) override; 1520b57cec5SDimitry Andric void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, 1530b57cec5SDimitry Andric StringRef FileName, bool IsAngled, 15481ad6265SDimitry Andric CharSourceRange FilenameRange, 155bdd1243dSDimitry Andric OptionalFileEntryRef File, StringRef SearchPath, 15681ad6265SDimitry Andric StringRef RelativePath, const Module *Imported, 1570b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType) override; 1580b57cec5SDimitry Andric void Ident(SourceLocation Loc, StringRef str) override; 1590b57cec5SDimitry Andric void PragmaMessage(SourceLocation Loc, StringRef Namespace, 1600b57cec5SDimitry Andric PragmaMessageKind Kind, StringRef Str) override; 1610b57cec5SDimitry Andric void PragmaDebug(SourceLocation Loc, StringRef DebugType) override; 1620b57cec5SDimitry Andric void PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) override; 1630b57cec5SDimitry Andric void PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) override; 1640b57cec5SDimitry Andric void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace, 1650b57cec5SDimitry Andric diag::Severity Map, StringRef Str) override; 166349cc55cSDimitry Andric void PragmaWarning(SourceLocation Loc, PragmaWarningSpecifier WarningSpec, 1670b57cec5SDimitry Andric ArrayRef<int> Ids) override; 1680b57cec5SDimitry Andric void PragmaWarningPush(SourceLocation Loc, int Level) override; 1690b57cec5SDimitry Andric void PragmaWarningPop(SourceLocation Loc) override; 1700b57cec5SDimitry Andric void PragmaExecCharsetPush(SourceLocation Loc, StringRef Str) override; 1710b57cec5SDimitry Andric void PragmaExecCharsetPop(SourceLocation Loc) override; 1720b57cec5SDimitry Andric void PragmaAssumeNonNullBegin(SourceLocation Loc) override; 1730b57cec5SDimitry Andric void PragmaAssumeNonNullEnd(SourceLocation Loc) override; 1740b57cec5SDimitry Andric 175349cc55cSDimitry Andric /// Insert whitespace before emitting the next token. 176349cc55cSDimitry Andric /// 177349cc55cSDimitry Andric /// @param Tok Next token to be emitted. 178349cc55cSDimitry Andric /// @param RequireSpace Ensure at least one whitespace is emitted. Useful 179349cc55cSDimitry Andric /// if non-tokens have been emitted to the stream. 180349cc55cSDimitry Andric /// @param RequireSameLine Never emit newlines. Useful when semantics depend 181349cc55cSDimitry Andric /// on being on the same line, such as directives. 182349cc55cSDimitry Andric void HandleWhitespaceBeforeTok(const Token &Tok, bool RequireSpace, 183349cc55cSDimitry Andric bool RequireSameLine); 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric /// Move to the line of the provided source location. This will 186349cc55cSDimitry Andric /// return true if a newline was inserted or if 187349cc55cSDimitry Andric /// the requested location is the first token on the first line. 188349cc55cSDimitry Andric /// In these cases the next output will be the first column on the line and 189349cc55cSDimitry Andric /// make it possible to insert indention. The newline was inserted 190349cc55cSDimitry Andric /// implicitly when at the beginning of the file. 191349cc55cSDimitry Andric /// 192349cc55cSDimitry Andric /// @param Tok Token where to move to. 193349cc55cSDimitry Andric /// @param RequireStartOfLine Whether the next line depends on being in the 194349cc55cSDimitry Andric /// first column, such as a directive. 195349cc55cSDimitry Andric /// 196349cc55cSDimitry Andric /// @return Whether column adjustments are necessary. 197349cc55cSDimitry Andric bool MoveToLine(const Token &Tok, bool RequireStartOfLine) { 198349cc55cSDimitry Andric PresumedLoc PLoc = SM.getPresumedLoc(Tok.getLocation()); 199349cc55cSDimitry Andric unsigned TargetLine = PLoc.isValid() ? PLoc.getLine() : CurLine; 20081ad6265SDimitry Andric bool IsFirstInFile = 20181ad6265SDimitry Andric Tok.isAtStartOfLine() && PLoc.isValid() && PLoc.getLine() == 1; 202349cc55cSDimitry Andric return MoveToLine(TargetLine, RequireStartOfLine) || IsFirstInFile; 2030b57cec5SDimitry Andric } 204349cc55cSDimitry Andric 205349cc55cSDimitry Andric /// Move to the line of the provided source location. Returns true if a new 206349cc55cSDimitry Andric /// line was inserted. 207349cc55cSDimitry Andric bool MoveToLine(SourceLocation Loc, bool RequireStartOfLine) { 208349cc55cSDimitry Andric PresumedLoc PLoc = SM.getPresumedLoc(Loc); 209349cc55cSDimitry Andric unsigned TargetLine = PLoc.isValid() ? PLoc.getLine() : CurLine; 210349cc55cSDimitry Andric return MoveToLine(TargetLine, RequireStartOfLine); 211349cc55cSDimitry Andric } 212349cc55cSDimitry Andric bool MoveToLine(unsigned LineNo, bool RequireStartOfLine); 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok, 2150b57cec5SDimitry Andric const Token &Tok) { 2160b57cec5SDimitry Andric return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok); 2170b57cec5SDimitry Andric } 2180b57cec5SDimitry Andric void WriteLineInfo(unsigned LineNo, const char *Extra=nullptr, 2190b57cec5SDimitry Andric unsigned ExtraLen=0); 2200b57cec5SDimitry Andric bool LineMarkersAreDisabled() const { return DisableLineMarkers; } 2210b57cec5SDimitry Andric void HandleNewlinesInToken(const char *TokStr, unsigned Len); 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric /// MacroDefined - This hook is called whenever a macro definition is seen. 2240b57cec5SDimitry Andric void MacroDefined(const Token &MacroNameTok, 2250b57cec5SDimitry Andric const MacroDirective *MD) override; 2260b57cec5SDimitry Andric 2270b57cec5SDimitry Andric /// MacroUndefined - This hook is called whenever a macro #undef is seen. 2280b57cec5SDimitry Andric void MacroUndefined(const Token &MacroNameTok, 2290b57cec5SDimitry Andric const MacroDefinition &MD, 2300b57cec5SDimitry Andric const MacroDirective *Undef) override; 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric void BeginModule(const Module *M); 2330b57cec5SDimitry Andric void EndModule(const Module *M); 2340b57cec5SDimitry Andric }; 2350b57cec5SDimitry Andric } // end anonymous namespace 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo, 2380b57cec5SDimitry Andric const char *Extra, 2390b57cec5SDimitry Andric unsigned ExtraLen) { 240349cc55cSDimitry Andric startNewLineIfNeeded(); 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric // Emit #line directives or GNU line markers depending on what mode we're in. 2430b57cec5SDimitry Andric if (UseLineDirectives) { 244*5f757f3fSDimitry Andric *OS << "#line" << ' ' << LineNo << ' ' << '"'; 245*5f757f3fSDimitry Andric OS->write_escaped(CurFilename); 246*5f757f3fSDimitry Andric *OS << '"'; 2470b57cec5SDimitry Andric } else { 248*5f757f3fSDimitry Andric *OS << '#' << ' ' << LineNo << ' ' << '"'; 249*5f757f3fSDimitry Andric OS->write_escaped(CurFilename); 250*5f757f3fSDimitry Andric *OS << '"'; 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andric if (ExtraLen) 253*5f757f3fSDimitry Andric OS->write(Extra, ExtraLen); 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric if (FileType == SrcMgr::C_System) 256*5f757f3fSDimitry Andric OS->write(" 3", 2); 2570b57cec5SDimitry Andric else if (FileType == SrcMgr::C_ExternCSystem) 258*5f757f3fSDimitry Andric OS->write(" 3 4", 4); 2590b57cec5SDimitry Andric } 260*5f757f3fSDimitry Andric *OS << '\n'; 2610b57cec5SDimitry Andric } 2620b57cec5SDimitry Andric 2630b57cec5SDimitry Andric /// MoveToLine - Move the output to the source line specified by the location 2640b57cec5SDimitry Andric /// object. We can do this by emitting some number of \n's, or be emitting a 2650b57cec5SDimitry Andric /// #line directive. This returns false if already at the specified line, true 2660b57cec5SDimitry Andric /// if some newlines were emitted. 267349cc55cSDimitry Andric bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo, 268349cc55cSDimitry Andric bool RequireStartOfLine) { 269349cc55cSDimitry Andric // If it is required to start a new line or finish the current, insert 270349cc55cSDimitry Andric // vertical whitespace now and take it into account when moving to the 271349cc55cSDimitry Andric // expected line. 272349cc55cSDimitry Andric bool StartedNewLine = false; 273349cc55cSDimitry Andric if ((RequireStartOfLine && EmittedTokensOnThisLine) || 274349cc55cSDimitry Andric EmittedDirectiveOnThisLine) { 275*5f757f3fSDimitry Andric *OS << '\n'; 276349cc55cSDimitry Andric StartedNewLine = true; 277349cc55cSDimitry Andric CurLine += 1; 278349cc55cSDimitry Andric EmittedTokensOnThisLine = false; 279349cc55cSDimitry Andric EmittedDirectiveOnThisLine = false; 280349cc55cSDimitry Andric } 281349cc55cSDimitry Andric 2820b57cec5SDimitry Andric // If this line is "close enough" to the original line, just print newlines, 2830b57cec5SDimitry Andric // otherwise print a #line directive. 284349cc55cSDimitry Andric if (CurLine == LineNo) { 285349cc55cSDimitry Andric // Nothing to do if we are already on the correct line. 286349cc55cSDimitry Andric } else if (MinimizeWhitespace && DisableLineMarkers) { 287349cc55cSDimitry Andric // With -E -P -fminimize-whitespace, don't emit anything if not necessary. 288349cc55cSDimitry Andric } else if (!StartedNewLine && LineNo - CurLine == 1) { 289349cc55cSDimitry Andric // Printing a single line has priority over printing a #line directive, even 290349cc55cSDimitry Andric // when minimizing whitespace which otherwise would print #line directives 291349cc55cSDimitry Andric // for every single line. 292*5f757f3fSDimitry Andric *OS << '\n'; 293349cc55cSDimitry Andric StartedNewLine = true; 294349cc55cSDimitry Andric } else if (!DisableLineMarkers) { 295349cc55cSDimitry Andric if (LineNo - CurLine <= 8) { 2960b57cec5SDimitry Andric const char *NewLines = "\n\n\n\n\n\n\n\n"; 297*5f757f3fSDimitry Andric OS->write(NewLines, LineNo - CurLine); 298349cc55cSDimitry Andric } else { 2990b57cec5SDimitry Andric // Emit a #line or line marker. 3000b57cec5SDimitry Andric WriteLineInfo(LineNo, nullptr, 0); 301349cc55cSDimitry Andric } 302349cc55cSDimitry Andric StartedNewLine = true; 303349cc55cSDimitry Andric } else if (EmittedTokensOnThisLine) { 304349cc55cSDimitry Andric // If we are not on the correct line and don't need to be line-correct, 305349cc55cSDimitry Andric // at least ensure we start on a new line. 306*5f757f3fSDimitry Andric *OS << '\n'; 307349cc55cSDimitry Andric StartedNewLine = true; 308349cc55cSDimitry Andric } 309349cc55cSDimitry Andric 310349cc55cSDimitry Andric if (StartedNewLine) { 311349cc55cSDimitry Andric EmittedTokensOnThisLine = false; 312349cc55cSDimitry Andric EmittedDirectiveOnThisLine = false; 3130b57cec5SDimitry Andric } 3140b57cec5SDimitry Andric 3150b57cec5SDimitry Andric CurLine = LineNo; 316349cc55cSDimitry Andric return StartedNewLine; 3170b57cec5SDimitry Andric } 3180b57cec5SDimitry Andric 319349cc55cSDimitry Andric void PrintPPOutputPPCallbacks::startNewLineIfNeeded() { 3200b57cec5SDimitry Andric if (EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) { 321*5f757f3fSDimitry Andric *OS << '\n'; 3220b57cec5SDimitry Andric EmittedTokensOnThisLine = false; 3230b57cec5SDimitry Andric EmittedDirectiveOnThisLine = false; 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric } 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric /// FileChanged - Whenever the preprocessor enters or exits a #include file 3280b57cec5SDimitry Andric /// it invokes this handler. Update our conception of the current source 3290b57cec5SDimitry Andric /// position. 3300b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc, 3310b57cec5SDimitry Andric FileChangeReason Reason, 3320b57cec5SDimitry Andric SrcMgr::CharacteristicKind NewFileType, 3330b57cec5SDimitry Andric FileID PrevFID) { 3340b57cec5SDimitry Andric // Unless we are exiting a #include, make sure to skip ahead to the line the 3350b57cec5SDimitry Andric // #include directive was at. 3360b57cec5SDimitry Andric SourceManager &SourceMgr = SM; 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andric PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc); 3390b57cec5SDimitry Andric if (UserLoc.isInvalid()) 3400b57cec5SDimitry Andric return; 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric unsigned NewLine = UserLoc.getLine(); 3430b57cec5SDimitry Andric 3440b57cec5SDimitry Andric if (Reason == PPCallbacks::EnterFile) { 3450b57cec5SDimitry Andric SourceLocation IncludeLoc = UserLoc.getIncludeLoc(); 3460b57cec5SDimitry Andric if (IncludeLoc.isValid()) 347349cc55cSDimitry Andric MoveToLine(IncludeLoc, /*RequireStartOfLine=*/false); 3480b57cec5SDimitry Andric } else if (Reason == PPCallbacks::SystemHeaderPragma) { 3490b57cec5SDimitry Andric // GCC emits the # directive for this directive on the line AFTER the 3500b57cec5SDimitry Andric // directive and emits a bunch of spaces that aren't needed. This is because 3510b57cec5SDimitry Andric // otherwise we will emit a line marker for THIS line, which requires an 3520b57cec5SDimitry Andric // extra blank line after the directive to avoid making all following lines 3530b57cec5SDimitry Andric // off by one. We can do better by simply incrementing NewLine here. 3540b57cec5SDimitry Andric NewLine += 1; 3550b57cec5SDimitry Andric } 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric CurLine = NewLine; 3580b57cec5SDimitry Andric 359*5f757f3fSDimitry Andric // In KeepSystemIncludes mode, redirect OS as needed. 360*5f757f3fSDimitry Andric if (KeepSystemIncludes && (isSystem(FileType) != isSystem(NewFileType))) 361*5f757f3fSDimitry Andric OS = isSystem(FileType) ? OrigOS : NullOS.get(); 362*5f757f3fSDimitry Andric 3630b57cec5SDimitry Andric CurFilename.clear(); 3640b57cec5SDimitry Andric CurFilename += UserLoc.getFilename(); 3650b57cec5SDimitry Andric FileType = NewFileType; 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric if (DisableLineMarkers) { 368349cc55cSDimitry Andric if (!MinimizeWhitespace) 369349cc55cSDimitry Andric startNewLineIfNeeded(); 3700b57cec5SDimitry Andric return; 3710b57cec5SDimitry Andric } 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric if (!Initialized) { 3740b57cec5SDimitry Andric WriteLineInfo(CurLine); 3750b57cec5SDimitry Andric Initialized = true; 3760b57cec5SDimitry Andric } 3770b57cec5SDimitry Andric 3780b57cec5SDimitry Andric // Do not emit an enter marker for the main file (which we expect is the first 3790b57cec5SDimitry Andric // entered file). This matches gcc, and improves compatibility with some tools 3800b57cec5SDimitry Andric // which track the # line markers as a way to determine when the preprocessed 3810b57cec5SDimitry Andric // output is in the context of the main file. 3820b57cec5SDimitry Andric if (Reason == PPCallbacks::EnterFile && !IsFirstFileEntered) { 3830b57cec5SDimitry Andric IsFirstFileEntered = true; 3840b57cec5SDimitry Andric return; 3850b57cec5SDimitry Andric } 3860b57cec5SDimitry Andric 3870b57cec5SDimitry Andric switch (Reason) { 3880b57cec5SDimitry Andric case PPCallbacks::EnterFile: 3890b57cec5SDimitry Andric WriteLineInfo(CurLine, " 1", 2); 3900b57cec5SDimitry Andric break; 3910b57cec5SDimitry Andric case PPCallbacks::ExitFile: 3920b57cec5SDimitry Andric WriteLineInfo(CurLine, " 2", 2); 3930b57cec5SDimitry Andric break; 3940b57cec5SDimitry Andric case PPCallbacks::SystemHeaderPragma: 3950b57cec5SDimitry Andric case PPCallbacks::RenameFile: 3960b57cec5SDimitry Andric WriteLineInfo(CurLine); 3970b57cec5SDimitry Andric break; 3980b57cec5SDimitry Andric } 3990b57cec5SDimitry Andric } 4000b57cec5SDimitry Andric 4010b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::InclusionDirective( 402bdd1243dSDimitry Andric SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, 403bdd1243dSDimitry Andric bool IsAngled, CharSourceRange FilenameRange, OptionalFileEntryRef File, 404bdd1243dSDimitry Andric StringRef SearchPath, StringRef RelativePath, const Module *Imported, 4050b57cec5SDimitry Andric SrcMgr::CharacteristicKind FileType) { 4060b57cec5SDimitry Andric // In -dI mode, dump #include directives prior to dumping their content or 407*5f757f3fSDimitry Andric // interpretation. Similar for -fkeep-system-includes. 408*5f757f3fSDimitry Andric if (DumpIncludeDirectives || (KeepSystemIncludes && isSystem(FileType))) { 409349cc55cSDimitry Andric MoveToLine(HashLoc, /*RequireStartOfLine=*/true); 4100b57cec5SDimitry Andric const std::string TokenText = PP.getSpelling(IncludeTok); 4110b57cec5SDimitry Andric assert(!TokenText.empty()); 412*5f757f3fSDimitry Andric *OS << "#" << TokenText << " " 4130b57cec5SDimitry Andric << (IsAngled ? '<' : '"') << FileName << (IsAngled ? '>' : '"') 414*5f757f3fSDimitry Andric << " /* clang -E " 415*5f757f3fSDimitry Andric << (DumpIncludeDirectives ? "-dI" : "-fkeep-system-includes") 416*5f757f3fSDimitry Andric << " */"; 4170b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4180b57cec5SDimitry Andric } 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric // When preprocessing, turn implicit imports into module import pragmas. 4210b57cec5SDimitry Andric if (Imported) { 4220b57cec5SDimitry Andric switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) { 4230b57cec5SDimitry Andric case tok::pp_include: 4240b57cec5SDimitry Andric case tok::pp_import: 4250b57cec5SDimitry Andric case tok::pp_include_next: 426349cc55cSDimitry Andric MoveToLine(HashLoc, /*RequireStartOfLine=*/true); 427*5f757f3fSDimitry Andric *OS << "#pragma clang module import " 428*5f757f3fSDimitry Andric << Imported->getFullModuleName(true) 4290b57cec5SDimitry Andric << " /* clang -E: implicit import for " 4300b57cec5SDimitry Andric << "#" << PP.getSpelling(IncludeTok) << " " 4310b57cec5SDimitry Andric << (IsAngled ? '<' : '"') << FileName << (IsAngled ? '>' : '"') 4320b57cec5SDimitry Andric << " */"; 433349cc55cSDimitry Andric setEmittedDirectiveOnThisLine(); 4340b57cec5SDimitry Andric break; 4350b57cec5SDimitry Andric 4360b57cec5SDimitry Andric case tok::pp___include_macros: 4370b57cec5SDimitry Andric // #__include_macros has no effect on a user of a preprocessed source 4380b57cec5SDimitry Andric // file; the only effect is on preprocessing. 4390b57cec5SDimitry Andric // 4400b57cec5SDimitry Andric // FIXME: That's not *quite* true: it causes the module in question to 4410b57cec5SDimitry Andric // be loaded, which can affect downstream diagnostics. 4420b57cec5SDimitry Andric break; 4430b57cec5SDimitry Andric 4440b57cec5SDimitry Andric default: 4450b57cec5SDimitry Andric llvm_unreachable("unknown include directive kind"); 4460b57cec5SDimitry Andric break; 4470b57cec5SDimitry Andric } 4480b57cec5SDimitry Andric } 4490b57cec5SDimitry Andric } 4500b57cec5SDimitry Andric 4510b57cec5SDimitry Andric /// Handle entering the scope of a module during a module compilation. 4520b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::BeginModule(const Module *M) { 4530b57cec5SDimitry Andric startNewLineIfNeeded(); 454*5f757f3fSDimitry Andric *OS << "#pragma clang module begin " << M->getFullModuleName(true); 4550b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4560b57cec5SDimitry Andric } 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric /// Handle leaving the scope of a module during a module compilation. 4590b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::EndModule(const Module *M) { 4600b57cec5SDimitry Andric startNewLineIfNeeded(); 461*5f757f3fSDimitry Andric *OS << "#pragma clang module end /*" << M->getFullModuleName(true) << "*/"; 4620b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4630b57cec5SDimitry Andric } 4640b57cec5SDimitry Andric 4650b57cec5SDimitry Andric /// Ident - Handle #ident directives when read by the preprocessor. 4660b57cec5SDimitry Andric /// 4670b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, StringRef S) { 468349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 4690b57cec5SDimitry Andric 470*5f757f3fSDimitry Andric OS->write("#ident ", strlen("#ident ")); 471*5f757f3fSDimitry Andric OS->write(S.begin(), S.size()); 472349cc55cSDimitry Andric setEmittedTokensOnThisLine(); 4730b57cec5SDimitry Andric } 4740b57cec5SDimitry Andric 4750b57cec5SDimitry Andric /// MacroDefined - This hook is called whenever a macro definition is seen. 4760b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok, 4770b57cec5SDimitry Andric const MacroDirective *MD) { 4780b57cec5SDimitry Andric const MacroInfo *MI = MD->getMacroInfo(); 47981ad6265SDimitry Andric // Print out macro definitions in -dD mode and when we have -fdirectives-only 48081ad6265SDimitry Andric // for C++20 header units. 48181ad6265SDimitry Andric if ((!DumpDefines && !DirectivesOnly) || 4820b57cec5SDimitry Andric // Ignore __FILE__ etc. 48381ad6265SDimitry Andric MI->isBuiltinMacro()) 48481ad6265SDimitry Andric return; 4850b57cec5SDimitry Andric 48681ad6265SDimitry Andric SourceLocation DefLoc = MI->getDefinitionLoc(); 48781ad6265SDimitry Andric if (DirectivesOnly && !MI->isUsed()) { 48881ad6265SDimitry Andric SourceManager &SM = PP.getSourceManager(); 48981ad6265SDimitry Andric if (SM.isWrittenInBuiltinFile(DefLoc) || 49081ad6265SDimitry Andric SM.isWrittenInCommandLineFile(DefLoc)) 49181ad6265SDimitry Andric return; 49281ad6265SDimitry Andric } 49381ad6265SDimitry Andric MoveToLine(DefLoc, /*RequireStartOfLine=*/true); 4940b57cec5SDimitry Andric PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS); 4950b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 4960b57cec5SDimitry Andric } 4970b57cec5SDimitry Andric 4980b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok, 4990b57cec5SDimitry Andric const MacroDefinition &MD, 5000b57cec5SDimitry Andric const MacroDirective *Undef) { 50181ad6265SDimitry Andric // Print out macro definitions in -dD mode and when we have -fdirectives-only 50281ad6265SDimitry Andric // for C++20 header units. 50381ad6265SDimitry Andric if (!DumpDefines && !DirectivesOnly) 50481ad6265SDimitry Andric return; 5050b57cec5SDimitry Andric 506349cc55cSDimitry Andric MoveToLine(MacroNameTok.getLocation(), /*RequireStartOfLine=*/true); 507*5f757f3fSDimitry Andric *OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName(); 5080b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5090b57cec5SDimitry Andric } 5100b57cec5SDimitry Andric 511*5f757f3fSDimitry Andric static void outputPrintable(raw_ostream *OS, StringRef Str) { 5120b57cec5SDimitry Andric for (unsigned char Char : Str) { 5130b57cec5SDimitry Andric if (isPrintable(Char) && Char != '\\' && Char != '"') 514*5f757f3fSDimitry Andric *OS << (char)Char; 5150b57cec5SDimitry Andric else // Output anything hard as an octal escape. 516*5f757f3fSDimitry Andric *OS << '\\' 5170b57cec5SDimitry Andric << (char)('0' + ((Char >> 6) & 7)) 5180b57cec5SDimitry Andric << (char)('0' + ((Char >> 3) & 7)) 5190b57cec5SDimitry Andric << (char)('0' + ((Char >> 0) & 7)); 5200b57cec5SDimitry Andric } 5210b57cec5SDimitry Andric } 5220b57cec5SDimitry Andric 5230b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc, 5240b57cec5SDimitry Andric StringRef Namespace, 5250b57cec5SDimitry Andric PragmaMessageKind Kind, 5260b57cec5SDimitry Andric StringRef Str) { 527349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 528*5f757f3fSDimitry Andric *OS << "#pragma "; 5290b57cec5SDimitry Andric if (!Namespace.empty()) 530*5f757f3fSDimitry Andric *OS << Namespace << ' '; 5310b57cec5SDimitry Andric switch (Kind) { 5320b57cec5SDimitry Andric case PMK_Message: 533*5f757f3fSDimitry Andric *OS << "message(\""; 5340b57cec5SDimitry Andric break; 5350b57cec5SDimitry Andric case PMK_Warning: 536*5f757f3fSDimitry Andric *OS << "warning \""; 5370b57cec5SDimitry Andric break; 5380b57cec5SDimitry Andric case PMK_Error: 539*5f757f3fSDimitry Andric *OS << "error \""; 5400b57cec5SDimitry Andric break; 5410b57cec5SDimitry Andric } 5420b57cec5SDimitry Andric 5430b57cec5SDimitry Andric outputPrintable(OS, Str); 544*5f757f3fSDimitry Andric *OS << '"'; 5450b57cec5SDimitry Andric if (Kind == PMK_Message) 546*5f757f3fSDimitry Andric *OS << ')'; 5470b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5480b57cec5SDimitry Andric } 5490b57cec5SDimitry Andric 5500b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaDebug(SourceLocation Loc, 5510b57cec5SDimitry Andric StringRef DebugType) { 552349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 5530b57cec5SDimitry Andric 554*5f757f3fSDimitry Andric *OS << "#pragma clang __debug "; 555*5f757f3fSDimitry Andric *OS << DebugType; 5560b57cec5SDimitry Andric 5570b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5580b57cec5SDimitry Andric } 5590b57cec5SDimitry Andric 5600b57cec5SDimitry Andric void PrintPPOutputPPCallbacks:: 5610b57cec5SDimitry Andric PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) { 562349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 563*5f757f3fSDimitry Andric *OS << "#pragma " << Namespace << " diagnostic push"; 5640b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5650b57cec5SDimitry Andric } 5660b57cec5SDimitry Andric 5670b57cec5SDimitry Andric void PrintPPOutputPPCallbacks:: 5680b57cec5SDimitry Andric PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) { 569349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 570*5f757f3fSDimitry Andric *OS << "#pragma " << Namespace << " diagnostic pop"; 5710b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5720b57cec5SDimitry Andric } 5730b57cec5SDimitry Andric 5740b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaDiagnostic(SourceLocation Loc, 5750b57cec5SDimitry Andric StringRef Namespace, 5760b57cec5SDimitry Andric diag::Severity Map, 5770b57cec5SDimitry Andric StringRef Str) { 578349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 579*5f757f3fSDimitry Andric *OS << "#pragma " << Namespace << " diagnostic "; 5800b57cec5SDimitry Andric switch (Map) { 5810b57cec5SDimitry Andric case diag::Severity::Remark: 582*5f757f3fSDimitry Andric *OS << "remark"; 5830b57cec5SDimitry Andric break; 5840b57cec5SDimitry Andric case diag::Severity::Warning: 585*5f757f3fSDimitry Andric *OS << "warning"; 5860b57cec5SDimitry Andric break; 5870b57cec5SDimitry Andric case diag::Severity::Error: 588*5f757f3fSDimitry Andric *OS << "error"; 5890b57cec5SDimitry Andric break; 5900b57cec5SDimitry Andric case diag::Severity::Ignored: 591*5f757f3fSDimitry Andric *OS << "ignored"; 5920b57cec5SDimitry Andric break; 5930b57cec5SDimitry Andric case diag::Severity::Fatal: 594*5f757f3fSDimitry Andric *OS << "fatal"; 5950b57cec5SDimitry Andric break; 5960b57cec5SDimitry Andric } 597*5f757f3fSDimitry Andric *OS << " \"" << Str << '"'; 5980b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 5990b57cec5SDimitry Andric } 6000b57cec5SDimitry Andric 6010b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaWarning(SourceLocation Loc, 602349cc55cSDimitry Andric PragmaWarningSpecifier WarningSpec, 6030b57cec5SDimitry Andric ArrayRef<int> Ids) { 604349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 605349cc55cSDimitry Andric 606*5f757f3fSDimitry Andric *OS << "#pragma warning("; 607349cc55cSDimitry Andric switch(WarningSpec) { 608*5f757f3fSDimitry Andric case PWS_Default: *OS << "default"; break; 609*5f757f3fSDimitry Andric case PWS_Disable: *OS << "disable"; break; 610*5f757f3fSDimitry Andric case PWS_Error: *OS << "error"; break; 611*5f757f3fSDimitry Andric case PWS_Once: *OS << "once"; break; 612*5f757f3fSDimitry Andric case PWS_Suppress: *OS << "suppress"; break; 613*5f757f3fSDimitry Andric case PWS_Level1: *OS << '1'; break; 614*5f757f3fSDimitry Andric case PWS_Level2: *OS << '2'; break; 615*5f757f3fSDimitry Andric case PWS_Level3: *OS << '3'; break; 616*5f757f3fSDimitry Andric case PWS_Level4: *OS << '4'; break; 617349cc55cSDimitry Andric } 618*5f757f3fSDimitry Andric *OS << ':'; 619349cc55cSDimitry Andric 6200b57cec5SDimitry Andric for (ArrayRef<int>::iterator I = Ids.begin(), E = Ids.end(); I != E; ++I) 621*5f757f3fSDimitry Andric *OS << ' ' << *I; 622*5f757f3fSDimitry Andric *OS << ')'; 6230b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6240b57cec5SDimitry Andric } 6250b57cec5SDimitry Andric 6260b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaWarningPush(SourceLocation Loc, 6270b57cec5SDimitry Andric int Level) { 628349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 629*5f757f3fSDimitry Andric *OS << "#pragma warning(push"; 6300b57cec5SDimitry Andric if (Level >= 0) 631*5f757f3fSDimitry Andric *OS << ", " << Level; 632*5f757f3fSDimitry Andric *OS << ')'; 6330b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6340b57cec5SDimitry Andric } 6350b57cec5SDimitry Andric 6360b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaWarningPop(SourceLocation Loc) { 637349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 638*5f757f3fSDimitry Andric *OS << "#pragma warning(pop)"; 6390b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6400b57cec5SDimitry Andric } 6410b57cec5SDimitry Andric 6420b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaExecCharsetPush(SourceLocation Loc, 6430b57cec5SDimitry Andric StringRef Str) { 644349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 645*5f757f3fSDimitry Andric *OS << "#pragma character_execution_set(push"; 6460b57cec5SDimitry Andric if (!Str.empty()) 647*5f757f3fSDimitry Andric *OS << ", " << Str; 648*5f757f3fSDimitry Andric *OS << ')'; 6490b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6500b57cec5SDimitry Andric } 6510b57cec5SDimitry Andric 6520b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::PragmaExecCharsetPop(SourceLocation Loc) { 653349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 654*5f757f3fSDimitry Andric *OS << "#pragma character_execution_set(pop)"; 6550b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6560b57cec5SDimitry Andric } 6570b57cec5SDimitry Andric 6580b57cec5SDimitry Andric void PrintPPOutputPPCallbacks:: 6590b57cec5SDimitry Andric PragmaAssumeNonNullBegin(SourceLocation Loc) { 660349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 661*5f757f3fSDimitry Andric *OS << "#pragma clang assume_nonnull begin"; 6620b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6630b57cec5SDimitry Andric } 6640b57cec5SDimitry Andric 6650b57cec5SDimitry Andric void PrintPPOutputPPCallbacks:: 6660b57cec5SDimitry Andric PragmaAssumeNonNullEnd(SourceLocation Loc) { 667349cc55cSDimitry Andric MoveToLine(Loc, /*RequireStartOfLine=*/true); 668*5f757f3fSDimitry Andric *OS << "#pragma clang assume_nonnull end"; 6690b57cec5SDimitry Andric setEmittedDirectiveOnThisLine(); 6700b57cec5SDimitry Andric } 6710b57cec5SDimitry Andric 672349cc55cSDimitry Andric void PrintPPOutputPPCallbacks::HandleWhitespaceBeforeTok(const Token &Tok, 673349cc55cSDimitry Andric bool RequireSpace, 674349cc55cSDimitry Andric bool RequireSameLine) { 675349cc55cSDimitry Andric // These tokens are not expanded to anything and don't need whitespace before 676349cc55cSDimitry Andric // them. 677349cc55cSDimitry Andric if (Tok.is(tok::eof) || 678349cc55cSDimitry Andric (Tok.isAnnotation() && !Tok.is(tok::annot_header_unit) && 67906c3fb27SDimitry Andric !Tok.is(tok::annot_module_begin) && !Tok.is(tok::annot_module_end) && 68006c3fb27SDimitry Andric !Tok.is(tok::annot_repl_input_end))) 681349cc55cSDimitry Andric return; 6820b57cec5SDimitry Andric 683349cc55cSDimitry Andric // EmittedDirectiveOnThisLine takes priority over RequireSameLine. 684349cc55cSDimitry Andric if ((!RequireSameLine || EmittedDirectiveOnThisLine) && 685349cc55cSDimitry Andric MoveToLine(Tok, /*RequireStartOfLine=*/EmittedDirectiveOnThisLine)) { 686349cc55cSDimitry Andric if (MinimizeWhitespace) { 687349cc55cSDimitry Andric // Avoid interpreting hash as a directive under -fpreprocessed. 688349cc55cSDimitry Andric if (Tok.is(tok::hash)) 689*5f757f3fSDimitry Andric *OS << ' '; 690349cc55cSDimitry Andric } else { 6910b57cec5SDimitry Andric // Print out space characters so that the first token on a line is 6920b57cec5SDimitry Andric // indented for easy reading. 6930b57cec5SDimitry Andric unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation()); 6940b57cec5SDimitry Andric 695349cc55cSDimitry Andric // The first token on a line can have a column number of 1, yet still 696349cc55cSDimitry Andric // expect leading white space, if a macro expansion in column 1 starts 697349cc55cSDimitry Andric // with an empty macro argument, or an empty nested macro expansion. In 698349cc55cSDimitry Andric // this case, move the token to column 2. 6990b57cec5SDimitry Andric if (ColNo == 1 && Tok.hasLeadingSpace()) 7000b57cec5SDimitry Andric ColNo = 2; 7010b57cec5SDimitry Andric 7020b57cec5SDimitry Andric // This hack prevents stuff like: 7030b57cec5SDimitry Andric // #define HASH # 7040b57cec5SDimitry Andric // HASH define foo bar 7050b57cec5SDimitry Andric // From having the # character end up at column 1, which makes it so it 7060b57cec5SDimitry Andric // is not handled as a #define next time through the preprocessor if in 7070b57cec5SDimitry Andric // -fpreprocessed mode. 7080b57cec5SDimitry Andric if (ColNo <= 1 && Tok.is(tok::hash)) 709*5f757f3fSDimitry Andric *OS << ' '; 7100b57cec5SDimitry Andric 7110b57cec5SDimitry Andric // Otherwise, indent the appropriate number of spaces. 7120b57cec5SDimitry Andric for (; ColNo > 1; --ColNo) 713*5f757f3fSDimitry Andric *OS << ' '; 714349cc55cSDimitry Andric } 715349cc55cSDimitry Andric } else { 716349cc55cSDimitry Andric // Insert whitespace between the previous and next token if either 717349cc55cSDimitry Andric // - The caller requires it 718349cc55cSDimitry Andric // - The input had whitespace between them and we are not in 719349cc55cSDimitry Andric // whitespace-minimization mode 720349cc55cSDimitry Andric // - The whitespace is necessary to keep the tokens apart and there is not 721349cc55cSDimitry Andric // already a newline between them 722349cc55cSDimitry Andric if (RequireSpace || (!MinimizeWhitespace && Tok.hasLeadingSpace()) || 723349cc55cSDimitry Andric ((EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) && 724349cc55cSDimitry Andric AvoidConcat(PrevPrevTok, PrevTok, Tok))) 725*5f757f3fSDimitry Andric *OS << ' '; 726349cc55cSDimitry Andric } 7270b57cec5SDimitry Andric 728349cc55cSDimitry Andric PrevPrevTok = PrevTok; 729349cc55cSDimitry Andric PrevTok = Tok; 7300b57cec5SDimitry Andric } 7310b57cec5SDimitry Andric 7320b57cec5SDimitry Andric void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr, 7330b57cec5SDimitry Andric unsigned Len) { 7340b57cec5SDimitry Andric unsigned NumNewlines = 0; 7350b57cec5SDimitry Andric for (; Len; --Len, ++TokStr) { 7360b57cec5SDimitry Andric if (*TokStr != '\n' && 7370b57cec5SDimitry Andric *TokStr != '\r') 7380b57cec5SDimitry Andric continue; 7390b57cec5SDimitry Andric 7400b57cec5SDimitry Andric ++NumNewlines; 7410b57cec5SDimitry Andric 7420b57cec5SDimitry Andric // If we have \n\r or \r\n, skip both and count as one line. 7430b57cec5SDimitry Andric if (Len != 1 && 7440b57cec5SDimitry Andric (TokStr[1] == '\n' || TokStr[1] == '\r') && 7450b57cec5SDimitry Andric TokStr[0] != TokStr[1]) { 7460b57cec5SDimitry Andric ++TokStr; 7470b57cec5SDimitry Andric --Len; 7480b57cec5SDimitry Andric } 7490b57cec5SDimitry Andric } 7500b57cec5SDimitry Andric 7510b57cec5SDimitry Andric if (NumNewlines == 0) return; 7520b57cec5SDimitry Andric 7530b57cec5SDimitry Andric CurLine += NumNewlines; 7540b57cec5SDimitry Andric } 7550b57cec5SDimitry Andric 7560b57cec5SDimitry Andric 7570b57cec5SDimitry Andric namespace { 7580b57cec5SDimitry Andric struct UnknownPragmaHandler : public PragmaHandler { 7590b57cec5SDimitry Andric const char *Prefix; 7600b57cec5SDimitry Andric PrintPPOutputPPCallbacks *Callbacks; 7610b57cec5SDimitry Andric 7620b57cec5SDimitry Andric // Set to true if tokens should be expanded 7630b57cec5SDimitry Andric bool ShouldExpandTokens; 7640b57cec5SDimitry Andric 7650b57cec5SDimitry Andric UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks, 7660b57cec5SDimitry Andric bool RequireTokenExpansion) 7670b57cec5SDimitry Andric : Prefix(prefix), Callbacks(callbacks), 7680b57cec5SDimitry Andric ShouldExpandTokens(RequireTokenExpansion) {} 7690b57cec5SDimitry Andric void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, 7700b57cec5SDimitry Andric Token &PragmaTok) override { 7710b57cec5SDimitry Andric // Figure out what line we went to and insert the appropriate number of 7720b57cec5SDimitry Andric // newline characters. 773349cc55cSDimitry Andric Callbacks->MoveToLine(PragmaTok.getLocation(), /*RequireStartOfLine=*/true); 774*5f757f3fSDimitry Andric Callbacks->OS->write(Prefix, strlen(Prefix)); 775349cc55cSDimitry Andric Callbacks->setEmittedTokensOnThisLine(); 7760b57cec5SDimitry Andric 7770b57cec5SDimitry Andric if (ShouldExpandTokens) { 7780b57cec5SDimitry Andric // The first token does not have expanded macros. Expand them, if 7790b57cec5SDimitry Andric // required. 780a7dea167SDimitry Andric auto Toks = std::make_unique<Token[]>(1); 7810b57cec5SDimitry Andric Toks[0] = PragmaTok; 7820b57cec5SDimitry Andric PP.EnterTokenStream(std::move(Toks), /*NumToks=*/1, 7830b57cec5SDimitry Andric /*DisableMacroExpansion=*/false, 7840b57cec5SDimitry Andric /*IsReinject=*/false); 7850b57cec5SDimitry Andric PP.Lex(PragmaTok); 7860b57cec5SDimitry Andric } 7870b57cec5SDimitry Andric 7880b57cec5SDimitry Andric // Read and print all of the pragma tokens. 789349cc55cSDimitry Andric bool IsFirst = true; 7900b57cec5SDimitry Andric while (PragmaTok.isNot(tok::eod)) { 791349cc55cSDimitry Andric Callbacks->HandleWhitespaceBeforeTok(PragmaTok, /*RequireSpace=*/IsFirst, 792349cc55cSDimitry Andric /*RequireSameLine=*/true); 793349cc55cSDimitry Andric IsFirst = false; 7940b57cec5SDimitry Andric std::string TokSpell = PP.getSpelling(PragmaTok); 795*5f757f3fSDimitry Andric Callbacks->OS->write(&TokSpell[0], TokSpell.size()); 796349cc55cSDimitry Andric Callbacks->setEmittedTokensOnThisLine(); 7970b57cec5SDimitry Andric 7980b57cec5SDimitry Andric if (ShouldExpandTokens) 7990b57cec5SDimitry Andric PP.Lex(PragmaTok); 8000b57cec5SDimitry Andric else 8010b57cec5SDimitry Andric PP.LexUnexpandedToken(PragmaTok); 8020b57cec5SDimitry Andric } 8030b57cec5SDimitry Andric Callbacks->setEmittedDirectiveOnThisLine(); 8040b57cec5SDimitry Andric } 8050b57cec5SDimitry Andric }; 8060b57cec5SDimitry Andric } // end anonymous namespace 8070b57cec5SDimitry Andric 8080b57cec5SDimitry Andric 8090b57cec5SDimitry Andric static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok, 810*5f757f3fSDimitry Andric PrintPPOutputPPCallbacks *Callbacks) { 8110b57cec5SDimitry Andric bool DropComments = PP.getLangOpts().TraditionalCPP && 8120b57cec5SDimitry Andric !PP.getCommentRetentionState(); 8130b57cec5SDimitry Andric 814349cc55cSDimitry Andric bool IsStartOfLine = false; 8150b57cec5SDimitry Andric char Buffer[256]; 81604eeddc0SDimitry Andric while (true) { 817349cc55cSDimitry Andric // Two lines joined with line continuation ('\' as last character on the 818349cc55cSDimitry Andric // line) must be emitted as one line even though Tok.getLine() returns two 819349cc55cSDimitry Andric // different values. In this situation Tok.isAtStartOfLine() is false even 820349cc55cSDimitry Andric // though it may be the first token on the lexical line. When 821349cc55cSDimitry Andric // dropping/skipping a token that is at the start of a line, propagate the 822349cc55cSDimitry Andric // start-of-line-ness to the next token to not append it to the previous 823349cc55cSDimitry Andric // line. 824349cc55cSDimitry Andric IsStartOfLine = IsStartOfLine || Tok.isAtStartOfLine(); 8250b57cec5SDimitry Andric 826349cc55cSDimitry Andric Callbacks->HandleWhitespaceBeforeTok(Tok, /*RequireSpace=*/false, 827349cc55cSDimitry Andric /*RequireSameLine=*/!IsStartOfLine); 8280b57cec5SDimitry Andric 8290b57cec5SDimitry Andric if (DropComments && Tok.is(tok::comment)) { 8300b57cec5SDimitry Andric // Skip comments. Normally the preprocessor does not generate 8310b57cec5SDimitry Andric // tok::comment nodes at all when not keeping comments, but under 8320b57cec5SDimitry Andric // -traditional-cpp the lexer keeps /all/ whitespace, including comments. 833349cc55cSDimitry Andric PP.Lex(Tok); 834349cc55cSDimitry Andric continue; 83506c3fb27SDimitry Andric } else if (Tok.is(tok::annot_repl_input_end)) { 83606c3fb27SDimitry Andric PP.Lex(Tok); 83706c3fb27SDimitry Andric continue; 8380b57cec5SDimitry Andric } else if (Tok.is(tok::eod)) { 8390b57cec5SDimitry Andric // Don't print end of directive tokens, since they are typically newlines 8400b57cec5SDimitry Andric // that mess up our line tracking. These come from unknown pre-processor 8410b57cec5SDimitry Andric // directives or hash-prefixed comments in standalone assembly files. 8420b57cec5SDimitry Andric PP.Lex(Tok); 843349cc55cSDimitry Andric // FIXME: The token on the next line after #include should have 844349cc55cSDimitry Andric // Tok.isAtStartOfLine() set. 845349cc55cSDimitry Andric IsStartOfLine = true; 8460b57cec5SDimitry Andric continue; 8470b57cec5SDimitry Andric } else if (Tok.is(tok::annot_module_include)) { 8480b57cec5SDimitry Andric // PrintPPOutputPPCallbacks::InclusionDirective handles producing 8490b57cec5SDimitry Andric // appropriate output here. Ignore this token entirely. 8500b57cec5SDimitry Andric PP.Lex(Tok); 851349cc55cSDimitry Andric IsStartOfLine = true; 8520b57cec5SDimitry Andric continue; 8530b57cec5SDimitry Andric } else if (Tok.is(tok::annot_module_begin)) { 8540b57cec5SDimitry Andric // FIXME: We retrieve this token after the FileChanged callback, and 8550b57cec5SDimitry Andric // retrieve the module_end token before the FileChanged callback, so 8560b57cec5SDimitry Andric // we render this within the file and render the module end outside the 8570b57cec5SDimitry Andric // file, but this is backwards from the token locations: the module_begin 8580b57cec5SDimitry Andric // token is at the include location (outside the file) and the module_end 8590b57cec5SDimitry Andric // token is at the EOF location (within the file). 8600b57cec5SDimitry Andric Callbacks->BeginModule( 8610b57cec5SDimitry Andric reinterpret_cast<Module *>(Tok.getAnnotationValue())); 8620b57cec5SDimitry Andric PP.Lex(Tok); 863349cc55cSDimitry Andric IsStartOfLine = true; 8640b57cec5SDimitry Andric continue; 8650b57cec5SDimitry Andric } else if (Tok.is(tok::annot_module_end)) { 8660b57cec5SDimitry Andric Callbacks->EndModule( 8670b57cec5SDimitry Andric reinterpret_cast<Module *>(Tok.getAnnotationValue())); 8680b57cec5SDimitry Andric PP.Lex(Tok); 869349cc55cSDimitry Andric IsStartOfLine = true; 8700b57cec5SDimitry Andric continue; 8710b57cec5SDimitry Andric } else if (Tok.is(tok::annot_header_unit)) { 8720b57cec5SDimitry Andric // This is a header-name that has been (effectively) converted into a 8730b57cec5SDimitry Andric // module-name. 8740b57cec5SDimitry Andric // FIXME: The module name could contain non-identifier module name 8750b57cec5SDimitry Andric // components. We don't have a good way to round-trip those. 8760b57cec5SDimitry Andric Module *M = reinterpret_cast<Module *>(Tok.getAnnotationValue()); 8770b57cec5SDimitry Andric std::string Name = M->getFullModuleName(); 878*5f757f3fSDimitry Andric Callbacks->OS->write(Name.data(), Name.size()); 8790b57cec5SDimitry Andric Callbacks->HandleNewlinesInToken(Name.data(), Name.size()); 8800b57cec5SDimitry Andric } else if (Tok.isAnnotation()) { 8810b57cec5SDimitry Andric // Ignore annotation tokens created by pragmas - the pragmas themselves 8820b57cec5SDimitry Andric // will be reproduced in the preprocessed output. 8830b57cec5SDimitry Andric PP.Lex(Tok); 8840b57cec5SDimitry Andric continue; 8850b57cec5SDimitry Andric } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) { 886*5f757f3fSDimitry Andric *Callbacks->OS << II->getName(); 8870b57cec5SDimitry Andric } else if (Tok.isLiteral() && !Tok.needsCleaning() && 8880b57cec5SDimitry Andric Tok.getLiteralData()) { 889*5f757f3fSDimitry Andric Callbacks->OS->write(Tok.getLiteralData(), Tok.getLength()); 890bdd1243dSDimitry Andric } else if (Tok.getLength() < std::size(Buffer)) { 8910b57cec5SDimitry Andric const char *TokPtr = Buffer; 8920b57cec5SDimitry Andric unsigned Len = PP.getSpelling(Tok, TokPtr); 893*5f757f3fSDimitry Andric Callbacks->OS->write(TokPtr, Len); 8940b57cec5SDimitry Andric 8950b57cec5SDimitry Andric // Tokens that can contain embedded newlines need to adjust our current 8960b57cec5SDimitry Andric // line number. 897349cc55cSDimitry Andric // FIXME: The token may end with a newline in which case 898349cc55cSDimitry Andric // setEmittedDirectiveOnThisLine/setEmittedTokensOnThisLine afterwards is 899349cc55cSDimitry Andric // wrong. 9000b57cec5SDimitry Andric if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown) 9010b57cec5SDimitry Andric Callbacks->HandleNewlinesInToken(TokPtr, Len); 902349cc55cSDimitry Andric if (Tok.is(tok::comment) && Len >= 2 && TokPtr[0] == '/' && 903349cc55cSDimitry Andric TokPtr[1] == '/') { 904349cc55cSDimitry Andric // It's a line comment; 905349cc55cSDimitry Andric // Ensure that we don't concatenate anything behind it. 906349cc55cSDimitry Andric Callbacks->setEmittedDirectiveOnThisLine(); 907349cc55cSDimitry Andric } 9080b57cec5SDimitry Andric } else { 9090b57cec5SDimitry Andric std::string S = PP.getSpelling(Tok); 910*5f757f3fSDimitry Andric Callbacks->OS->write(S.data(), S.size()); 9110b57cec5SDimitry Andric 9120b57cec5SDimitry Andric // Tokens that can contain embedded newlines need to adjust our current 9130b57cec5SDimitry Andric // line number. 9140b57cec5SDimitry Andric if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown) 9150b57cec5SDimitry Andric Callbacks->HandleNewlinesInToken(S.data(), S.size()); 916349cc55cSDimitry Andric if (Tok.is(tok::comment) && S.size() >= 2 && S[0] == '/' && S[1] == '/') { 917349cc55cSDimitry Andric // It's a line comment; 918349cc55cSDimitry Andric // Ensure that we don't concatenate anything behind it. 919349cc55cSDimitry Andric Callbacks->setEmittedDirectiveOnThisLine(); 920349cc55cSDimitry Andric } 9210b57cec5SDimitry Andric } 9220b57cec5SDimitry Andric Callbacks->setEmittedTokensOnThisLine(); 923349cc55cSDimitry Andric IsStartOfLine = false; 9240b57cec5SDimitry Andric 9250b57cec5SDimitry Andric if (Tok.is(tok::eof)) break; 9260b57cec5SDimitry Andric 9270b57cec5SDimitry Andric PP.Lex(Tok); 9280b57cec5SDimitry Andric } 9290b57cec5SDimitry Andric } 9300b57cec5SDimitry Andric 9310b57cec5SDimitry Andric typedef std::pair<const IdentifierInfo *, MacroInfo *> id_macro_pair; 9320b57cec5SDimitry Andric static int MacroIDCompare(const id_macro_pair *LHS, const id_macro_pair *RHS) { 9330b57cec5SDimitry Andric return LHS->first->getName().compare(RHS->first->getName()); 9340b57cec5SDimitry Andric } 9350b57cec5SDimitry Andric 9360b57cec5SDimitry Andric static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) { 9370b57cec5SDimitry Andric // Ignore unknown pragmas. 9380b57cec5SDimitry Andric PP.IgnorePragmas(); 9390b57cec5SDimitry Andric 9400b57cec5SDimitry Andric // -dM mode just scans and ignores all tokens in the files, then dumps out 9410b57cec5SDimitry Andric // the macro table at the end. 9420b57cec5SDimitry Andric PP.EnterMainSourceFile(); 9430b57cec5SDimitry Andric 9440b57cec5SDimitry Andric Token Tok; 9450b57cec5SDimitry Andric do PP.Lex(Tok); 9460b57cec5SDimitry Andric while (Tok.isNot(tok::eof)); 9470b57cec5SDimitry Andric 9480b57cec5SDimitry Andric SmallVector<id_macro_pair, 128> MacrosByID; 9490b57cec5SDimitry Andric for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end(); 9500b57cec5SDimitry Andric I != E; ++I) { 9510b57cec5SDimitry Andric auto *MD = I->second.getLatest(); 9520b57cec5SDimitry Andric if (MD && MD->isDefined()) 9530b57cec5SDimitry Andric MacrosByID.push_back(id_macro_pair(I->first, MD->getMacroInfo())); 9540b57cec5SDimitry Andric } 9550b57cec5SDimitry Andric llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare); 9560b57cec5SDimitry Andric 9570b57cec5SDimitry Andric for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) { 9580b57cec5SDimitry Andric MacroInfo &MI = *MacrosByID[i].second; 9590b57cec5SDimitry Andric // Ignore computed macros like __LINE__ and friends. 9600b57cec5SDimitry Andric if (MI.isBuiltinMacro()) continue; 9610b57cec5SDimitry Andric 962*5f757f3fSDimitry Andric PrintMacroDefinition(*MacrosByID[i].first, MI, PP, OS); 9630b57cec5SDimitry Andric *OS << '\n'; 9640b57cec5SDimitry Andric } 9650b57cec5SDimitry Andric } 9660b57cec5SDimitry Andric 9670b57cec5SDimitry Andric /// DoPrintPreprocessedInput - This implements -E mode. 9680b57cec5SDimitry Andric /// 9690b57cec5SDimitry Andric void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, 9700b57cec5SDimitry Andric const PreprocessorOutputOptions &Opts) { 9710b57cec5SDimitry Andric // Show macros with no output is handled specially. 9720b57cec5SDimitry Andric if (!Opts.ShowCPP) { 9730b57cec5SDimitry Andric assert(Opts.ShowMacros && "Not yet implemented!"); 9740b57cec5SDimitry Andric DoPrintMacros(PP, OS); 9750b57cec5SDimitry Andric return; 9760b57cec5SDimitry Andric } 9770b57cec5SDimitry Andric 9780b57cec5SDimitry Andric // Inform the preprocessor whether we want it to retain comments or not, due 9790b57cec5SDimitry Andric // to -C or -CC. 9800b57cec5SDimitry Andric PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments); 9810b57cec5SDimitry Andric 9820b57cec5SDimitry Andric PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks( 983*5f757f3fSDimitry Andric PP, OS, !Opts.ShowLineMarkers, Opts.ShowMacros, 984349cc55cSDimitry Andric Opts.ShowIncludeDirectives, Opts.UseLineDirectives, 985*5f757f3fSDimitry Andric Opts.MinimizeWhitespace, Opts.DirectivesOnly, Opts.KeepSystemIncludes); 9860b57cec5SDimitry Andric 9870b57cec5SDimitry Andric // Expand macros in pragmas with -fms-extensions. The assumption is that 9880b57cec5SDimitry Andric // the majority of pragmas in such a file will be Microsoft pragmas. 9890b57cec5SDimitry Andric // Remember the handlers we will add so that we can remove them later. 9900b57cec5SDimitry Andric std::unique_ptr<UnknownPragmaHandler> MicrosoftExtHandler( 9910b57cec5SDimitry Andric new UnknownPragmaHandler( 9920b57cec5SDimitry Andric "#pragma", Callbacks, 9930b57cec5SDimitry Andric /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt)); 9940b57cec5SDimitry Andric 9950b57cec5SDimitry Andric std::unique_ptr<UnknownPragmaHandler> GCCHandler(new UnknownPragmaHandler( 9960b57cec5SDimitry Andric "#pragma GCC", Callbacks, 9970b57cec5SDimitry Andric /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt)); 9980b57cec5SDimitry Andric 9990b57cec5SDimitry Andric std::unique_ptr<UnknownPragmaHandler> ClangHandler(new UnknownPragmaHandler( 10000b57cec5SDimitry Andric "#pragma clang", Callbacks, 10010b57cec5SDimitry Andric /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt)); 10020b57cec5SDimitry Andric 10030b57cec5SDimitry Andric PP.AddPragmaHandler(MicrosoftExtHandler.get()); 10040b57cec5SDimitry Andric PP.AddPragmaHandler("GCC", GCCHandler.get()); 10050b57cec5SDimitry Andric PP.AddPragmaHandler("clang", ClangHandler.get()); 10060b57cec5SDimitry Andric 10070b57cec5SDimitry Andric // The tokens after pragma omp need to be expanded. 10080b57cec5SDimitry Andric // 10090b57cec5SDimitry Andric // OpenMP [2.1, Directive format] 10100b57cec5SDimitry Andric // Preprocessing tokens following the #pragma omp are subject to macro 10110b57cec5SDimitry Andric // replacement. 10120b57cec5SDimitry Andric std::unique_ptr<UnknownPragmaHandler> OpenMPHandler( 10130b57cec5SDimitry Andric new UnknownPragmaHandler("#pragma omp", Callbacks, 10140b57cec5SDimitry Andric /*RequireTokenExpansion=*/true)); 10150b57cec5SDimitry Andric PP.AddPragmaHandler("omp", OpenMPHandler.get()); 10160b57cec5SDimitry Andric 10170b57cec5SDimitry Andric PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks)); 10180b57cec5SDimitry Andric 10190b57cec5SDimitry Andric // After we have configured the preprocessor, enter the main file. 10200b57cec5SDimitry Andric PP.EnterMainSourceFile(); 102181ad6265SDimitry Andric if (Opts.DirectivesOnly) 102281ad6265SDimitry Andric PP.SetMacroExpansionOnlyInDirectives(); 10230b57cec5SDimitry Andric 10240b57cec5SDimitry Andric // Consume all of the tokens that come from the predefines buffer. Those 10250b57cec5SDimitry Andric // should not be emitted into the output and are guaranteed to be at the 10260b57cec5SDimitry Andric // start. 10270b57cec5SDimitry Andric const SourceManager &SourceMgr = PP.getSourceManager(); 10280b57cec5SDimitry Andric Token Tok; 10290b57cec5SDimitry Andric do { 10300b57cec5SDimitry Andric PP.Lex(Tok); 10310b57cec5SDimitry Andric if (Tok.is(tok::eof) || !Tok.getLocation().isFileID()) 10320b57cec5SDimitry Andric break; 10330b57cec5SDimitry Andric 10340b57cec5SDimitry Andric PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); 10350b57cec5SDimitry Andric if (PLoc.isInvalid()) 10360b57cec5SDimitry Andric break; 10370b57cec5SDimitry Andric 10380b57cec5SDimitry Andric if (strcmp(PLoc.getFilename(), "<built-in>")) 10390b57cec5SDimitry Andric break; 10400b57cec5SDimitry Andric } while (true); 10410b57cec5SDimitry Andric 10420b57cec5SDimitry Andric // Read all the preprocessed tokens, printing them out to the stream. 1043*5f757f3fSDimitry Andric PrintPreprocessedTokens(PP, Tok, Callbacks); 10440b57cec5SDimitry Andric *OS << '\n'; 10450b57cec5SDimitry Andric 10460b57cec5SDimitry Andric // Remove the handlers we just added to leave the preprocessor in a sane state 10470b57cec5SDimitry Andric // so that it can be reused (for example by a clang::Parser instance). 10480b57cec5SDimitry Andric PP.RemovePragmaHandler(MicrosoftExtHandler.get()); 10490b57cec5SDimitry Andric PP.RemovePragmaHandler("GCC", GCCHandler.get()); 10500b57cec5SDimitry Andric PP.RemovePragmaHandler("clang", ClangHandler.get()); 10510b57cec5SDimitry Andric PP.RemovePragmaHandler("omp", OpenMPHandler.get()); 10520b57cec5SDimitry Andric } 1053