Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 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/PPCallbacks.h" |
| 17 | #include "clang/Lex/Preprocessor.h" |
| 18 | #include "clang/Lex/Pragma.h" |
| 19 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 6619f66 | 2008-04-08 04:16:20 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
| 22 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 6619f66 | 2008-04-08 04:16:20 +0000 | [diff] [blame] | 23 | #include "llvm/System/Path.h" |
| 24 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 25 | #include "llvm/Config/config.h" |
Chris Lattner | 93b4f30 | 2008-08-17 01:47:12 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 27 | #include <cstdio> |
| 28 | using namespace clang; |
| 29 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // Preprocessed token printer |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | static llvm::cl::opt<bool> |
| 35 | DisableLineMarkers("P", llvm::cl::desc("Disable linemarker output in -E mode")); |
| 36 | static llvm::cl::opt<bool> |
| 37 | EnableCommentOutput("C", llvm::cl::desc("Enable comment output in -E mode")); |
| 38 | static llvm::cl::opt<bool> |
| 39 | EnableMacroCommentOutput("CC", |
| 40 | llvm::cl::desc("Enable comment output in -E mode, " |
| 41 | "even from macro expansions")); |
| 42 | |
| 43 | namespace { |
| 44 | class PrintPPOutputPPCallbacks : public PPCallbacks { |
| 45 | Preprocessor &PP; |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 46 | public: |
| 47 | llvm::raw_ostream &OS; |
| 48 | private: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 49 | unsigned CurLine; |
| 50 | bool EmittedTokensOnThisLine; |
| 51 | DirectoryLookup::DirType FileType; |
| 52 | llvm::SmallString<512> CurFilename; |
| 53 | public: |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 54 | PrintPPOutputPPCallbacks(Preprocessor &pp, llvm::raw_ostream &os) |
| 55 | : PP(pp), OS(os) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 56 | CurLine = 0; |
| 57 | CurFilename += "<uninit>"; |
| 58 | EmittedTokensOnThisLine = false; |
| 59 | FileType = DirectoryLookup::NormalHeaderDir; |
| 60 | } |
| 61 | |
| 62 | void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; } |
| 63 | bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; } |
| 64 | |
| 65 | virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 66 | DirectoryLookup::DirType FileType); |
| 67 | virtual void Ident(SourceLocation Loc, const std::string &str); |
| 68 | |
| 69 | |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 70 | bool HandleFirstTokOnLine(Token &Tok); |
| 71 | bool MoveToLine(SourceLocation Loc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 72 | bool AvoidConcat(const Token &PrevTok, const Token &Tok); |
| 73 | }; |
Chris Lattner | 6619f66 | 2008-04-08 04:16:20 +0000 | [diff] [blame] | 74 | } // end anonymous namespace |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 75 | |
| 76 | /// UToStr - Do itoa on the specified number, in-place in the specified buffer. |
| 77 | /// endptr points to the end of the buffer. |
| 78 | static char *UToStr(unsigned N, char *EndPtr) { |
| 79 | // Null terminate the buffer. |
| 80 | *--EndPtr = '\0'; |
| 81 | if (N == 0) // Zero is a special case. |
| 82 | *--EndPtr = '0'; |
| 83 | while (N) { |
| 84 | *--EndPtr = '0' + char(N % 10); |
| 85 | N /= 10; |
| 86 | } |
| 87 | return EndPtr; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /// MoveToLine - Move the output to the source line specified by the location |
| 92 | /// object. We can do this by emitting some number of \n's, or be emitting a |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 93 | /// #line directive. This returns false if already at the specified line, true |
| 94 | /// if some newlines were emitted. |
| 95 | bool PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 96 | if (DisableLineMarkers) { |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 97 | unsigned LineNo = PP.getSourceManager().getLogicalLineNumber(Loc); |
| 98 | if (LineNo == CurLine) return false; |
| 99 | |
| 100 | CurLine = LineNo; |
| 101 | |
| 102 | if (!EmittedTokensOnThisLine) |
| 103 | return true; |
| 104 | |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 105 | OS << '\n'; |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 106 | EmittedTokensOnThisLine = false; |
| 107 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | unsigned LineNo = PP.getSourceManager().getLogicalLineNumber(Loc); |
| 111 | |
| 112 | // If this line is "close enough" to the original line, just print newlines, |
| 113 | // otherwise print a #line directive. |
| 114 | if (LineNo-CurLine < 8) { |
| 115 | if (LineNo-CurLine == 1) |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 116 | OS << '\n'; |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 117 | else if (LineNo == CurLine) |
| 118 | return false; // Phys line moved, but logical line didn't. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 119 | else { |
| 120 | const char *NewLines = "\n\n\n\n\n\n\n\n"; |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 121 | OS.write(NewLines, LineNo-CurLine); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 122 | } |
Chris Lattner | 45ac817 | 2007-12-09 20:45:43 +0000 | [diff] [blame] | 123 | CurLine = LineNo; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 124 | } else { |
| 125 | if (EmittedTokensOnThisLine) { |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 126 | OS << '\n'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 127 | EmittedTokensOnThisLine = false; |
| 128 | } |
| 129 | |
| 130 | CurLine = LineNo; |
| 131 | |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 132 | OS << '#' << ' '; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 133 | char NumberBuffer[20]; |
| 134 | const char *NumStr = UToStr(LineNo, NumberBuffer+20); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 135 | OS.write(NumStr, (NumberBuffer+20)-NumStr-1); |
| 136 | OS << ' '; |
| 137 | OS << '"'; |
| 138 | OS.write(&CurFilename[0], CurFilename.size()); |
| 139 | OS << '"'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 140 | |
| 141 | if (FileType == DirectoryLookup::SystemHeaderDir) |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 142 | OS.write(" 3", 2); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 143 | else if (FileType == DirectoryLookup::ExternCSystemHeaderDir) |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 144 | OS.write(" 3 4", 4); |
| 145 | OS << '\n'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 146 | } |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 147 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
| 151 | /// FileChanged - Whenever the preprocessor enters or exits a #include file |
| 152 | /// it invokes this handler. Update our conception of the current source |
| 153 | /// position. |
| 154 | void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc, |
| 155 | FileChangeReason Reason, |
| 156 | DirectoryLookup::DirType FileType) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 157 | // Unless we are exiting a #include, make sure to skip ahead to the line the |
| 158 | // #include directive was at. |
| 159 | SourceManager &SourceMgr = PP.getSourceManager(); |
| 160 | if (Reason == PPCallbacks::EnterFile) { |
| 161 | MoveToLine(SourceMgr.getIncludeLoc(Loc)); |
| 162 | } else if (Reason == PPCallbacks::SystemHeaderPragma) { |
| 163 | MoveToLine(Loc); |
| 164 | |
| 165 | // TODO GCC emits the # directive for this directive on the line AFTER the |
| 166 | // directive and emits a bunch of spaces that aren't needed. Emulate this |
| 167 | // strange behavior. |
| 168 | } |
| 169 | |
| 170 | Loc = SourceMgr.getLogicalLoc(Loc); |
| 171 | CurLine = SourceMgr.getLineNumber(Loc); |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 172 | |
| 173 | if (DisableLineMarkers) return; |
| 174 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 175 | CurFilename.clear(); |
| 176 | CurFilename += SourceMgr.getSourceName(Loc); |
| 177 | Lexer::Stringify(CurFilename); |
| 178 | FileType = FileType; |
| 179 | |
| 180 | if (EmittedTokensOnThisLine) { |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 181 | OS << '\n'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 182 | EmittedTokensOnThisLine = false; |
| 183 | } |
| 184 | |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 185 | OS << '#' << ' '; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 186 | |
| 187 | char NumberBuffer[20]; |
| 188 | const char *NumStr = UToStr(CurLine, NumberBuffer+20); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 189 | OS.write(NumStr, (NumberBuffer+20)-NumStr-1); |
| 190 | OS << ' ' << '"'; |
| 191 | OS.write(&CurFilename[0], CurFilename.size()); |
| 192 | OS << '"'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 193 | |
| 194 | switch (Reason) { |
| 195 | case PPCallbacks::EnterFile: |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 196 | OS.write(" 1", 2); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 197 | break; |
| 198 | case PPCallbacks::ExitFile: |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 199 | OS.write(" 2", 2); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 200 | break; |
| 201 | case PPCallbacks::SystemHeaderPragma: break; |
| 202 | case PPCallbacks::RenameFile: break; |
| 203 | } |
| 204 | |
| 205 | if (FileType == DirectoryLookup::SystemHeaderDir) |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 206 | OS.write(" 3", 2); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 207 | else if (FileType == DirectoryLookup::ExternCSystemHeaderDir) |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 208 | OS.write(" 3 4", 4); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 210 | OS << '\n'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /// HandleIdent - Handle #ident directives when read by the preprocessor. |
| 214 | /// |
| 215 | void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) { |
| 216 | MoveToLine(Loc); |
| 217 | |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 218 | OS.write("#ident ", strlen("#ident ")); |
| 219 | OS.write(&S[0], S.size()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 220 | EmittedTokensOnThisLine = true; |
| 221 | } |
| 222 | |
| 223 | /// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 224 | /// is called for the first token on each new line. If this really is the start |
| 225 | /// of a new logical line, handle it and return true, otherwise return false. |
| 226 | /// This may not be the start of a logical line because the "start of line" |
| 227 | /// marker is set for physical lines, not logical ones. |
| 228 | bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 229 | // Figure out what line we went to and insert the appropriate number of |
| 230 | // newline characters. |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 231 | if (!MoveToLine(Tok.getLocation())) |
| 232 | return false; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 233 | |
| 234 | // Print out space characters so that the first token on a line is |
| 235 | // indented for easy reading. |
| 236 | const SourceManager &SourceMgr = PP.getSourceManager(); |
| 237 | unsigned ColNo = SourceMgr.getLogicalColumnNumber(Tok.getLocation()); |
| 238 | |
| 239 | // This hack prevents stuff like: |
| 240 | // #define HASH # |
| 241 | // HASH define foo bar |
| 242 | // From having the # character end up at column 1, which makes it so it |
| 243 | // is not handled as a #define next time through the preprocessor if in |
| 244 | // -fpreprocessed mode. |
Chris Lattner | 3b49415 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 245 | if (ColNo <= 1 && Tok.is(tok::hash)) |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 246 | OS << ' '; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 247 | |
| 248 | // Otherwise, indent the appropriate number of spaces. |
| 249 | for (; ColNo > 1; --ColNo) |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 250 | OS << ' '; |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 251 | |
| 252 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | namespace { |
| 256 | struct UnknownPragmaHandler : public PragmaHandler { |
| 257 | const char *Prefix; |
| 258 | PrintPPOutputPPCallbacks *Callbacks; |
| 259 | |
| 260 | UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks) |
| 261 | : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {} |
| 262 | virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) { |
| 263 | // Figure out what line we went to and insert the appropriate number of |
| 264 | // newline characters. |
| 265 | Callbacks->MoveToLine(PragmaTok.getLocation()); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 266 | Callbacks->OS.write(Prefix, strlen(Prefix)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 267 | |
| 268 | // Read and print all of the pragma tokens. |
Chris Lattner | 3b49415 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 269 | while (PragmaTok.isNot(tok::eom)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 270 | if (PragmaTok.hasLeadingSpace()) |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 271 | Callbacks->OS << ' '; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 272 | std::string TokSpell = PP.getSpelling(PragmaTok); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 273 | Callbacks->OS.write(&TokSpell[0], TokSpell.size()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 274 | PP.LexUnexpandedToken(PragmaTok); |
| 275 | } |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 276 | Callbacks->OS << '\n'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 277 | } |
| 278 | }; |
| 279 | } // end anonymous namespace |
| 280 | |
| 281 | |
| 282 | enum AvoidConcatInfo { |
| 283 | /// By default, a token never needs to avoid concatenation. Most tokens (e.g. |
| 284 | /// ',', ')', etc) don't cause a problem when concatenated. |
| 285 | aci_never_avoid_concat = 0, |
| 286 | |
| 287 | /// aci_custom_firstchar - AvoidConcat contains custom code to handle this |
| 288 | /// token's requirements, and it needs to know the first character of the |
| 289 | /// token. |
| 290 | aci_custom_firstchar = 1, |
| 291 | |
| 292 | /// aci_custom - AvoidConcat contains custom code to handle this token's |
| 293 | /// requirements, but it doesn't need to know the first character of the |
| 294 | /// token. |
| 295 | aci_custom = 2, |
| 296 | |
| 297 | /// aci_avoid_equal - Many tokens cannot be safely followed by an '=' |
| 298 | /// character. For example, "<<" turns into "<<=" when followed by an =. |
| 299 | aci_avoid_equal = 4 |
| 300 | }; |
| 301 | |
| 302 | /// This array contains information for each token on what action to take when |
| 303 | /// avoiding concatenation of tokens in the AvoidConcat method. |
| 304 | static char TokenInfo[tok::NUM_TOKENS]; |
| 305 | |
| 306 | /// InitAvoidConcatTokenInfo - Tokens that must avoid concatenation should be |
| 307 | /// marked by this function. |
| 308 | static void InitAvoidConcatTokenInfo() { |
| 309 | // These tokens have custom code in AvoidConcat. |
| 310 | TokenInfo[tok::identifier ] |= aci_custom; |
| 311 | TokenInfo[tok::numeric_constant] |= aci_custom_firstchar; |
| 312 | TokenInfo[tok::period ] |= aci_custom_firstchar; |
| 313 | TokenInfo[tok::amp ] |= aci_custom_firstchar; |
| 314 | TokenInfo[tok::plus ] |= aci_custom_firstchar; |
| 315 | TokenInfo[tok::minus ] |= aci_custom_firstchar; |
| 316 | TokenInfo[tok::slash ] |= aci_custom_firstchar; |
| 317 | TokenInfo[tok::less ] |= aci_custom_firstchar; |
| 318 | TokenInfo[tok::greater ] |= aci_custom_firstchar; |
| 319 | TokenInfo[tok::pipe ] |= aci_custom_firstchar; |
| 320 | TokenInfo[tok::percent ] |= aci_custom_firstchar; |
| 321 | TokenInfo[tok::colon ] |= aci_custom_firstchar; |
| 322 | TokenInfo[tok::hash ] |= aci_custom_firstchar; |
| 323 | TokenInfo[tok::arrow ] |= aci_custom_firstchar; |
| 324 | |
| 325 | // These tokens change behavior if followed by an '='. |
| 326 | TokenInfo[tok::amp ] |= aci_avoid_equal; // &= |
| 327 | TokenInfo[tok::plus ] |= aci_avoid_equal; // += |
| 328 | TokenInfo[tok::minus ] |= aci_avoid_equal; // -= |
| 329 | TokenInfo[tok::slash ] |= aci_avoid_equal; // /= |
| 330 | TokenInfo[tok::less ] |= aci_avoid_equal; // <= |
| 331 | TokenInfo[tok::greater ] |= aci_avoid_equal; // >= |
| 332 | TokenInfo[tok::pipe ] |= aci_avoid_equal; // |= |
| 333 | TokenInfo[tok::percent ] |= aci_avoid_equal; // %= |
| 334 | TokenInfo[tok::star ] |= aci_avoid_equal; // *= |
| 335 | TokenInfo[tok::exclaim ] |= aci_avoid_equal; // != |
| 336 | TokenInfo[tok::lessless ] |= aci_avoid_equal; // <<= |
| 337 | TokenInfo[tok::greaterequal] |= aci_avoid_equal; // >>= |
| 338 | TokenInfo[tok::caret ] |= aci_avoid_equal; // ^= |
| 339 | TokenInfo[tok::equal ] |= aci_avoid_equal; // == |
| 340 | } |
| 341 | |
Chris Lattner | afa4012 | 2008-01-15 05:22:14 +0000 | [diff] [blame] | 342 | /// StartsWithL - Return true if the spelling of this token starts with 'L'. |
Chris Lattner | 400f024 | 2008-01-15 05:14:19 +0000 | [diff] [blame] | 343 | static bool StartsWithL(const Token &Tok, Preprocessor &PP) { |
Chris Lattner | 400f024 | 2008-01-15 05:14:19 +0000 | [diff] [blame] | 344 | if (!Tok.needsCleaning()) { |
| 345 | SourceManager &SrcMgr = PP.getSourceManager(); |
| 346 | return *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation())) |
| 347 | == 'L'; |
| 348 | } |
| 349 | |
| 350 | if (Tok.getLength() < 256) { |
Chris Lattner | afa4012 | 2008-01-15 05:22:14 +0000 | [diff] [blame] | 351 | char Buffer[256]; |
Chris Lattner | 400f024 | 2008-01-15 05:14:19 +0000 | [diff] [blame] | 352 | const char *TokPtr = Buffer; |
| 353 | PP.getSpelling(Tok, TokPtr); |
| 354 | return TokPtr[0] == 'L'; |
| 355 | } |
| 356 | |
| 357 | return PP.getSpelling(Tok)[0] == 'L'; |
| 358 | } |
| 359 | |
Chris Lattner | afa4012 | 2008-01-15 05:22:14 +0000 | [diff] [blame] | 360 | /// IsIdentifierL - Return true if the spelling of this token is literally 'L'. |
| 361 | static bool IsIdentifierL(const Token &Tok, Preprocessor &PP) { |
| 362 | if (!Tok.needsCleaning()) { |
| 363 | if (Tok.getLength() != 1) |
| 364 | return false; |
| 365 | SourceManager &SrcMgr = PP.getSourceManager(); |
| 366 | return *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation())) |
| 367 | == 'L'; |
| 368 | } |
| 369 | |
| 370 | if (Tok.getLength() < 256) { |
| 371 | char Buffer[256]; |
| 372 | const char *TokPtr = Buffer; |
| 373 | if (PP.getSpelling(Tok, TokPtr) != 1) |
| 374 | return false; |
| 375 | return TokPtr[0] == 'L'; |
| 376 | } |
| 377 | |
| 378 | return PP.getSpelling(Tok) == "L"; |
| 379 | } |
| 380 | |
| 381 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 382 | /// AvoidConcat - If printing PrevTok immediately followed by Tok would cause |
| 383 | /// the two individual tokens to be lexed as a single token, return true (which |
| 384 | /// causes a space to be printed between them). This allows the output of -E |
| 385 | /// mode to be lexed to the same token stream as lexing the input directly |
| 386 | /// would. |
| 387 | /// |
| 388 | /// This code must conservatively return true if it doesn't want to be 100% |
| 389 | /// accurate. This will cause the output to include extra space characters, but |
| 390 | /// the resulting output won't have incorrect concatenations going on. Examples |
| 391 | /// include "..", which we print with a space between, because we don't want to |
| 392 | /// track enough to tell "x.." from "...". |
| 393 | bool PrintPPOutputPPCallbacks::AvoidConcat(const Token &PrevTok, |
| 394 | const Token &Tok) { |
| 395 | char Buffer[256]; |
| 396 | |
| 397 | tok::TokenKind PrevKind = PrevTok.getKind(); |
| 398 | if (PrevTok.getIdentifierInfo()) // Language keyword or named operator. |
| 399 | PrevKind = tok::identifier; |
| 400 | |
| 401 | // Look up information on when we should avoid concatenation with prevtok. |
| 402 | unsigned ConcatInfo = TokenInfo[PrevKind]; |
| 403 | |
| 404 | // If prevtok never causes a problem for anything after it, return quickly. |
| 405 | if (ConcatInfo == 0) return false; |
| 406 | |
| 407 | if (ConcatInfo & aci_avoid_equal) { |
| 408 | // If the next token is '=' or '==', avoid concatenation. |
Chris Lattner | 3b49415 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 409 | if (Tok.is(tok::equal) || Tok.is(tok::equalequal)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 410 | return true; |
| 411 | ConcatInfo &= ~aci_avoid_equal; |
| 412 | } |
| 413 | |
| 414 | if (ConcatInfo == 0) return false; |
| 415 | |
| 416 | |
| 417 | |
| 418 | // Basic algorithm: we look at the first character of the second token, and |
| 419 | // determine whether it, if appended to the first token, would form (or would |
| 420 | // contribute) to a larger token if concatenated. |
| 421 | char FirstChar = 0; |
| 422 | if (ConcatInfo & aci_custom) { |
| 423 | // If the token does not need to know the first character, don't get it. |
| 424 | } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 425 | // Avoid spelling identifiers, the most common form of token. |
| 426 | FirstChar = II->getName()[0]; |
| 427 | } else if (!Tok.needsCleaning()) { |
| 428 | SourceManager &SrcMgr = PP.getSourceManager(); |
| 429 | FirstChar = |
| 430 | *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation())); |
| 431 | } else if (Tok.getLength() < 256) { |
| 432 | const char *TokPtr = Buffer; |
| 433 | PP.getSpelling(Tok, TokPtr); |
| 434 | FirstChar = TokPtr[0]; |
| 435 | } else { |
| 436 | FirstChar = PP.getSpelling(Tok)[0]; |
| 437 | } |
| 438 | |
| 439 | switch (PrevKind) { |
| 440 | default: assert(0 && "InitAvoidConcatTokenInfo built wrong"); |
| 441 | case tok::identifier: // id+id or id+number or id+L"foo". |
Chris Lattner | 3b49415 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 442 | if (Tok.is(tok::numeric_constant) || Tok.getIdentifierInfo() || |
| 443 | Tok.is(tok::wide_string_literal) /* || |
| 444 | Tok.is(tok::wide_char_literal)*/) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 445 | return true; |
Chris Lattner | 400f024 | 2008-01-15 05:14:19 +0000 | [diff] [blame] | 446 | |
| 447 | // If this isn't identifier + string, we're done. |
| 448 | if (Tok.isNot(tok::char_constant) && Tok.isNot(tok::string_literal)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 449 | return false; |
| 450 | |
| 451 | // FIXME: need a wide_char_constant! |
Chris Lattner | 400f024 | 2008-01-15 05:14:19 +0000 | [diff] [blame] | 452 | |
| 453 | // If the string was a wide string L"foo" or wide char L'f', it would concat |
| 454 | // with the previous identifier into fooL"bar". Avoid this. |
| 455 | if (StartsWithL(Tok, PP)) |
| 456 | return true; |
| 457 | |
Chris Lattner | afa4012 | 2008-01-15 05:22:14 +0000 | [diff] [blame] | 458 | // Otherwise, this is a narrow character or string. If the *identifier* is |
| 459 | // a literal 'L', avoid pasting L "foo" -> L"foo". |
| 460 | return IsIdentifierL(PrevTok, PP); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 461 | case tok::numeric_constant: |
Chris Lattner | 3b49415 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 462 | return isalnum(FirstChar) || Tok.is(tok::numeric_constant) || |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 463 | FirstChar == '+' || FirstChar == '-' || FirstChar == '.'; |
| 464 | case tok::period: // ..., .*, .1234 |
| 465 | return FirstChar == '.' || FirstChar == '*' || isdigit(FirstChar); |
| 466 | case tok::amp: // && |
| 467 | return FirstChar == '&'; |
| 468 | case tok::plus: // ++ |
| 469 | return FirstChar == '+'; |
| 470 | case tok::minus: // --, ->, ->* |
| 471 | return FirstChar == '-' || FirstChar == '>'; |
| 472 | case tok::slash: //, /*, // |
| 473 | return FirstChar == '*' || FirstChar == '/'; |
| 474 | case tok::less: // <<, <<=, <:, <% |
| 475 | return FirstChar == '<' || FirstChar == ':' || FirstChar == '%'; |
| 476 | case tok::greater: // >>, >>= |
| 477 | return FirstChar == '>'; |
| 478 | case tok::pipe: // || |
| 479 | return FirstChar == '|'; |
| 480 | case tok::percent: // %>, %: |
| 481 | return FirstChar == '>' || FirstChar == ':'; |
| 482 | case tok::colon: // ::, :> |
| 483 | return FirstChar == ':' || FirstChar == '>'; |
| 484 | case tok::hash: // ##, #@, %:%: |
| 485 | return FirstChar == '#' || FirstChar == '@' || FirstChar == '%'; |
| 486 | case tok::arrow: // ->* |
| 487 | return FirstChar == '*'; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /// DoPrintPreprocessedInput - This implements -E mode. |
| 492 | /// |
Chris Lattner | 6619f66 | 2008-04-08 04:16:20 +0000 | [diff] [blame] | 493 | void clang::DoPrintPreprocessedInput(Preprocessor &PP, |
| 494 | const std::string &OutFile) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 495 | // Inform the preprocessor whether we want it to retain comments or not, due |
| 496 | // to -C or -CC. |
| 497 | PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 498 | InitAvoidConcatTokenInfo(); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 499 | |
| 500 | |
| 501 | // Open the output buffer. |
Chris Lattner | 1be9690 | 2008-08-17 03:54:39 +0000 | [diff] [blame] | 502 | std::string Err; |
Chris Lattner | 03074ed | 2008-08-17 07:09:08 +0000 | [diff] [blame^] | 503 | llvm::raw_fd_ostream OS(OutFile.empty() ? "-" : OutFile.c_str(), Err); |
Chris Lattner | 1be9690 | 2008-08-17 03:54:39 +0000 | [diff] [blame] | 504 | if (!Err.empty()) { |
| 505 | fprintf(stderr, "%s\n", Err.c_str()); |
| 506 | exit(1); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 507 | } |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 508 | |
Chris Lattner | 1be9690 | 2008-08-17 03:54:39 +0000 | [diff] [blame] | 509 | OS.SetBufferSize(64*1024); |
| 510 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 511 | |
| 512 | Token Tok, PrevTok; |
| 513 | char Buffer[256]; |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 514 | PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP, OS); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 515 | PP.setPPCallbacks(Callbacks); |
| 516 | |
| 517 | PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks)); |
| 518 | PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks)); |
| 519 | |
| 520 | // After we have configured the preprocessor, enter the main file. |
| 521 | |
| 522 | // Start parsing the specified input file. |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 523 | PP.EnterMainSourceFile(); |
Chris Lattner | 3eddc86 | 2007-10-10 20:45:16 +0000 | [diff] [blame] | 524 | |
| 525 | // Consume all of the tokens that come from the predefines buffer. Those |
| 526 | // should not be emitted into the output and are guaranteed to be at the |
| 527 | // start. |
| 528 | const SourceManager &SourceMgr = PP.getSourceManager(); |
| 529 | do PP.Lex(Tok); |
Chris Lattner | 890c593 | 2007-10-10 23:31:03 +0000 | [diff] [blame] | 530 | while (Tok.isNot(tok::eof) && Tok.getLocation().isFileID() && |
Chris Lattner | 3eddc86 | 2007-10-10 20:45:16 +0000 | [diff] [blame] | 531 | !strcmp(SourceMgr.getSourceName(Tok.getLocation()), "<predefines>")); |
| 532 | |
| 533 | while (1) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 534 | |
| 535 | // If this token is at the start of a line, emit newlines if needed. |
Chris Lattner | 6c45129 | 2007-12-09 21:11:08 +0000 | [diff] [blame] | 536 | if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) { |
| 537 | // done. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 538 | } else if (Tok.hasLeadingSpace() || |
| 539 | // If we haven't emitted a token on this line yet, PrevTok isn't |
| 540 | // useful to look at and no concatenation could happen anyway. |
| 541 | (Callbacks->hasEmittedTokensOnThisLine() && |
| 542 | // Don't print "-" next to "-", it would form "--". |
| 543 | Callbacks->AvoidConcat(PrevTok, Tok))) { |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 544 | OS << ' '; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | if (IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 548 | const char *Str = II->getName(); |
| 549 | unsigned Len = Tok.needsCleaning() ? strlen(Str) : Tok.getLength(); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 550 | OS.write(Str, Len); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 551 | } else if (Tok.getLength() < 256) { |
| 552 | const char *TokPtr = Buffer; |
| 553 | unsigned Len = PP.getSpelling(Tok, TokPtr); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 554 | OS.write(TokPtr, Len); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 555 | } else { |
| 556 | std::string S = PP.getSpelling(Tok); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 557 | OS.write(&S[0], S.size()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 558 | } |
| 559 | Callbacks->SetEmittedTokensOnThisLine(); |
Chris Lattner | 3eddc86 | 2007-10-10 20:45:16 +0000 | [diff] [blame] | 560 | |
| 561 | if (Tok.is(tok::eof)) break; |
| 562 | |
| 563 | PrevTok = Tok; |
| 564 | PP.Lex(Tok); |
| 565 | } |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 566 | OS << '\n'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 567 | |
Chris Lattner | cb7f180 | 2008-08-17 07:07:01 +0000 | [diff] [blame] | 568 | // Flush the ostream. |
| 569 | OS.flush(); |
Chris Lattner | 2149422 | 2008-08-17 03:12:02 +0000 | [diff] [blame] | 570 | |
| 571 | // If an error occurred, remove the output file. |
| 572 | if (PP.getDiagnostics().hasErrorOccurred() && !OutFile.empty()) |
| 573 | llvm::sys::Path(OutFile).eraseFromDisk(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 574 | } |
| 575 | |