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" |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
| 21 | #include "llvm/Config/config.h" |
Chris Lattner | deb3701 | 2006-07-04 19:24:06 +0000 | [diff] [blame] | 22 | #include <cstdio> |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | using namespace clang; |
| 25 | |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | // Simple buffered I/O |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | // |
| 30 | // Empirically, iostream is over 30% slower than stdio for this workload, and |
| 31 | // stdio itself isn't very well suited. The problem with stdio is use of |
| 32 | // putchar_unlocked. We have many newline characters that need to be emitted, |
| 33 | // but stdio needs to do extra checks to handle line buffering mode. These |
| 34 | // extra checks make putchar_unlocked fall off its inlined code path, hitting |
| 35 | // slow system code. In practice, using 'write' directly makes 'clang -E -P' |
| 36 | // about 10% faster than using the stdio path on darwin. |
| 37 | |
| 38 | #ifdef HAVE_UNISTD_H |
| 39 | #include <unistd.h> |
| 40 | #else |
| 41 | #define USE_STDIO 1 |
| 42 | #endif |
| 43 | |
| 44 | static char *OutBufStart = 0, *OutBufEnd, *OutBufCur; |
| 45 | |
| 46 | /// InitOutputBuffer - Initialize our output buffer. |
| 47 | /// |
| 48 | static void InitOutputBuffer() { |
| 49 | #ifndef USE_STDIO |
| 50 | OutBufStart = new char[64*1024]; |
| 51 | OutBufEnd = OutBufStart+64*1024; |
| 52 | OutBufCur = OutBufStart; |
| 53 | #endif |
| 54 | } |
| 55 | |
| 56 | /// FlushBuffer - Write the accumulated bytes to the output stream. |
| 57 | /// |
| 58 | static void FlushBuffer() { |
| 59 | #ifndef USE_STDIO |
| 60 | write(STDOUT_FILENO, OutBufStart, OutBufCur-OutBufStart); |
| 61 | OutBufCur = OutBufStart; |
| 62 | #endif |
| 63 | } |
| 64 | |
| 65 | /// CleanupOutputBuffer - Finish up output. |
| 66 | /// |
| 67 | static void CleanupOutputBuffer() { |
| 68 | #ifndef USE_STDIO |
| 69 | FlushBuffer(); |
| 70 | delete [] OutBufStart; |
| 71 | #endif |
| 72 | } |
| 73 | |
| 74 | static void OutputChar(char c) { |
| 75 | #ifdef USE_STDIO |
| 76 | putchar_unlocked(c); |
| 77 | #else |
| 78 | if (OutBufCur >= OutBufEnd) |
| 79 | FlushBuffer(); |
| 80 | *OutBufCur++ = c; |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | static void OutputString(const char *Ptr, unsigned Size) { |
| 85 | #ifdef USE_STDIO |
| 86 | fwrite(Ptr, Size, 1, stdout); |
| 87 | #else |
| 88 | if (OutBufCur+Size >= OutBufEnd) |
| 89 | FlushBuffer(); |
| 90 | memcpy(OutBufCur, Ptr, Size); |
| 91 | OutBufCur += Size; |
| 92 | #endif |
| 93 | } |
| 94 | |
| 95 | |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | // Preprocessed token printer |
| 98 | //===----------------------------------------------------------------------===// |
| 99 | |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 100 | static cl::opt<bool> |
| 101 | DisableLineMarkers("P", cl::desc("Disable linemarker output in -E mode")); |
Chris Lattner | 457fc15 | 2006-07-29 06:30:25 +0000 | [diff] [blame^] | 102 | static cl::opt<bool> |
| 103 | EnableCommentOutput("C", cl::desc("Enable comment output in -E mode")); |
| 104 | static cl::opt<bool> |
| 105 | EnableMacroCommentOutput("CC", cl::desc("Enable comment output in -E mode, " |
| 106 | "even from macro expansions")); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 107 | |
| 108 | static unsigned EModeCurLine; |
| 109 | static std::string EModeCurFilename; |
| 110 | static Preprocessor *EModePP; |
| 111 | static bool EmodeEmittedTokensOnThisLine; |
| 112 | static DirectoryLookup::DirType EmodeFileType =DirectoryLookup::NormalHeaderDir; |
| 113 | |
Chris Lattner | 728b4dc | 2006-07-04 21:28:37 +0000 | [diff] [blame] | 114 | /// MoveToLine - Move the output to the source line specified by the location |
| 115 | /// object. We can do this by emitting some number of \n's, or be emitting a |
| 116 | /// #line directive. |
Chris Lattner | 3338ba8 | 2006-07-04 21:19:39 +0000 | [diff] [blame] | 117 | static void MoveToLine(SourceLocation Loc) { |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 118 | if (DisableLineMarkers) { |
| 119 | if (EmodeEmittedTokensOnThisLine) { |
| 120 | OutputChar('\n'); |
| 121 | EmodeEmittedTokensOnThisLine = false; |
| 122 | } |
| 123 | return; |
| 124 | } |
Chris Lattner | 3338ba8 | 2006-07-04 21:19:39 +0000 | [diff] [blame] | 125 | |
| 126 | unsigned LineNo = EModePP->getSourceManager().getLineNumber(Loc); |
| 127 | |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 128 | // If this line is "close enough" to the original line, just print newlines, |
| 129 | // otherwise print a #line directive. |
| 130 | if (LineNo-EModeCurLine < 8) { |
Chris Lattner | deb3701 | 2006-07-04 19:24:06 +0000 | [diff] [blame] | 131 | unsigned CurLine = EModeCurLine; |
Chris Lattner | deb3701 | 2006-07-04 19:24:06 +0000 | [diff] [blame] | 132 | for (; CurLine != LineNo; ++CurLine) |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 133 | OutputChar('\n'); |
Chris Lattner | deb3701 | 2006-07-04 19:24:06 +0000 | [diff] [blame] | 134 | EModeCurLine = CurLine; |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 135 | } else { |
| 136 | if (EmodeEmittedTokensOnThisLine) { |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 137 | OutputChar('\n'); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 138 | EmodeEmittedTokensOnThisLine = false; |
| 139 | } |
| 140 | |
| 141 | EModeCurLine = LineNo; |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 142 | |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 143 | OutputChar('#'); |
| 144 | OutputChar(' '); |
| 145 | std::string Num = utostr_32(LineNo); |
| 146 | OutputString(&Num[0], Num.size()); |
| 147 | OutputChar(' '); |
| 148 | OutputString(&EModeCurFilename[0], EModeCurFilename.size()); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 149 | |
| 150 | if (EmodeFileType == DirectoryLookup::SystemHeaderDir) |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 151 | OutputString(" 3", 2); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 152 | else if (EmodeFileType == DirectoryLookup::ExternCSystemHeaderDir) |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 153 | OutputString(" 3 4", 4); |
| 154 | OutputChar('\n'); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | /// HandleFileChange - Whenever the preprocessor enters or exits a #include file |
| 159 | /// it invokes this handler. Update our conception of the current |
| 160 | static void HandleFileChange(SourceLocation Loc, |
| 161 | Preprocessor::FileChangeReason Reason, |
| 162 | DirectoryLookup::DirType FileType) { |
Chris Lattner | 03cbe1f | 2006-07-04 21:24:33 +0000 | [diff] [blame] | 163 | if (DisableLineMarkers) return; |
Chris Lattner | 73b6a2f | 2006-07-04 19:40:52 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 165 | // Unless we are exiting a #include, make sure to skip ahead to the line the |
| 166 | // #include directive was at. |
Chris Lattner | ff3f5f4 | 2006-07-04 21:25:59 +0000 | [diff] [blame] | 167 | SourceManager &SourceMgr = EModePP->getSourceManager(); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 168 | if (Reason == Preprocessor::EnterFile) { |
Chris Lattner | 3338ba8 | 2006-07-04 21:19:39 +0000 | [diff] [blame] | 169 | MoveToLine(SourceMgr.getIncludeLoc(Loc.getFileID())); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 170 | } else if (Reason == Preprocessor::SystemHeaderPragma) { |
Chris Lattner | 3338ba8 | 2006-07-04 21:19:39 +0000 | [diff] [blame] | 171 | MoveToLine(Loc); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 172 | |
| 173 | // TODO GCC emits the # directive for this directive on the line AFTER the |
| 174 | // directive and emits a bunch of spaces that aren't needed. Emulate this |
| 175 | // strange behavior. |
| 176 | } |
| 177 | |
| 178 | EModeCurLine = SourceMgr.getLineNumber(Loc); |
Chris Lattner | ecc39e9 | 2006-07-15 05:23:31 +0000 | [diff] [blame] | 179 | EModeCurFilename = '"' + Lexer::Stringify(SourceMgr.getSourceName(Loc)) + '"'; |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 180 | EmodeFileType = FileType; |
| 181 | |
| 182 | if (EmodeEmittedTokensOnThisLine) { |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 183 | OutputChar('\n'); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 184 | EmodeEmittedTokensOnThisLine = false; |
| 185 | } |
| 186 | |
| 187 | if (DisableLineMarkers) return; |
| 188 | |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 189 | OutputChar('#'); |
| 190 | OutputChar(' '); |
| 191 | std::string Num = utostr_32(EModeCurLine); |
| 192 | OutputString(&Num[0], Num.size()); |
| 193 | OutputChar(' '); |
| 194 | OutputString(&EModeCurFilename[0], EModeCurFilename.size()); |
| 195 | |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 196 | switch (Reason) { |
Chris Lattner | 3338ba8 | 2006-07-04 21:19:39 +0000 | [diff] [blame] | 197 | case Preprocessor::EnterFile: |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 198 | OutputString(" 1", 2); |
Chris Lattner | 3338ba8 | 2006-07-04 21:19:39 +0000 | [diff] [blame] | 199 | break; |
| 200 | case Preprocessor::ExitFile: |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 201 | OutputString(" 2", 2); |
Chris Lattner | 3338ba8 | 2006-07-04 21:19:39 +0000 | [diff] [blame] | 202 | break; |
| 203 | case Preprocessor::SystemHeaderPragma: break; |
| 204 | case Preprocessor::RenameFile: break; |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | if (FileType == DirectoryLookup::SystemHeaderDir) |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 208 | OutputString(" 3", 2); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 209 | else if (FileType == DirectoryLookup::ExternCSystemHeaderDir) |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 210 | OutputString(" 3 4", 4); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 211 | |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 212 | OutputChar('\n'); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Chris Lattner | 728b4dc | 2006-07-04 21:28:37 +0000 | [diff] [blame] | 215 | /// HandleIdent - Handle #ident directives when read by the preprocessor. |
| 216 | /// |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 217 | static void HandleIdent(SourceLocation Loc, const std::string &Val) { |
Chris Lattner | 3338ba8 | 2006-07-04 21:19:39 +0000 | [diff] [blame] | 218 | MoveToLine(Loc); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 219 | |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 220 | OutputString("#ident ", strlen("#ident ")); |
| 221 | OutputString(&Val[0], Val.size()); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 222 | EmodeEmittedTokensOnThisLine = true; |
| 223 | } |
| 224 | |
| 225 | /// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this |
| 226 | /// is called for the first token on each new line. |
| 227 | static void HandleFirstTokOnLine(LexerToken &Tok, Preprocessor &PP) { |
| 228 | // Figure out what line we went to and insert the appropriate number of |
| 229 | // newline characters. |
Chris Lattner | 3338ba8 | 2006-07-04 21:19:39 +0000 | [diff] [blame] | 230 | MoveToLine(Tok.getLocation()); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 231 | |
| 232 | // Print out space characters so that the first token on a line is |
| 233 | // indented for easy reading. |
| 234 | unsigned ColNo = |
| 235 | PP.getSourceManager().getColumnNumber(Tok.getLocation()); |
| 236 | |
| 237 | // This hack prevents stuff like: |
| 238 | // #define HASH # |
| 239 | // HASH define foo bar |
| 240 | // From having the # character end up at column 1, which makes it so it |
| 241 | // is not handled as a #define next time through the preprocessor if in |
| 242 | // -fpreprocessed mode. |
| 243 | if (ColNo <= 1 && Tok.getKind() == tok::hash) |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 244 | OutputChar(' '); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 245 | |
| 246 | // Otherwise, indent the appropriate number of spaces. |
| 247 | for (; ColNo > 1; --ColNo) |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 248 | OutputChar(' '); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Chris Lattner | 5de858c | 2006-07-04 19:04:44 +0000 | [diff] [blame] | 251 | namespace { |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 252 | struct UnknownPragmaHandler : public PragmaHandler { |
| 253 | const char *Prefix; |
| 254 | UnknownPragmaHandler(const char *prefix) : PragmaHandler(0), Prefix(prefix) {} |
| 255 | virtual void HandlePragma(Preprocessor &PP, LexerToken &PragmaTok) { |
| 256 | // Figure out what line we went to and insert the appropriate number of |
| 257 | // newline characters. |
Chris Lattner | 3338ba8 | 2006-07-04 21:19:39 +0000 | [diff] [blame] | 258 | MoveToLine(PragmaTok.getLocation()); |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 259 | OutputString(Prefix, strlen(Prefix)); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 260 | |
| 261 | // Read and print all of the pragma tokens. |
| 262 | while (PragmaTok.getKind() != tok::eom) { |
| 263 | if (PragmaTok.hasLeadingSpace()) |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 264 | OutputChar(' '); |
| 265 | std::string TokSpell = PP.getSpelling(PragmaTok); |
| 266 | OutputString(&TokSpell[0], TokSpell.size()); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 267 | PP.LexUnexpandedToken(PragmaTok); |
| 268 | } |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 269 | OutputChar('\n'); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 270 | } |
| 271 | }; |
Chris Lattner | 5de858c | 2006-07-04 19:04:44 +0000 | [diff] [blame] | 272 | } // end anonymous namespace |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 331ad77 | 2006-07-28 06:56:01 +0000 | [diff] [blame] | 274 | /// AvoidConcat - If printing PrevTok immediately followed by Tok would cause |
| 275 | /// the two individual tokens to be lexed as a single token, return true (which |
| 276 | /// causes a space to be printed between them). This allows the output of -E |
| 277 | /// mode to be lexed to the same token stream as lexing the input directly |
| 278 | /// would. |
| 279 | /// |
| 280 | /// This code must conservatively return true if it doesn't want to be 100% |
| 281 | /// accurate. This will cause the output to include extra space characters, but |
| 282 | /// the resulting output won't have incorrect concatenations going on. Examples |
| 283 | /// include "..", which we print with a space between, because we don't want to |
| 284 | /// track enough to tell "x.." from "...". |
| 285 | static bool AvoidConcat(const LexerToken &PrevTok, const LexerToken &Tok, |
| 286 | Preprocessor &PP) { |
| 287 | char Buffer[256]; |
| 288 | |
| 289 | // If we haven't emitted a token on this line yet, PrevTok isn't useful to |
| 290 | // look at and no concatenation could happen anyway. |
| 291 | if (!EmodeEmittedTokensOnThisLine) |
| 292 | return false; |
| 293 | |
| 294 | // Basic algorithm: we look at the first character of the second token, and |
| 295 | // determine whether it, if appended to the first token, would form (or would |
| 296 | // contribute) to a larger token if concatenated. |
| 297 | char FirstChar; |
| 298 | if (IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 299 | // Avoid spelling identifiers, the most common form of token. |
| 300 | FirstChar = II->getName()[0]; |
| 301 | } else if (Tok.getLength() < 256) { |
| 302 | const char *TokPtr = Buffer; |
| 303 | unsigned Len = PP.getSpelling(Tok, TokPtr); |
| 304 | FirstChar = TokPtr[0]; |
| 305 | } else { |
| 306 | FirstChar = PP.getSpelling(Tok)[0]; |
| 307 | } |
| 308 | |
| 309 | tok::TokenKind PrevKind = PrevTok.getKind(); |
| 310 | if (PrevTok.getIdentifierInfo()) // Language keyword or named operator. |
| 311 | PrevKind = tok::identifier; |
| 312 | |
| 313 | switch (PrevKind) { |
| 314 | default: return false; |
| 315 | case tok::identifier: // id+id or id+number or id+L"foo". |
| 316 | return isalnum(FirstChar) || FirstChar == '_'; |
| 317 | case tok::numeric_constant: |
| 318 | return isalnum(FirstChar) || Tok.getKind() == tok::numeric_constant || |
| 319 | FirstChar == '+' || FirstChar == '-' || FirstChar == '.'; |
| 320 | case tok::period: // ..., .*, .1234 |
| 321 | return FirstChar == '.' || FirstChar == '*' || isdigit(FirstChar); |
| 322 | case tok::amp: // &&, &= |
| 323 | return FirstChar == '&' || FirstChar == '='; |
| 324 | case tok::plus: // ++, += |
| 325 | return FirstChar == '+' || FirstChar == '='; |
| 326 | case tok::minus: // --, ->, -=, ->* |
| 327 | return FirstChar == '-' || FirstChar == '>' || FirstChar == '='; |
| 328 | case tok::slash: // /=, /*, // |
| 329 | return FirstChar == '=' || FirstChar == '*' || FirstChar == '/'; |
| 330 | case tok::less: // <<, <<=, <=, <?=, <?, <:, <% |
| 331 | return FirstChar == '<' || FirstChar == '?' || FirstChar == '=' || |
| 332 | FirstChar == ':' || FirstChar == '%'; |
| 333 | case tok::greater: // >>, >=, >>=, >?=, >?, ->* |
| 334 | return FirstChar == '>' || FirstChar == '?' || FirstChar == '=' || |
| 335 | FirstChar == '*'; |
| 336 | case tok::pipe: // ||, |= |
| 337 | return FirstChar == '|' || FirstChar == '='; |
| 338 | case tok::percent: // %=, %>, %: |
| 339 | return FirstChar == '=' || FirstChar == '>' || FirstChar == ':'; |
| 340 | case tok::colon: // ::, :> |
| 341 | return FirstChar == ':' || FirstChar == '>'; |
| 342 | case tok::hash: // ##, #@, %:%: |
| 343 | return FirstChar == '#' || FirstChar == '@' || FirstChar == '%'; |
| 344 | case tok::question: // <?=, >?=, ??x -> trigraphs. |
| 345 | // Have to check for <?= in case <? is disabled. |
| 346 | return FirstChar == '?' || FirstChar == '='; |
| 347 | case tok::arrow: // ->* |
| 348 | return FirstChar == '*'; |
| 349 | |
| 350 | case tok::star: // *= |
| 351 | case tok::exclaim: // != |
| 352 | case tok::lessless: // <<= |
| 353 | case tok::greaterequal: // >>= |
| 354 | case tok::caret: // ^= |
| 355 | case tok::equal: // == |
| 356 | case tok::lessquestion: // <?= |
| 357 | case tok::greaterquestion: // >?= |
| 358 | // Cases that concatenate only if the next char is =. |
| 359 | return FirstChar == '='; |
| 360 | } |
| 361 | } |
| 362 | |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 363 | /// DoPrintPreprocessedInput - This implements -E mode. |
Chris Lattner | 728b4dc | 2006-07-04 21:28:37 +0000 | [diff] [blame] | 364 | /// |
Chris Lattner | 457fc15 | 2006-07-29 06:30:25 +0000 | [diff] [blame^] | 365 | void clang::DoPrintPreprocessedInput(Preprocessor &PP, LangOptions &Options) { |
| 366 | if (EnableCommentOutput) // -C specified? |
| 367 | Options.KeepComments = 1; |
| 368 | if (EnableMacroCommentOutput) // -CC specified? |
| 369 | Options.KeepComments = Options.KeepMacroComments = 1; |
| 370 | |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 371 | InitOutputBuffer(); |
| 372 | |
Chris Lattner | 331ad77 | 2006-07-28 06:56:01 +0000 | [diff] [blame] | 373 | LexerToken Tok, PrevTok; |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 374 | char Buffer[256]; |
| 375 | EModeCurLine = 0; |
| 376 | EModeCurFilename = "\"<uninit>\""; |
| 377 | PP.setFileChangeHandler(HandleFileChange); |
| 378 | PP.setIdentHandler(HandleIdent); |
| 379 | EModePP = &PP; |
| 380 | EmodeEmittedTokensOnThisLine = false; |
| 381 | |
| 382 | PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma")); |
| 383 | PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC")); |
| 384 | do { |
Chris Lattner | 331ad77 | 2006-07-28 06:56:01 +0000 | [diff] [blame] | 385 | PrevTok = Tok; |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 386 | PP.Lex(Tok); |
| 387 | |
Chris Lattner | 67c3848 | 2006-07-04 23:24:26 +0000 | [diff] [blame] | 388 | // If this token is at the start of a line, emit newlines if needed. |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 389 | if (Tok.isAtStartOfLine()) { |
| 390 | HandleFirstTokOnLine(Tok, PP); |
Chris Lattner | 331ad77 | 2006-07-28 06:56:01 +0000 | [diff] [blame] | 391 | } else if (Tok.hasLeadingSpace() || |
| 392 | // Don't print "-" next to "-", it would form "--". |
| 393 | AvoidConcat(PrevTok, Tok, PP)) { |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 394 | OutputChar(' '); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | if (Tok.getLength() < 256) { |
Chris Lattner | ef9eae1 | 2006-07-04 22:33:12 +0000 | [diff] [blame] | 398 | const char *TokPtr = Buffer; |
| 399 | unsigned Len = PP.getSpelling(Tok, TokPtr); |
| 400 | OutputString(TokPtr, Len); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 401 | } else { |
Chris Lattner | deb3701 | 2006-07-04 19:24:06 +0000 | [diff] [blame] | 402 | std::string S = PP.getSpelling(Tok); |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 403 | OutputString(&S[0], S.size()); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 404 | } |
| 405 | EmodeEmittedTokensOnThisLine = true; |
| 406 | } while (Tok.getKind() != tok::eof); |
Chris Lattner | f46be6c | 2006-07-04 22:19:33 +0000 | [diff] [blame] | 407 | OutputChar('\n'); |
| 408 | |
| 409 | CleanupOutputBuffer(); |
Chris Lattner | 09e3cdf | 2006-07-04 19:04:05 +0000 | [diff] [blame] | 410 | } |
| 411 | |