Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame^] | 1 | //===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This code simply runs the preprocessor on the input file and prints out the |
| 11 | // result. This is the traditional behavior of the -E option. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang.h" |
| 16 | #include "clang/Lex/Preprocessor.h" |
| 17 | #include "clang/Lex/Pragma.h" |
| 18 | #include "clang/Basic/SourceManager.h" |
| 19 | #include "llvm/Support/CommandLine.h" |
| 20 | #include <iostream> |
| 21 | using namespace llvm; |
| 22 | using namespace clang; |
| 23 | |
| 24 | static cl::opt<bool> |
| 25 | DisableLineMarkers("P", cl::desc("Disable linemarker output in -E mode")); |
| 26 | |
| 27 | static unsigned EModeCurLine; |
| 28 | static std::string EModeCurFilename; |
| 29 | static Preprocessor *EModePP; |
| 30 | static bool EmodeEmittedTokensOnThisLine; |
| 31 | static DirectoryLookup::DirType EmodeFileType =DirectoryLookup::NormalHeaderDir; |
| 32 | |
| 33 | static void MoveToLine(unsigned LineNo) { |
| 34 | // If this line is "close enough" to the original line, just print newlines, |
| 35 | // otherwise print a #line directive. |
| 36 | if (LineNo-EModeCurLine < 8) { |
| 37 | for (; EModeCurLine != LineNo; ++EModeCurLine) |
| 38 | std::cout << "\n"; |
| 39 | } else { |
| 40 | if (EmodeEmittedTokensOnThisLine) { |
| 41 | std::cout << "\n"; |
| 42 | EmodeEmittedTokensOnThisLine = false; |
| 43 | } |
| 44 | |
| 45 | EModeCurLine = LineNo; |
| 46 | if (DisableLineMarkers) return; |
| 47 | |
| 48 | std::cout << "# " << LineNo << " " << EModeCurFilename; |
| 49 | |
| 50 | if (EmodeFileType == DirectoryLookup::SystemHeaderDir) |
| 51 | std::cout << " 3"; |
| 52 | else if (EmodeFileType == DirectoryLookup::ExternCSystemHeaderDir) |
| 53 | std::cout << " 3 4"; |
| 54 | std::cout << "\n"; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /// HandleFileChange - Whenever the preprocessor enters or exits a #include file |
| 59 | /// it invokes this handler. Update our conception of the current |
| 60 | static void HandleFileChange(SourceLocation Loc, |
| 61 | Preprocessor::FileChangeReason Reason, |
| 62 | DirectoryLookup::DirType FileType) { |
| 63 | SourceManager &SourceMgr = EModePP->getSourceManager(); |
| 64 | |
| 65 | // Unless we are exiting a #include, make sure to skip ahead to the line the |
| 66 | // #include directive was at. |
| 67 | if (Reason == Preprocessor::EnterFile) { |
| 68 | SourceLocation IncludeLoc = SourceMgr.getIncludeLoc(Loc.getFileID()); |
| 69 | MoveToLine(SourceMgr.getLineNumber(IncludeLoc)); |
| 70 | } else if (Reason == Preprocessor::SystemHeaderPragma) { |
| 71 | MoveToLine(SourceMgr.getLineNumber(Loc)); |
| 72 | |
| 73 | // TODO GCC emits the # directive for this directive on the line AFTER the |
| 74 | // directive and emits a bunch of spaces that aren't needed. Emulate this |
| 75 | // strange behavior. |
| 76 | } |
| 77 | |
| 78 | EModeCurLine = SourceMgr.getLineNumber(Loc); |
| 79 | EModeCurFilename = Lexer::Stringify(SourceMgr.getSourceName(Loc)); |
| 80 | EmodeFileType = FileType; |
| 81 | |
| 82 | if (EmodeEmittedTokensOnThisLine) { |
| 83 | std::cout << "\n"; |
| 84 | EmodeEmittedTokensOnThisLine = false; |
| 85 | } |
| 86 | |
| 87 | if (DisableLineMarkers) return; |
| 88 | |
| 89 | std::cout << "# " << EModeCurLine << " " << EModeCurFilename; |
| 90 | switch (Reason) { |
| 91 | case Preprocessor::EnterFile: |
| 92 | std::cout << " 1"; |
| 93 | break; |
| 94 | case Preprocessor::ExitFile: |
| 95 | std::cout << " 2"; |
| 96 | break; |
| 97 | case Preprocessor::SystemHeaderPragma: break; |
| 98 | case Preprocessor::RenameFile: break; |
| 99 | } |
| 100 | |
| 101 | if (FileType == DirectoryLookup::SystemHeaderDir) |
| 102 | std::cout << " 3"; |
| 103 | else if (FileType == DirectoryLookup::ExternCSystemHeaderDir) |
| 104 | std::cout << " 3 4"; |
| 105 | |
| 106 | std::cout << "\n"; |
| 107 | } |
| 108 | |
| 109 | static void HandleIdent(SourceLocation Loc, const std::string &Val) { |
| 110 | SourceManager &SourceMgr = EModePP->getSourceManager(); |
| 111 | MoveToLine(SourceMgr.getLineNumber(Loc)); |
| 112 | |
| 113 | std::cout << "#ident " << Val; |
| 114 | EmodeEmittedTokensOnThisLine = true; |
| 115 | } |
| 116 | |
| 117 | /// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this |
| 118 | /// is called for the first token on each new line. |
| 119 | static void HandleFirstTokOnLine(LexerToken &Tok, Preprocessor &PP) { |
| 120 | // Figure out what line we went to and insert the appropriate number of |
| 121 | // newline characters. |
| 122 | unsigned LineNo = PP.getSourceManager().getLineNumber(Tok.getLocation()); |
| 123 | |
| 124 | // Move to the specified line. |
| 125 | MoveToLine(LineNo); |
| 126 | |
| 127 | |
| 128 | // Print out space characters so that the first token on a line is |
| 129 | // indented for easy reading. |
| 130 | unsigned ColNo = |
| 131 | PP.getSourceManager().getColumnNumber(Tok.getLocation()); |
| 132 | |
| 133 | // This hack prevents stuff like: |
| 134 | // #define HASH # |
| 135 | // HASH define foo bar |
| 136 | // From having the # character end up at column 1, which makes it so it |
| 137 | // is not handled as a #define next time through the preprocessor if in |
| 138 | // -fpreprocessed mode. |
| 139 | if (ColNo <= 1 && Tok.getKind() == tok::hash) |
| 140 | std::cout << ' '; |
| 141 | |
| 142 | // Otherwise, indent the appropriate number of spaces. |
| 143 | for (; ColNo > 1; --ColNo) |
| 144 | std::cout << ' '; |
| 145 | } |
| 146 | |
| 147 | struct UnknownPragmaHandler : public PragmaHandler { |
| 148 | const char *Prefix; |
| 149 | UnknownPragmaHandler(const char *prefix) : PragmaHandler(0), Prefix(prefix) {} |
| 150 | virtual void HandlePragma(Preprocessor &PP, LexerToken &PragmaTok) { |
| 151 | // Figure out what line we went to and insert the appropriate number of |
| 152 | // newline characters. |
| 153 | MoveToLine(PP.getSourceManager().getLineNumber(PragmaTok.getLocation())); |
| 154 | std::cout << Prefix; |
| 155 | |
| 156 | // Read and print all of the pragma tokens. |
| 157 | while (PragmaTok.getKind() != tok::eom) { |
| 158 | if (PragmaTok.hasLeadingSpace()) |
| 159 | std::cout << ' '; |
| 160 | std::cout << PP.getSpelling(PragmaTok); |
| 161 | PP.LexUnexpandedToken(PragmaTok); |
| 162 | } |
| 163 | std::cout << "\n"; |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | |
| 168 | /// DoPrintPreprocessedInput - This implements -E mode. |
| 169 | void clang::DoPrintPreprocessedInput(Preprocessor &PP) { |
| 170 | LexerToken Tok; |
| 171 | char Buffer[256]; |
| 172 | EModeCurLine = 0; |
| 173 | EModeCurFilename = "\"<uninit>\""; |
| 174 | PP.setFileChangeHandler(HandleFileChange); |
| 175 | PP.setIdentHandler(HandleIdent); |
| 176 | EModePP = &PP; |
| 177 | EmodeEmittedTokensOnThisLine = false; |
| 178 | |
| 179 | PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma")); |
| 180 | PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC")); |
| 181 | do { |
| 182 | PP.Lex(Tok); |
| 183 | |
| 184 | // If this token is at the start of a line. Emit the \n and indentation. |
| 185 | // FIXME: this shouldn't use the isAtStartOfLine flag. This should use a |
| 186 | // "newline callback" from the lexer. |
| 187 | // FIXME: For some tests, this fails just because there is no col# info from |
| 188 | // macro expansions! |
| 189 | if (Tok.isAtStartOfLine()) { |
| 190 | HandleFirstTokOnLine(Tok, PP); |
| 191 | } else if (Tok.hasLeadingSpace()) { |
| 192 | std::cout << ' '; |
| 193 | } |
| 194 | |
| 195 | if (Tok.getLength() < 256) { |
| 196 | unsigned Len = PP.getSpelling(Tok, Buffer); |
| 197 | Buffer[Len] = 0; |
| 198 | std::cout << Buffer; |
| 199 | } else { |
| 200 | std::cout << PP.getSpelling(Tok); |
| 201 | } |
| 202 | EmodeEmittedTokensOnThisLine = true; |
| 203 | } while (Tok.getKind() != tok::eof); |
| 204 | std::cout << "\n"; |
| 205 | } |
| 206 | |