clang API Documentation

RewriteMacros.cpp
Go to the documentation of this file.
00001 //===--- RewriteMacros.cpp - Rewrite macros into their expansions ---------===//
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 rewrites macro invocations into their expansions.  This gives you
00011 // a macro expanded file that retains comments and #includes.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "clang/Rewrite/Frontend/Rewriters.h"
00016 #include "clang/Basic/SourceManager.h"
00017 #include "clang/Lex/Preprocessor.h"
00018 #include "clang/Rewrite/Core/Rewriter.h"
00019 #include "llvm/Support/Path.h"
00020 #include "llvm/Support/raw_ostream.h"
00021 #include <cstdio>
00022 #include <memory>
00023 
00024 using namespace clang;
00025 
00026 /// isSameToken - Return true if the two specified tokens start have the same
00027 /// content.
00028 static bool isSameToken(Token &RawTok, Token &PPTok) {
00029   // If two tokens have the same kind and the same identifier info, they are
00030   // obviously the same.
00031   if (PPTok.getKind() == RawTok.getKind() &&
00032       PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo())
00033     return true;
00034 
00035   // Otherwise, if they are different but have the same identifier info, they
00036   // are also considered to be the same.  This allows keywords and raw lexed
00037   // identifiers with the same name to be treated the same.
00038   if (PPTok.getIdentifierInfo() &&
00039       PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo())
00040     return true;
00041 
00042   return false;
00043 }
00044 
00045 
00046 /// GetNextRawTok - Return the next raw token in the stream, skipping over
00047 /// comments if ReturnComment is false.
00048 static const Token &GetNextRawTok(const std::vector<Token> &RawTokens,
00049                                   unsigned &CurTok, bool ReturnComment) {
00050   assert(CurTok < RawTokens.size() && "Overran eof!");
00051 
00052   // If the client doesn't want comments and we have one, skip it.
00053   if (!ReturnComment && RawTokens[CurTok].is(tok::comment))
00054     ++CurTok;
00055 
00056   return RawTokens[CurTok++];
00057 }
00058 
00059 
00060 /// LexRawTokensFromMainFile - Lets all the raw tokens from the main file into
00061 /// the specified vector.
00062 static void LexRawTokensFromMainFile(Preprocessor &PP,
00063                                      std::vector<Token> &RawTokens) {
00064   SourceManager &SM = PP.getSourceManager();
00065 
00066   // Create a lexer to lex all the tokens of the main file in raw mode.  Even
00067   // though it is in raw mode, it will not return comments.
00068   const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
00069   Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
00070 
00071   // Switch on comment lexing because we really do want them.
00072   RawLex.SetCommentRetentionState(true);
00073 
00074   Token RawTok;
00075   do {
00076     RawLex.LexFromRawLexer(RawTok);
00077 
00078     // If we have an identifier with no identifier info for our raw token, look
00079     // up the indentifier info.  This is important for equality comparison of
00080     // identifier tokens.
00081     if (RawTok.is(tok::raw_identifier))
00082       PP.LookUpIdentifierInfo(RawTok);
00083 
00084     RawTokens.push_back(RawTok);
00085   } while (RawTok.isNot(tok::eof));
00086 }
00087 
00088 
00089 /// RewriteMacrosInInput - Implement -rewrite-macros mode.
00090 void clang::RewriteMacrosInInput(Preprocessor &PP, raw_ostream *OS) {
00091   SourceManager &SM = PP.getSourceManager();
00092 
00093   Rewriter Rewrite;
00094   Rewrite.setSourceMgr(SM, PP.getLangOpts());
00095   RewriteBuffer &RB = Rewrite.getEditBuffer(SM.getMainFileID());
00096 
00097   std::vector<Token> RawTokens;
00098   LexRawTokensFromMainFile(PP, RawTokens);
00099   unsigned CurRawTok = 0;
00100   Token RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
00101 
00102 
00103   // Get the first preprocessing token.
00104   PP.EnterMainSourceFile();
00105   Token PPTok;
00106   PP.Lex(PPTok);
00107 
00108   // Preprocess the input file in parallel with raw lexing the main file. Ignore
00109   // all tokens that are preprocessed from a file other than the main file (e.g.
00110   // a header).  If we see tokens that are in the preprocessed file but not the
00111   // lexed file, we have a macro expansion.  If we see tokens in the lexed file
00112   // that aren't in the preprocessed view, we have macros that expand to no
00113   // tokens, or macro arguments etc.
00114   while (RawTok.isNot(tok::eof) || PPTok.isNot(tok::eof)) {
00115     SourceLocation PPLoc = SM.getExpansionLoc(PPTok.getLocation());
00116 
00117     // If PPTok is from a different source file, ignore it.
00118     if (!SM.isWrittenInMainFile(PPLoc)) {
00119       PP.Lex(PPTok);
00120       continue;
00121     }
00122 
00123     // If the raw file hits a preprocessor directive, they will be extra tokens
00124     // in the raw file that don't exist in the preprocsesed file.  However, we
00125     // choose to preserve them in the output file and otherwise handle them
00126     // specially.
00127     if (RawTok.is(tok::hash) && RawTok.isAtStartOfLine()) {
00128       // If this is a #warning directive or #pragma mark (GNU extensions),
00129       // comment the line out.
00130       if (RawTokens[CurRawTok].is(tok::identifier)) {
00131         const IdentifierInfo *II = RawTokens[CurRawTok].getIdentifierInfo();
00132         if (II->getName() == "warning") {
00133           // Comment out #warning.
00134           RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
00135         } else if (II->getName() == "pragma" &&
00136                    RawTokens[CurRawTok+1].is(tok::identifier) &&
00137                    (RawTokens[CurRawTok+1].getIdentifierInfo()->getName() ==
00138                     "mark")) {
00139           // Comment out #pragma mark.
00140           RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
00141         }
00142       }
00143 
00144       // Otherwise, if this is a #include or some other directive, just leave it
00145       // in the file by skipping over the line.
00146       RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
00147       while (!RawTok.isAtStartOfLine() && RawTok.isNot(tok::eof))
00148         RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
00149       continue;
00150     }
00151 
00152     // Okay, both tokens are from the same file.  Get their offsets from the
00153     // start of the file.
00154     unsigned PPOffs = SM.getFileOffset(PPLoc);
00155     unsigned RawOffs = SM.getFileOffset(RawTok.getLocation());
00156 
00157     // If the offsets are the same and the token kind is the same, ignore them.
00158     if (PPOffs == RawOffs && isSameToken(RawTok, PPTok)) {
00159       RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
00160       PP.Lex(PPTok);
00161       continue;
00162     }
00163 
00164     // If the PP token is farther along than the raw token, something was
00165     // deleted.  Comment out the raw token.
00166     if (RawOffs <= PPOffs) {
00167       // Comment out a whole run of tokens instead of bracketing each one with
00168       // comments.  Add a leading space if RawTok didn't have one.
00169       bool HasSpace = RawTok.hasLeadingSpace();
00170       RB.InsertTextAfter(RawOffs, &" /*"[HasSpace]);
00171       unsigned EndPos;
00172 
00173       do {
00174         EndPos = RawOffs+RawTok.getLength();
00175 
00176         RawTok = GetNextRawTok(RawTokens, CurRawTok, true);
00177         RawOffs = SM.getFileOffset(RawTok.getLocation());
00178 
00179         if (RawTok.is(tok::comment)) {
00180           // Skip past the comment.
00181           RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
00182           break;
00183         }
00184 
00185       } while (RawOffs <= PPOffs && !RawTok.isAtStartOfLine() &&
00186                (PPOffs != RawOffs || !isSameToken(RawTok, PPTok)));
00187 
00188       RB.InsertTextBefore(EndPos, "*/");
00189       continue;
00190     }
00191 
00192     // Otherwise, there was a replacement an expansion.  Insert the new token
00193     // in the output buffer.  Insert the whole run of new tokens at once to get
00194     // them in the right order.
00195     unsigned InsertPos = PPOffs;
00196     std::string Expansion;
00197     while (PPOffs < RawOffs) {
00198       Expansion += ' ' + PP.getSpelling(PPTok);
00199       PP.Lex(PPTok);
00200       PPLoc = SM.getExpansionLoc(PPTok.getLocation());
00201       PPOffs = SM.getFileOffset(PPLoc);
00202     }
00203     Expansion += ' ';
00204     RB.InsertTextBefore(InsertPos, Expansion);
00205   }
00206 
00207   // Get the buffer corresponding to MainFileID.  If we haven't changed it, then
00208   // we are done.
00209   if (const RewriteBuffer *RewriteBuf =
00210       Rewrite.getRewriteBufferFor(SM.getMainFileID())) {
00211     //printf("Changed:\n");
00212     *OS << std::string(RewriteBuf->begin(), RewriteBuf->end());
00213   } else {
00214     fprintf(stderr, "No changes\n");
00215   }
00216   OS->flush();
00217 }