clang API Documentation

PrintPreprocessedOutput.cpp
Go to the documentation of this file.
00001 //===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This code simply runs the preprocessor on the input file and prints out the
00011 // result.  This is the traditional behavior of the -E option.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "clang/Frontend/Utils.h"
00016 #include "clang/Basic/CharInfo.h"
00017 #include "clang/Basic/Diagnostic.h"
00018 #include "clang/Basic/SourceManager.h"
00019 #include "clang/Frontend/PreprocessorOutputOptions.h"
00020 #include "clang/Lex/MacroInfo.h"
00021 #include "clang/Lex/PPCallbacks.h"
00022 #include "clang/Lex/Pragma.h"
00023 #include "clang/Lex/Preprocessor.h"
00024 #include "clang/Lex/TokenConcatenation.h"
00025 #include "llvm/ADT/STLExtras.h"
00026 #include "llvm/ADT/SmallString.h"
00027 #include "llvm/ADT/StringRef.h"
00028 #include "llvm/Support/ErrorHandling.h"
00029 #include "llvm/Support/raw_ostream.h"
00030 #include <cstdio>
00031 using namespace clang;
00032 
00033 /// PrintMacroDefinition - Print a macro definition in a form that will be
00034 /// properly accepted back as a definition.
00035 static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI,
00036                                  Preprocessor &PP, raw_ostream &OS) {
00037   OS << "#define " << II.getName();
00038 
00039   if (MI.isFunctionLike()) {
00040     OS << '(';
00041     if (!MI.arg_empty()) {
00042       MacroInfo::arg_iterator AI = MI.arg_begin(), E = MI.arg_end();
00043       for (; AI+1 != E; ++AI) {
00044         OS << (*AI)->getName();
00045         OS << ',';
00046       }
00047 
00048       // Last argument.
00049       if ((*AI)->getName() == "__VA_ARGS__")
00050         OS << "...";
00051       else
00052         OS << (*AI)->getName();
00053     }
00054 
00055     if (MI.isGNUVarargs())
00056       OS << "...";  // #define foo(x...)
00057 
00058     OS << ')';
00059   }
00060 
00061   // GCC always emits a space, even if the macro body is empty.  However, do not
00062   // want to emit two spaces if the first token has a leading space.
00063   if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace())
00064     OS << ' ';
00065 
00066   SmallString<128> SpellingBuffer;
00067   for (MacroInfo::tokens_iterator I = MI.tokens_begin(), E = MI.tokens_end();
00068        I != E; ++I) {
00069     if (I->hasLeadingSpace())
00070       OS << ' ';
00071 
00072     OS << PP.getSpelling(*I, SpellingBuffer);
00073   }
00074 }
00075 
00076 //===----------------------------------------------------------------------===//
00077 // Preprocessed token printer
00078 //===----------------------------------------------------------------------===//
00079 
00080 namespace {
00081 class PrintPPOutputPPCallbacks : public PPCallbacks {
00082   Preprocessor &PP;
00083   SourceManager &SM;
00084   TokenConcatenation ConcatInfo;
00085 public:
00086   raw_ostream &OS;
00087 private:
00088   unsigned CurLine;
00089 
00090   bool EmittedTokensOnThisLine;
00091   bool EmittedDirectiveOnThisLine;
00092   SrcMgr::CharacteristicKind FileType;
00093   SmallString<512> CurFilename;
00094   bool Initialized;
00095   bool DisableLineMarkers;
00096   bool DumpDefines;
00097   bool UseLineDirective;
00098   bool IsFirstFileEntered;
00099 public:
00100   PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os,
00101                            bool lineMarkers, bool defines)
00102      : PP(pp), SM(PP.getSourceManager()),
00103        ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers),
00104        DumpDefines(defines) {
00105     CurLine = 0;
00106     CurFilename += "<uninit>";
00107     EmittedTokensOnThisLine = false;
00108     EmittedDirectiveOnThisLine = false;
00109     FileType = SrcMgr::C_User;
00110     Initialized = false;
00111     IsFirstFileEntered = false;
00112 
00113     // If we're in microsoft mode, use normal #line instead of line markers.
00114     UseLineDirective = PP.getLangOpts().MicrosoftExt;
00115   }
00116 
00117   void setEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
00118   bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
00119 
00120   void setEmittedDirectiveOnThisLine() { EmittedDirectiveOnThisLine = true; }
00121   bool hasEmittedDirectiveOnThisLine() const {
00122     return EmittedDirectiveOnThisLine;
00123   }
00124 
00125   bool startNewLineIfNeeded(bool ShouldUpdateCurrentLine = true);
00126 
00127   void FileChanged(SourceLocation Loc, FileChangeReason Reason,
00128                    SrcMgr::CharacteristicKind FileType,
00129                    FileID PrevFID) override;
00130   void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
00131                           StringRef FileName, bool IsAngled,
00132                           CharSourceRange FilenameRange, const FileEntry *File,
00133                           StringRef SearchPath, StringRef RelativePath,
00134                           const Module *Imported) override;
00135   void Ident(SourceLocation Loc, const std::string &str) override;
00136   void PragmaMessage(SourceLocation Loc, StringRef Namespace,
00137                      PragmaMessageKind Kind, StringRef Str) override;
00138   void PragmaDebug(SourceLocation Loc, StringRef DebugType) override;
00139   void PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) override;
00140   void PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) override;
00141   void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
00142                         diag::Severity Map, StringRef Str) override;
00143   void PragmaWarning(SourceLocation Loc, StringRef WarningSpec,
00144                      ArrayRef<int> Ids) override;
00145   void PragmaWarningPush(SourceLocation Loc, int Level) override;
00146   void PragmaWarningPop(SourceLocation Loc) override;
00147 
00148   bool HandleFirstTokOnLine(Token &Tok);
00149 
00150   /// Move to the line of the provided source location. This will
00151   /// return true if the output stream required adjustment or if
00152   /// the requested location is on the first line.
00153   bool MoveToLine(SourceLocation Loc) {
00154     PresumedLoc PLoc = SM.getPresumedLoc(Loc);
00155     if (PLoc.isInvalid())
00156       return false;
00157     return MoveToLine(PLoc.getLine()) || (PLoc.getLine() == 1);
00158   }
00159   bool MoveToLine(unsigned LineNo);
00160 
00161   bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok, 
00162                    const Token &Tok) {
00163     return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok);
00164   }
00165   void WriteLineInfo(unsigned LineNo, const char *Extra=nullptr,
00166                      unsigned ExtraLen=0);
00167   bool LineMarkersAreDisabled() const { return DisableLineMarkers; }
00168   void HandleNewlinesInToken(const char *TokStr, unsigned Len);
00169 
00170   /// MacroDefined - This hook is called whenever a macro definition is seen.
00171   void MacroDefined(const Token &MacroNameTok,
00172                     const MacroDirective *MD) override;
00173 
00174   /// MacroUndefined - This hook is called whenever a macro #undef is seen.
00175   void MacroUndefined(const Token &MacroNameTok,
00176                       const MacroDirective *MD) override;
00177 };
00178 }  // end anonymous namespace
00179 
00180 void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
00181                                              const char *Extra,
00182                                              unsigned ExtraLen) {
00183   startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
00184 
00185   // Emit #line directives or GNU line markers depending on what mode we're in.
00186   if (UseLineDirective) {
00187     OS << "#line" << ' ' << LineNo << ' ' << '"';
00188     OS.write_escaped(CurFilename);
00189     OS << '"';
00190   } else {
00191     OS << '#' << ' ' << LineNo << ' ' << '"';
00192     OS.write_escaped(CurFilename);
00193     OS << '"';
00194 
00195     if (ExtraLen)
00196       OS.write(Extra, ExtraLen);
00197 
00198     if (FileType == SrcMgr::C_System)
00199       OS.write(" 3", 2);
00200     else if (FileType == SrcMgr::C_ExternCSystem)
00201       OS.write(" 3 4", 4);
00202   }
00203   OS << '\n';
00204 }
00205 
00206 /// MoveToLine - Move the output to the source line specified by the location
00207 /// object.  We can do this by emitting some number of \n's, or be emitting a
00208 /// #line directive.  This returns false if already at the specified line, true
00209 /// if some newlines were emitted.
00210 bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo) {
00211   // If this line is "close enough" to the original line, just print newlines,
00212   // otherwise print a #line directive.
00213   if (LineNo-CurLine <= 8) {
00214     if (LineNo-CurLine == 1)
00215       OS << '\n';
00216     else if (LineNo == CurLine)
00217       return false;    // Spelling line moved, but expansion line didn't.
00218     else {
00219       const char *NewLines = "\n\n\n\n\n\n\n\n";
00220       OS.write(NewLines, LineNo-CurLine);
00221     }
00222   } else if (!DisableLineMarkers) {
00223     // Emit a #line or line marker.
00224     WriteLineInfo(LineNo, nullptr, 0);
00225   } else {
00226     // Okay, we're in -P mode, which turns off line markers.  However, we still
00227     // need to emit a newline between tokens on different lines.
00228     startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
00229   }
00230 
00231   CurLine = LineNo;
00232   return true;
00233 }
00234 
00235 bool
00236 PrintPPOutputPPCallbacks::startNewLineIfNeeded(bool ShouldUpdateCurrentLine) {
00237   if (EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) {
00238     OS << '\n';
00239     EmittedTokensOnThisLine = false;
00240     EmittedDirectiveOnThisLine = false;
00241     if (ShouldUpdateCurrentLine)
00242       ++CurLine;
00243     return true;
00244   }
00245   
00246   return false;
00247 }
00248 
00249 /// FileChanged - Whenever the preprocessor enters or exits a #include file
00250 /// it invokes this handler.  Update our conception of the current source
00251 /// position.
00252 void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
00253                                            FileChangeReason Reason,
00254                                        SrcMgr::CharacteristicKind NewFileType,
00255                                        FileID PrevFID) {
00256   // Unless we are exiting a #include, make sure to skip ahead to the line the
00257   // #include directive was at.
00258   SourceManager &SourceMgr = SM;
00259   
00260   PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc);
00261   if (UserLoc.isInvalid())
00262     return;
00263   
00264   unsigned NewLine = UserLoc.getLine();
00265 
00266   if (Reason == PPCallbacks::EnterFile) {
00267     SourceLocation IncludeLoc = UserLoc.getIncludeLoc();
00268     if (IncludeLoc.isValid())
00269       MoveToLine(IncludeLoc);
00270   } else if (Reason == PPCallbacks::SystemHeaderPragma) {
00271     // GCC emits the # directive for this directive on the line AFTER the
00272     // directive and emits a bunch of spaces that aren't needed. This is because
00273     // otherwise we will emit a line marker for THIS line, which requires an
00274     // extra blank line after the directive to avoid making all following lines
00275     // off by one. We can do better by simply incrementing NewLine here.
00276     NewLine += 1;
00277   }
00278   
00279   CurLine = NewLine;
00280 
00281   CurFilename.clear();
00282   CurFilename += UserLoc.getFilename();
00283   FileType = NewFileType;
00284 
00285   if (DisableLineMarkers) {
00286     startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
00287     return;
00288   }
00289   
00290   if (!Initialized) {
00291     WriteLineInfo(CurLine);
00292     Initialized = true;
00293   }
00294 
00295   // Do not emit an enter marker for the main file (which we expect is the first
00296   // entered file). This matches gcc, and improves compatibility with some tools
00297   // which track the # line markers as a way to determine when the preprocessed
00298   // output is in the context of the main file.
00299   if (Reason == PPCallbacks::EnterFile && !IsFirstFileEntered) {
00300     IsFirstFileEntered = true;
00301     return;
00302   }
00303 
00304   switch (Reason) {
00305   case PPCallbacks::EnterFile:
00306     WriteLineInfo(CurLine, " 1", 2);
00307     break;
00308   case PPCallbacks::ExitFile:
00309     WriteLineInfo(CurLine, " 2", 2);
00310     break;
00311   case PPCallbacks::SystemHeaderPragma:
00312   case PPCallbacks::RenameFile:
00313     WriteLineInfo(CurLine);
00314     break;
00315   }
00316 }
00317 
00318 void PrintPPOutputPPCallbacks::InclusionDirective(SourceLocation HashLoc,
00319                                                   const Token &IncludeTok,
00320                                                   StringRef FileName,
00321                                                   bool IsAngled,
00322                                                   CharSourceRange FilenameRange,
00323                                                   const FileEntry *File,
00324                                                   StringRef SearchPath,
00325                                                   StringRef RelativePath,
00326                                                   const Module *Imported) {
00327   // When preprocessing, turn implicit imports into @imports.
00328   // FIXME: This is a stop-gap until a more comprehensive "preprocessing with
00329   // modules" solution is introduced.
00330   if (Imported) {
00331     startNewLineIfNeeded();
00332     MoveToLine(HashLoc);
00333     OS << "@import " << Imported->getFullModuleName() << ";"
00334        << " /* clang -E: implicit import for \"" << File->getName() << "\" */";
00335     // Since we want a newline after the @import, but not a #<line>, start a new
00336     // line immediately.
00337     EmittedTokensOnThisLine = true;
00338     startNewLineIfNeeded();
00339   }
00340 }
00341 
00342 /// Ident - Handle #ident directives when read by the preprocessor.
00343 ///
00344 void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
00345   MoveToLine(Loc);
00346 
00347   OS.write("#ident ", strlen("#ident "));
00348   OS.write(&S[0], S.size());
00349   EmittedTokensOnThisLine = true;
00350 }
00351 
00352 /// MacroDefined - This hook is called whenever a macro definition is seen.
00353 void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok,
00354                                             const MacroDirective *MD) {
00355   const MacroInfo *MI = MD->getMacroInfo();
00356   // Only print out macro definitions in -dD mode.
00357   if (!DumpDefines ||
00358       // Ignore __FILE__ etc.
00359       MI->isBuiltinMacro()) return;
00360 
00361   MoveToLine(MI->getDefinitionLoc());
00362   PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS);
00363   setEmittedDirectiveOnThisLine();
00364 }
00365 
00366 void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok,
00367                                               const MacroDirective *MD) {
00368   // Only print out macro definitions in -dD mode.
00369   if (!DumpDefines) return;
00370 
00371   MoveToLine(MacroNameTok.getLocation());
00372   OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName();
00373   setEmittedDirectiveOnThisLine();
00374 }
00375 
00376 static void outputPrintable(llvm::raw_ostream& OS,
00377                                              const std::string &Str) {
00378     for (unsigned i = 0, e = Str.size(); i != e; ++i) {
00379       unsigned char Char = Str[i];
00380       if (isPrintable(Char) && Char != '\\' && Char != '"')
00381         OS << (char)Char;
00382       else  // Output anything hard as an octal escape.
00383         OS << '\\'
00384            << (char)('0'+ ((Char >> 6) & 7))
00385            << (char)('0'+ ((Char >> 3) & 7))
00386            << (char)('0'+ ((Char >> 0) & 7));
00387     }
00388 }
00389 
00390 void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc,
00391                                              StringRef Namespace,
00392                                              PragmaMessageKind Kind,
00393                                              StringRef Str) {
00394   startNewLineIfNeeded();
00395   MoveToLine(Loc);
00396   OS << "#pragma ";
00397   if (!Namespace.empty())
00398     OS << Namespace << ' ';
00399   switch (Kind) {
00400     case PMK_Message:
00401       OS << "message(\"";
00402       break;
00403     case PMK_Warning:
00404       OS << "warning \"";
00405       break;
00406     case PMK_Error:
00407       OS << "error \"";
00408       break;
00409   }
00410 
00411   outputPrintable(OS, Str);
00412   OS << '"';
00413   if (Kind == PMK_Message)
00414     OS << ')';
00415   setEmittedDirectiveOnThisLine();
00416 }
00417 
00418 void PrintPPOutputPPCallbacks::PragmaDebug(SourceLocation Loc,
00419                                            StringRef DebugType) {
00420   startNewLineIfNeeded();
00421   MoveToLine(Loc);
00422 
00423   OS << "#pragma clang __debug ";
00424   OS << DebugType;
00425 
00426   setEmittedDirectiveOnThisLine();
00427 }
00428 
00429 void PrintPPOutputPPCallbacks::
00430 PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) {
00431   startNewLineIfNeeded();
00432   MoveToLine(Loc);
00433   OS << "#pragma " << Namespace << " diagnostic push";
00434   setEmittedDirectiveOnThisLine();
00435 }
00436 
00437 void PrintPPOutputPPCallbacks::
00438 PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) {
00439   startNewLineIfNeeded();
00440   MoveToLine(Loc);
00441   OS << "#pragma " << Namespace << " diagnostic pop";
00442   setEmittedDirectiveOnThisLine();
00443 }
00444 
00445 void PrintPPOutputPPCallbacks::PragmaDiagnostic(SourceLocation Loc,
00446                                                 StringRef Namespace,
00447                                                 diag::Severity Map,
00448                                                 StringRef Str) {
00449   startNewLineIfNeeded();
00450   MoveToLine(Loc);
00451   OS << "#pragma " << Namespace << " diagnostic ";
00452   switch (Map) {
00453   case diag::Severity::Remark:
00454     OS << "remark";
00455     break;
00456   case diag::Severity::Warning:
00457     OS << "warning";
00458     break;
00459   case diag::Severity::Error:
00460     OS << "error";
00461     break;
00462   case diag::Severity::Ignored:
00463     OS << "ignored";
00464     break;
00465   case diag::Severity::Fatal:
00466     OS << "fatal";
00467     break;
00468   }
00469   OS << " \"" << Str << '"';
00470   setEmittedDirectiveOnThisLine();
00471 }
00472 
00473 void PrintPPOutputPPCallbacks::PragmaWarning(SourceLocation Loc,
00474                                              StringRef WarningSpec,
00475                                              ArrayRef<int> Ids) {
00476   startNewLineIfNeeded();
00477   MoveToLine(Loc);
00478   OS << "#pragma warning(" << WarningSpec << ':';
00479   for (ArrayRef<int>::iterator I = Ids.begin(), E = Ids.end(); I != E; ++I)
00480     OS << ' ' << *I;
00481   OS << ')';
00482   setEmittedDirectiveOnThisLine();
00483 }
00484 
00485 void PrintPPOutputPPCallbacks::PragmaWarningPush(SourceLocation Loc,
00486                                                  int Level) {
00487   startNewLineIfNeeded();
00488   MoveToLine(Loc);
00489   OS << "#pragma warning(push";
00490   if (Level >= 0)
00491     OS << ", " << Level;
00492   OS << ')';
00493   setEmittedDirectiveOnThisLine();
00494 }
00495 
00496 void PrintPPOutputPPCallbacks::PragmaWarningPop(SourceLocation Loc) {
00497   startNewLineIfNeeded();
00498   MoveToLine(Loc);
00499   OS << "#pragma warning(pop)";
00500   setEmittedDirectiveOnThisLine();
00501 }
00502 
00503 /// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
00504 /// is called for the first token on each new line.  If this really is the start
00505 /// of a new logical line, handle it and return true, otherwise return false.
00506 /// This may not be the start of a logical line because the "start of line"
00507 /// marker is set for spelling lines, not expansion ones.
00508 bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
00509   // Figure out what line we went to and insert the appropriate number of
00510   // newline characters.
00511   if (!MoveToLine(Tok.getLocation()))
00512     return false;
00513 
00514   // Print out space characters so that the first token on a line is
00515   // indented for easy reading.
00516   unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation());
00517 
00518   // The first token on a line can have a column number of 1, yet still expect
00519   // leading white space, if a macro expansion in column 1 starts with an empty
00520   // macro argument, or an empty nested macro expansion. In this case, move the
00521   // token to column 2.
00522   if (ColNo == 1 && Tok.hasLeadingSpace())
00523     ColNo = 2;
00524 
00525   // This hack prevents stuff like:
00526   // #define HASH #
00527   // HASH define foo bar
00528   // From having the # character end up at column 1, which makes it so it
00529   // is not handled as a #define next time through the preprocessor if in
00530   // -fpreprocessed mode.
00531   if (ColNo <= 1 && Tok.is(tok::hash))
00532     OS << ' ';
00533 
00534   // Otherwise, indent the appropriate number of spaces.
00535   for (; ColNo > 1; --ColNo)
00536     OS << ' ';
00537 
00538   return true;
00539 }
00540 
00541 void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
00542                                                      unsigned Len) {
00543   unsigned NumNewlines = 0;
00544   for (; Len; --Len, ++TokStr) {
00545     if (*TokStr != '\n' &&
00546         *TokStr != '\r')
00547       continue;
00548 
00549     ++NumNewlines;
00550 
00551     // If we have \n\r or \r\n, skip both and count as one line.
00552     if (Len != 1 &&
00553         (TokStr[1] == '\n' || TokStr[1] == '\r') &&
00554         TokStr[0] != TokStr[1])
00555       ++TokStr, --Len;
00556   }
00557 
00558   if (NumNewlines == 0) return;
00559 
00560   CurLine += NumNewlines;
00561 }
00562 
00563 
00564 namespace {
00565 struct UnknownPragmaHandler : public PragmaHandler {
00566   const char *Prefix;
00567   PrintPPOutputPPCallbacks *Callbacks;
00568 
00569   UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
00570     : Prefix(prefix), Callbacks(callbacks) {}
00571   void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
00572                     Token &PragmaTok) override {
00573     // Figure out what line we went to and insert the appropriate number of
00574     // newline characters.
00575     Callbacks->startNewLineIfNeeded();
00576     Callbacks->MoveToLine(PragmaTok.getLocation());
00577     Callbacks->OS.write(Prefix, strlen(Prefix));
00578     // Read and print all of the pragma tokens.
00579     while (PragmaTok.isNot(tok::eod)) {
00580       if (PragmaTok.hasLeadingSpace())
00581         Callbacks->OS << ' ';
00582       std::string TokSpell = PP.getSpelling(PragmaTok);
00583       Callbacks->OS.write(&TokSpell[0], TokSpell.size());
00584 
00585       // Expand macros in pragmas with -fms-extensions.  The assumption is that
00586       // the majority of pragmas in such a file will be Microsoft pragmas.
00587       if (PP.getLangOpts().MicrosoftExt)
00588         PP.Lex(PragmaTok);
00589       else
00590         PP.LexUnexpandedToken(PragmaTok);
00591     }
00592     Callbacks->setEmittedDirectiveOnThisLine();
00593   }
00594 };
00595 } // end anonymous namespace
00596 
00597 
00598 static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
00599                                     PrintPPOutputPPCallbacks *Callbacks,
00600                                     raw_ostream &OS) {
00601   bool DropComments = PP.getLangOpts().TraditionalCPP &&
00602                       !PP.getCommentRetentionState();
00603 
00604   char Buffer[256];
00605   Token PrevPrevTok, PrevTok;
00606   PrevPrevTok.startToken();
00607   PrevTok.startToken();
00608   while (1) {
00609     if (Callbacks->hasEmittedDirectiveOnThisLine()) {
00610       Callbacks->startNewLineIfNeeded();
00611       Callbacks->MoveToLine(Tok.getLocation());
00612     }
00613 
00614     // If this token is at the start of a line, emit newlines if needed.
00615     if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
00616       // done.
00617     } else if (Tok.hasLeadingSpace() ||
00618                // If we haven't emitted a token on this line yet, PrevTok isn't
00619                // useful to look at and no concatenation could happen anyway.
00620                (Callbacks->hasEmittedTokensOnThisLine() &&
00621                 // Don't print "-" next to "-", it would form "--".
00622                 Callbacks->AvoidConcat(PrevPrevTok, PrevTok, Tok))) {
00623       OS << ' ';
00624     }
00625 
00626     if (DropComments && Tok.is(tok::comment)) {
00627       // Skip comments. Normally the preprocessor does not generate
00628       // tok::comment nodes at all when not keeping comments, but under
00629       // -traditional-cpp the lexer keeps /all/ whitespace, including comments.
00630       SourceLocation StartLoc = Tok.getLocation();
00631       Callbacks->MoveToLine(StartLoc.getLocWithOffset(Tok.getLength()));
00632     } else if (Tok.is(tok::annot_module_include) ||
00633                Tok.is(tok::annot_module_begin) ||
00634                Tok.is(tok::annot_module_end)) {
00635       // PrintPPOutputPPCallbacks::InclusionDirective handles producing
00636       // appropriate output here. Ignore this token entirely.
00637       PP.Lex(Tok);
00638       continue;
00639     } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
00640       OS << II->getName();
00641     } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
00642                Tok.getLiteralData()) {
00643       OS.write(Tok.getLiteralData(), Tok.getLength());
00644     } else if (Tok.getLength() < 256) {
00645       const char *TokPtr = Buffer;
00646       unsigned Len = PP.getSpelling(Tok, TokPtr);
00647       OS.write(TokPtr, Len);
00648 
00649       // Tokens that can contain embedded newlines need to adjust our current
00650       // line number.
00651       if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
00652         Callbacks->HandleNewlinesInToken(TokPtr, Len);
00653     } else {
00654       std::string S = PP.getSpelling(Tok);
00655       OS.write(&S[0], S.size());
00656 
00657       // Tokens that can contain embedded newlines need to adjust our current
00658       // line number.
00659       if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
00660         Callbacks->HandleNewlinesInToken(&S[0], S.size());
00661     }
00662     Callbacks->setEmittedTokensOnThisLine();
00663 
00664     if (Tok.is(tok::eof)) break;
00665 
00666     PrevPrevTok = PrevTok;
00667     PrevTok = Tok;
00668     PP.Lex(Tok);
00669   }
00670 }
00671 
00672 typedef std::pair<const IdentifierInfo *, MacroInfo *> id_macro_pair;
00673 static int MacroIDCompare(const id_macro_pair *LHS, const id_macro_pair *RHS) {
00674   return LHS->first->getName().compare(RHS->first->getName());
00675 }
00676 
00677 static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) {
00678   // Ignore unknown pragmas.
00679   PP.IgnorePragmas();
00680 
00681   // -dM mode just scans and ignores all tokens in the files, then dumps out
00682   // the macro table at the end.
00683   PP.EnterMainSourceFile();
00684 
00685   Token Tok;
00686   do PP.Lex(Tok);
00687   while (Tok.isNot(tok::eof));
00688 
00689   SmallVector<id_macro_pair, 128> MacrosByID;
00690   for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
00691        I != E; ++I) {
00692     if (I->first->hasMacroDefinition())
00693       MacrosByID.push_back(id_macro_pair(I->first, I->second->getMacroInfo()));
00694   }
00695   llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare);
00696 
00697   for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
00698     MacroInfo &MI = *MacrosByID[i].second;
00699     // Ignore computed macros like __LINE__ and friends.
00700     if (MI.isBuiltinMacro()) continue;
00701 
00702     PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
00703     *OS << '\n';
00704   }
00705 }
00706 
00707 /// DoPrintPreprocessedInput - This implements -E mode.
00708 ///
00709 void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
00710                                      const PreprocessorOutputOptions &Opts) {
00711   // Show macros with no output is handled specially.
00712   if (!Opts.ShowCPP) {
00713     assert(Opts.ShowMacros && "Not yet implemented!");
00714     DoPrintMacros(PP, OS);
00715     return;
00716   }
00717 
00718   // Inform the preprocessor whether we want it to retain comments or not, due
00719   // to -C or -CC.
00720   PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
00721 
00722   PrintPPOutputPPCallbacks *Callbacks =
00723       new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers,
00724                                    Opts.ShowMacros);
00725   PP.AddPragmaHandler(new UnknownPragmaHandler("#pragma", Callbacks));
00726   PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks));
00727   PP.AddPragmaHandler("clang",
00728                       new UnknownPragmaHandler("#pragma clang", Callbacks));
00729 
00730   PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
00731 
00732   // After we have configured the preprocessor, enter the main file.
00733   PP.EnterMainSourceFile();
00734 
00735   // Consume all of the tokens that come from the predefines buffer.  Those
00736   // should not be emitted into the output and are guaranteed to be at the
00737   // start.
00738   const SourceManager &SourceMgr = PP.getSourceManager();
00739   Token Tok;
00740   do {
00741     PP.Lex(Tok);
00742     if (Tok.is(tok::eof) || !Tok.getLocation().isFileID())
00743       break;
00744 
00745     PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
00746     if (PLoc.isInvalid())
00747       break;
00748 
00749     if (strcmp(PLoc.getFilename(), "<built-in>"))
00750       break;
00751   } while (true);
00752 
00753   // Read all the preprocessed tokens, printing them out to the stream.
00754   PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
00755   *OS << '\n';
00756 }