Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Preprocessor interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | // |
| 14 | // Options to support: |
| 15 | // -H - Print the name of each header file used. |
| 16 | // -d[MDNI] - Dump various things. |
| 17 | // -fworking-directory - #line's with preprocessor's working dir. |
| 18 | // -fpreprocessed |
| 19 | // -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD |
| 20 | // -W* |
| 21 | // -w |
| 22 | // |
| 23 | // Messages to emit: |
| 24 | // "Multiple include guards may be useful for:\n" |
| 25 | // |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | #include "clang/Lex/Preprocessor.h" |
| 29 | #include "clang/Lex/HeaderSearch.h" |
| 30 | #include "clang/Lex/MacroInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Pragma.h" |
| 32 | #include "clang/Lex/ScratchBuffer.h" |
| 33 | #include "clang/Basic/Diagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | #include "clang/Basic/SourceManager.h" |
| 35 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 2db78dd | 2008-10-05 20:40:30 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/APFloat.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MemoryBuffer.h" |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Streams.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | using namespace clang; |
| 41 | |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
Ted Kremenek | ec6c574 | 2008-04-17 21:23:07 +0000 | [diff] [blame] | 44 | PreprocessorFactory::~PreprocessorFactory() {} |
| 45 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 46 | Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts, |
| 47 | TargetInfo &target, SourceManager &SM, |
| 48 | HeaderSearch &Headers) |
| 49 | : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()), |
| 50 | SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts), |
Chris Lattner | 6cfe759 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 51 | CurLexer(0), CurDirLookup(0), CurTokenLexer(0), Callbacks(0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | ScratchBuf = new ScratchBuffer(SourceMgr); |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 53 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 54 | // Clear stats. |
| 55 | NumDirectives = NumDefined = NumUndefined = NumPragma = 0; |
| 56 | NumIf = NumElse = NumEndif = 0; |
| 57 | NumEnteredSourceFiles = 0; |
| 58 | NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0; |
| 59 | NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0; |
| 60 | MaxIncludeStackDepth = 0; |
| 61 | NumSkipped = 0; |
| 62 | |
| 63 | // Default to discarding comments. |
| 64 | KeepComments = false; |
| 65 | KeepMacroComments = false; |
| 66 | |
| 67 | // Macro expansion is enabled. |
| 68 | DisableMacroExpansion = false; |
| 69 | InMacroArgs = false; |
Chris Lattner | 6cfe759 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 70 | NumCachedTokenLexers = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 71 | |
Argyrios Kyrtzidis | 03db1b3 | 2008-08-10 13:15:22 +0000 | [diff] [blame] | 72 | CacheTokens = false; |
| 73 | CachedLexPos = 0; |
| 74 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 75 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 76 | // This gets unpoisoned where it is allowed. |
| 77 | (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned(); |
| 78 | |
| 79 | // Initialize the pragma handlers. |
| 80 | PragmaHandlers = new PragmaNamespace(0); |
| 81 | RegisterBuiltinPragmas(); |
| 82 | |
| 83 | // Initialize builtin macros like __LINE__ and friends. |
| 84 | RegisterBuiltinMacros(); |
| 85 | } |
| 86 | |
| 87 | Preprocessor::~Preprocessor() { |
Argyrios Kyrtzidis | 2174a4f | 2008-08-23 12:12:06 +0000 | [diff] [blame] | 88 | assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!"); |
| 89 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 90 | while (!IncludeMacroStack.empty()) { |
| 91 | delete IncludeMacroStack.back().TheLexer; |
Chris Lattner | 6cfe759 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 92 | delete IncludeMacroStack.back().TheTokenLexer; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 93 | IncludeMacroStack.pop_back(); |
| 94 | } |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 95 | |
| 96 | // Free any macro definitions. |
| 97 | for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I = |
| 98 | Macros.begin(), E = Macros.end(); I != E; ++I) { |
| 99 | // Free the macro definition. |
| 100 | delete I->second; |
| 101 | I->second = 0; |
| 102 | I->first->setHasMacroDefinition(false); |
| 103 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 105 | // Free any cached macro expanders. |
Chris Lattner | 6cfe759 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 106 | for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i) |
| 107 | delete TokenLexerCache[i]; |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 108 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 109 | // Release pragma information. |
| 110 | delete PragmaHandlers; |
| 111 | |
| 112 | // Delete the scratch buffer info. |
| 113 | delete ScratchBuf; |
Chris Lattner | eb50ed8 | 2008-03-14 06:07:05 +0000 | [diff] [blame] | 114 | |
| 115 | delete Callbacks; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 118 | /// Diag - Forwarding function for diagnostics. This emits a diagnostic at |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 119 | /// the specified Token's location, translating the token's start |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | /// position in the current buffer into a SourcePosition object for rendering. |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame^] | 121 | DiagnosticInfo Preprocessor::Diag(SourceLocation Loc, unsigned DiagID) { |
| 122 | return Diags.Report(getFullLoc(Loc), DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame^] | 125 | DiagnosticInfo Preprocessor::Diag(const Token &Tok, unsigned DiagID) { |
| 126 | return Diags.Report(getFullLoc(Tok.getLocation()), DiagID); |
| 127 | } |
| 128 | |
| 129 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID, |
| 131 | const std::string &Msg) { |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 132 | Diags.Report(getFullLoc(Loc), DiagID) << Msg; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 135 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID, |
| 136 | const std::string &Msg, |
| 137 | const SourceRange &R1, const SourceRange &R2) { |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 138 | Diags.Report(getFullLoc(Loc), DiagID) << Msg << R1 << R2; |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
| 142 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID, |
| 143 | const SourceRange &R) { |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 144 | Diags.Report(getFullLoc(Loc), DiagID) << R; |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID, |
| 148 | const SourceRange &R1, const SourceRange &R2) { |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 149 | Diags.Report(getFullLoc(Loc), DiagID) << R1 << R2; |
Chris Lattner | 8ed3044 | 2008-05-05 06:45:50 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 153 | void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const { |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 154 | llvm::cerr << tok::getTokenName(Tok.getKind()) << " '" |
| 155 | << getSpelling(Tok) << "'"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 156 | |
| 157 | if (!DumpFlags) return; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 158 | |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 159 | llvm::cerr << "\t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | if (Tok.isAtStartOfLine()) |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 161 | llvm::cerr << " [StartOfLine]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 162 | if (Tok.hasLeadingSpace()) |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 163 | llvm::cerr << " [LeadingSpace]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 164 | if (Tok.isExpandDisabled()) |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 165 | llvm::cerr << " [ExpandDisabled]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | if (Tok.needsCleaning()) { |
| 167 | const char *Start = SourceMgr.getCharacterData(Tok.getLocation()); |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 168 | llvm::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength()) |
| 169 | << "']"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | } |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 171 | |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 172 | llvm::cerr << "\tLoc=<"; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 173 | DumpLocation(Tok.getLocation()); |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 174 | llvm::cerr << ">"; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void Preprocessor::DumpLocation(SourceLocation Loc) const { |
| 178 | SourceLocation LogLoc = SourceMgr.getLogicalLoc(Loc); |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 179 | llvm::cerr << SourceMgr.getSourceName(LogLoc) << ':' |
| 180 | << SourceMgr.getLineNumber(LogLoc) << ':' |
Ted Kremenek | 109949a | 2008-07-19 19:10:04 +0000 | [diff] [blame] | 181 | << SourceMgr.getColumnNumber(LogLoc); |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 182 | |
| 183 | SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(Loc); |
| 184 | if (PhysLoc != LogLoc) { |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 185 | llvm::cerr << " <PhysLoc="; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 186 | DumpLocation(PhysLoc); |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 187 | llvm::cerr << ">"; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 188 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void Preprocessor::DumpMacro(const MacroInfo &MI) const { |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 192 | llvm::cerr << "MACRO: "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 193 | for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) { |
| 194 | DumpToken(MI.getReplacementToken(i)); |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 195 | llvm::cerr << " "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 196 | } |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 197 | llvm::cerr << "\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void Preprocessor::PrintStats() { |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 201 | llvm::cerr << "\n*** Preprocessor Stats:\n"; |
| 202 | llvm::cerr << NumDirectives << " directives found:\n"; |
| 203 | llvm::cerr << " " << NumDefined << " #define.\n"; |
| 204 | llvm::cerr << " " << NumUndefined << " #undef.\n"; |
| 205 | llvm::cerr << " #include/#include_next/#import:\n"; |
| 206 | llvm::cerr << " " << NumEnteredSourceFiles << " source files entered.\n"; |
| 207 | llvm::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n"; |
| 208 | llvm::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n"; |
| 209 | llvm::cerr << " " << NumElse << " #else/#elif.\n"; |
| 210 | llvm::cerr << " " << NumEndif << " #endif.\n"; |
| 211 | llvm::cerr << " " << NumPragma << " #pragma.\n"; |
| 212 | llvm::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 213 | |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 214 | llvm::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/" |
| 215 | << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, " |
| 216 | << NumFastMacroExpanded << " on the fast path.\n"; |
| 217 | llvm::cerr << (NumFastTokenPaste+NumTokenPaste) |
| 218 | << " token paste (##) operations performed, " |
| 219 | << NumFastTokenPaste << " on the fast path.\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | //===----------------------------------------------------------------------===// |
| 223 | // Token Spelling |
| 224 | //===----------------------------------------------------------------------===// |
| 225 | |
| 226 | |
| 227 | /// getSpelling() - Return the 'spelling' of this token. The spelling of a |
| 228 | /// token are the characters used to represent the token in the source file |
| 229 | /// after trigraph expansion and escaped-newline folding. In particular, this |
| 230 | /// wants to get the true, uncanonicalized, spelling of things like digraphs |
| 231 | /// UCNs, etc. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 232 | std::string Preprocessor::getSpelling(const Token &Tok) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 233 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
| 234 | |
| 235 | // If this token contains nothing interesting, return it directly. |
| 236 | const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
| 237 | if (!Tok.needsCleaning()) |
| 238 | return std::string(TokStart, TokStart+Tok.getLength()); |
| 239 | |
| 240 | std::string Result; |
| 241 | Result.reserve(Tok.getLength()); |
| 242 | |
| 243 | // Otherwise, hard case, relex the characters into the string. |
| 244 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 245 | Ptr != End; ) { |
| 246 | unsigned CharSize; |
| 247 | Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features)); |
| 248 | Ptr += CharSize; |
| 249 | } |
| 250 | assert(Result.size() != unsigned(Tok.getLength()) && |
| 251 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 252 | return Result; |
| 253 | } |
| 254 | |
| 255 | /// getSpelling - This method is used to get the spelling of a token into a |
| 256 | /// preallocated buffer, instead of as an std::string. The caller is required |
| 257 | /// to allocate enough space for the token, which is guaranteed to be at least |
| 258 | /// Tok.getLength() bytes long. The actual length of the token is returned. |
| 259 | /// |
| 260 | /// Note that this method may do two possible things: it may either fill in |
| 261 | /// the buffer specified with characters, or it may *change the input pointer* |
| 262 | /// to point to a constant buffer with the data already in it (avoiding a |
| 263 | /// copy). The caller is not allowed to modify the returned buffer pointer |
| 264 | /// if an internal buffer is returned. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 265 | unsigned Preprocessor::getSpelling(const Token &Tok, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 266 | const char *&Buffer) const { |
| 267 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
| 268 | |
| 269 | // If this token is an identifier, just return the string from the identifier |
| 270 | // table, which is very quick. |
| 271 | if (const IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 272 | Buffer = II->getName(); |
Chris Lattner | 0f67032 | 2007-07-22 22:50:09 +0000 | [diff] [blame] | 273 | |
| 274 | // Return the length of the token. If the token needed cleaning, don't |
| 275 | // include the size of the newlines or trigraphs in it. |
| 276 | if (!Tok.needsCleaning()) |
| 277 | return Tok.getLength(); |
| 278 | else |
| 279 | return strlen(Buffer); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | // Otherwise, compute the start of the token in the input lexer buffer. |
| 283 | const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
| 284 | |
| 285 | // If this token contains nothing interesting, return it directly. |
| 286 | if (!Tok.needsCleaning()) { |
| 287 | Buffer = TokStart; |
| 288 | return Tok.getLength(); |
| 289 | } |
| 290 | // Otherwise, hard case, relex the characters into the string. |
| 291 | char *OutBuf = const_cast<char*>(Buffer); |
| 292 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 293 | Ptr != End; ) { |
| 294 | unsigned CharSize; |
| 295 | *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features); |
| 296 | Ptr += CharSize; |
| 297 | } |
| 298 | assert(unsigned(OutBuf-Buffer) != Tok.getLength() && |
| 299 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 300 | |
| 301 | return OutBuf-Buffer; |
| 302 | } |
| 303 | |
| 304 | |
| 305 | /// CreateString - Plop the specified string into a scratch buffer and return a |
| 306 | /// location for it. If specified, the source location provides a source |
| 307 | /// location for the token. |
| 308 | SourceLocation Preprocessor:: |
| 309 | CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) { |
| 310 | if (SLoc.isValid()) |
| 311 | return ScratchBuf->getToken(Buf, Len, SLoc); |
| 312 | return ScratchBuf->getToken(Buf, Len); |
| 313 | } |
| 314 | |
| 315 | |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 316 | /// AdvanceToTokenCharacter - Given a location that specifies the start of a |
| 317 | /// token, return a new location that specifies a character within the token. |
| 318 | SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart, |
| 319 | unsigned CharNo) { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 320 | // If they request the first char of the token, we're trivially done. If this |
| 321 | // is a macro expansion, it doesn't make sense to point to a character within |
| 322 | // the instantiation point (the name). We could point to the source |
| 323 | // character, but without also pointing to instantiation info, this is |
| 324 | // confusing. |
| 325 | if (CharNo == 0 || TokStart.isMacroID()) return TokStart; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 326 | |
| 327 | // Figure out how many physical characters away the specified logical |
| 328 | // character is. This needs to take into consideration newlines and |
| 329 | // trigraphs. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 330 | const char *TokPtr = SourceMgr.getCharacterData(TokStart); |
| 331 | unsigned PhysOffset = 0; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 332 | |
| 333 | // The usual case is that tokens don't contain anything interesting. Skip |
| 334 | // over the uninteresting characters. If a token only consists of simple |
| 335 | // chars, this method is extremely fast. |
| 336 | while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr)) |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 337 | ++TokPtr, --CharNo, ++PhysOffset; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 338 | |
| 339 | // If we have a character that may be a trigraph or escaped newline, create a |
| 340 | // lexer to parse it correctly. |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 341 | if (CharNo != 0) { |
| 342 | // Create a lexer starting at this token position. |
Chris Lattner | 25bdb51 | 2007-07-20 16:52:03 +0000 | [diff] [blame] | 343 | Lexer TheLexer(TokStart, *this, TokPtr); |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 344 | Token Tok; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 345 | // Skip over characters the remaining characters. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 346 | const char *TokStartPtr = TokPtr; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 347 | for (; CharNo; --CharNo) |
| 348 | TheLexer.getAndAdvanceChar(TokPtr, Tok); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 349 | |
| 350 | PhysOffset += TokPtr-TokStartPtr; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 351 | } |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 352 | |
| 353 | return TokStart.getFileLocWithOffset(PhysOffset); |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 357 | //===----------------------------------------------------------------------===// |
| 358 | // Preprocessor Initialization Methods |
| 359 | //===----------------------------------------------------------------------===// |
| 360 | |
| 361 | // Append a #define line to Buf for Macro. Macro should be of the form XXX, |
| 362 | // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit |
| 363 | // "#define XXX Y z W". To get a #define with no value, use "XXX=". |
| 364 | static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro, |
| 365 | const char *Command = "#define ") { |
| 366 | Buf.insert(Buf.end(), Command, Command+strlen(Command)); |
| 367 | if (const char *Equal = strchr(Macro, '=')) { |
| 368 | // Turn the = into ' '. |
| 369 | Buf.insert(Buf.end(), Macro, Equal); |
| 370 | Buf.push_back(' '); |
| 371 | Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal)); |
| 372 | } else { |
| 373 | // Push "macroname 1". |
| 374 | Buf.insert(Buf.end(), Macro, Macro+strlen(Macro)); |
| 375 | Buf.push_back(' '); |
| 376 | Buf.push_back('1'); |
| 377 | } |
| 378 | Buf.push_back('\n'); |
| 379 | } |
| 380 | |
Chris Lattner | 2db78dd | 2008-10-05 20:40:30 +0000 | [diff] [blame] | 381 | /// PickFP - This is used to pick a value based on the FP semantics of the |
| 382 | /// specified FP model. |
| 383 | template <typename T> |
| 384 | static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal, |
| 385 | T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal) { |
| 386 | if (Sem == &llvm::APFloat::IEEEsingle) |
| 387 | return IEEESingleVal; |
| 388 | if (Sem == &llvm::APFloat::IEEEdouble) |
| 389 | return IEEEDoubleVal; |
| 390 | if (Sem == &llvm::APFloat::x87DoubleExtended) |
| 391 | return X87DoubleExtendedVal; |
| 392 | assert(Sem == &llvm::APFloat::PPCDoubleDouble); |
| 393 | return PPCDoubleDoubleVal; |
| 394 | } |
| 395 | |
| 396 | static void DefineFloatMacros(std::vector<char> &Buf, const char *Prefix, |
| 397 | const llvm::fltSemantics *Sem) { |
Chris Lattner | e9863ca | 2008-10-05 21:40:58 +0000 | [diff] [blame] | 398 | const char *DenormMin, *Epsilon, *Max, *Min; |
| 399 | DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324", |
| 400 | "3.64519953188247460253e-4951L", |
| 401 | "4.94065645841246544176568792868221e-324L"); |
| 402 | int Digits = PickFP(Sem, 6, 15, 18, 31); |
| 403 | Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16", |
| 404 | "1.08420217248550443401e-19L", |
| 405 | "4.94065645841246544176568792868221e-324L"); |
| 406 | int HasInifinity = 1, HasQuietNaN = 1; |
| 407 | int MantissaDigits = PickFP(Sem, 24, 53, 64, 106); |
| 408 | int Min10Exp = PickFP(Sem, -37, -307, -4931, -291); |
| 409 | int Max10Exp = PickFP(Sem, 38, 308, 4932, 308); |
| 410 | int MinExp = PickFP(Sem, -125, -1021, -16381, -968); |
| 411 | int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024); |
| 412 | Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308", |
| 413 | "3.36210314311209350626e-4932L", |
| 414 | "2.00416836000897277799610805135016e-292L"); |
| 415 | Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308", |
| 416 | "1.18973149535723176502e+4932L", |
| 417 | "1.79769313486231580793728971405301e+308L"); |
Chris Lattner | 2db78dd | 2008-10-05 20:40:30 +0000 | [diff] [blame] | 418 | |
| 419 | char MacroBuf[60]; |
| 420 | sprintf(MacroBuf, "__%s_DENORM_MIN__=%s", Prefix, DenormMin); |
| 421 | DefineBuiltinMacro(Buf, MacroBuf); |
Chris Lattner | e9863ca | 2008-10-05 21:40:58 +0000 | [diff] [blame] | 422 | sprintf(MacroBuf, "__%s_DIG__=%d", Prefix, Digits); |
| 423 | DefineBuiltinMacro(Buf, MacroBuf); |
| 424 | sprintf(MacroBuf, "__%s_EPSILON__=%s", Prefix, Epsilon); |
| 425 | DefineBuiltinMacro(Buf, MacroBuf); |
| 426 | sprintf(MacroBuf, "__%s_HAS_INFINITY__=%d", Prefix, HasInifinity); |
| 427 | DefineBuiltinMacro(Buf, MacroBuf); |
| 428 | sprintf(MacroBuf, "__%s_HAS_QUIET_NAN__=%d", Prefix, HasQuietNaN); |
| 429 | DefineBuiltinMacro(Buf, MacroBuf); |
| 430 | sprintf(MacroBuf, "__%s_MANT_DIG__=%d", Prefix, MantissaDigits); |
| 431 | DefineBuiltinMacro(Buf, MacroBuf); |
| 432 | sprintf(MacroBuf, "__%s_MAX_10_EXP__=%d", Prefix, Max10Exp); |
| 433 | DefineBuiltinMacro(Buf, MacroBuf); |
| 434 | sprintf(MacroBuf, "__%s_MAX_EXP__=%d", Prefix, MaxExp); |
| 435 | DefineBuiltinMacro(Buf, MacroBuf); |
| 436 | sprintf(MacroBuf, "__%s_MAX__=%s", Prefix, Max); |
| 437 | DefineBuiltinMacro(Buf, MacroBuf); |
| 438 | sprintf(MacroBuf, "__%s_MIN_10_EXP__=(%d)", Prefix, Min10Exp); |
| 439 | DefineBuiltinMacro(Buf, MacroBuf); |
| 440 | sprintf(MacroBuf, "__%s_MIN_EXP__=(%d)", Prefix, MinExp); |
| 441 | DefineBuiltinMacro(Buf, MacroBuf); |
| 442 | sprintf(MacroBuf, "__%s_MIN__=%s", Prefix, Min); |
| 443 | DefineBuiltinMacro(Buf, MacroBuf); |
Chris Lattner | 2db78dd | 2008-10-05 20:40:30 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 446 | |
| 447 | static void InitializePredefinedMacros(Preprocessor &PP, |
| 448 | std::vector<char> &Buf) { |
Chris Lattner | 62213d9 | 2008-10-05 19:32:22 +0000 | [diff] [blame] | 449 | // Compiler version introspection macros. |
| 450 | DefineBuiltinMacro(Buf, "__llvm__=1"); // LLVM Backend |
| 451 | DefineBuiltinMacro(Buf, "__clang__=1"); // Clang Frontend |
| 452 | |
| 453 | // Currently claim to be compatible with GCC 4.2.1-5621. |
| 454 | DefineBuiltinMacro(Buf, "__APPLE_CC__=5621"); |
| 455 | DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2"); |
| 456 | DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1"); |
| 457 | DefineBuiltinMacro(Buf, "__GNUC__=4"); |
| 458 | DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002"); |
| 459 | DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 (Apple Computer, Inc. " |
| 460 | "build 5621) (dot 3)\""); |
| 461 | |
| 462 | |
| 463 | // Initialize language-specific preprocessor defines. |
| 464 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 465 | // FIXME: Implement magic like cpp_init_builtins for things like __STDC__ |
| 466 | // and __DATE__ etc. |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 467 | // These should all be defined in the preprocessor according to the |
| 468 | // current language configuration. |
| 469 | DefineBuiltinMacro(Buf, "__STDC__=1"); |
| 470 | //DefineBuiltinMacro(Buf, "__ASSEMBLER__=1"); |
| 471 | if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus) |
| 472 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L"); |
| 473 | else if (0) // STDC94 ? |
| 474 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L"); |
| 475 | |
| 476 | DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1"); |
Daniel Dunbar | fba5cb1 | 2008-08-12 00:21:46 +0000 | [diff] [blame] | 477 | if (PP.getLangOptions().ObjC1) { |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 478 | DefineBuiltinMacro(Buf, "__OBJC__=1"); |
Daniel Dunbar | fba5cb1 | 2008-08-12 00:21:46 +0000 | [diff] [blame] | 479 | |
| 480 | if (PP.getLangOptions().getGCMode() == LangOptions::NonGC) { |
| 481 | DefineBuiltinMacro(Buf, "__weak="); |
| 482 | DefineBuiltinMacro(Buf, "__strong="); |
| 483 | } else { |
| 484 | DefineBuiltinMacro(Buf, "__weak=__attribute__((objc_gc(weak)))"); |
| 485 | DefineBuiltinMacro(Buf, "__strong=__attribute__((objc_gc(strong)))"); |
| 486 | DefineBuiltinMacro(Buf, "__OBJC_GC__=1"); |
| 487 | } |
| 488 | |
| 489 | if (PP.getLangOptions().NeXTRuntime) |
| 490 | DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1"); |
Daniel Dunbar | fba5cb1 | 2008-08-12 00:21:46 +0000 | [diff] [blame] | 491 | } |
Chris Lattner | 9b53316 | 2008-10-05 19:44:25 +0000 | [diff] [blame] | 492 | |
Chris Lattner | eb52b44 | 2008-10-06 07:43:09 +0000 | [diff] [blame] | 493 | // darwin_constant_cfstrings controls this. This is also dependent |
| 494 | // on other things like the runtime I believe. This is set even for C code. |
| 495 | DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1"); |
| 496 | |
Steve Naroff | 73b17cd | 2008-05-15 21:12:10 +0000 | [diff] [blame] | 497 | if (PP.getLangOptions().ObjC2) |
| 498 | DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES"); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 499 | |
Chris Lattner | 048dd94 | 2008-09-30 00:48:48 +0000 | [diff] [blame] | 500 | if (PP.getLangOptions().PascalStrings) |
| 501 | DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__"); |
| 502 | |
Chris Lattner | 62213d9 | 2008-10-05 19:32:22 +0000 | [diff] [blame] | 503 | if (PP.getLangOptions().Blocks) { |
| 504 | DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))"); |
| 505 | DefineBuiltinMacro(Buf, "__BLOCKS__=1"); |
Chris Lattner | 2b43ad9 | 2008-10-05 19:32:52 +0000 | [diff] [blame] | 506 | } |
Chris Lattner | 62213d9 | 2008-10-05 19:32:22 +0000 | [diff] [blame] | 507 | |
| 508 | if (PP.getLangOptions().CPlusPlus) { |
| 509 | DefineBuiltinMacro(Buf, "__DEPRECATED=1"); |
| 510 | DefineBuiltinMacro(Buf, "__EXCEPTIONS=1"); |
| 511 | DefineBuiltinMacro(Buf, "__GNUG__=4"); |
| 512 | DefineBuiltinMacro(Buf, "__GXX_WEAK__=1"); |
| 513 | DefineBuiltinMacro(Buf, "__cplusplus=1"); |
| 514 | DefineBuiltinMacro(Buf, "__private_extern__=extern"); |
| 515 | } |
| 516 | |
| 517 | // Filter out some microsoft extensions when trying to parse in ms-compat |
| 518 | // mode. |
| 519 | if (PP.getLangOptions().Microsoft) { |
| 520 | DefineBuiltinMacro(Buf, "__stdcall="); |
| 521 | DefineBuiltinMacro(Buf, "__cdecl="); |
| 522 | DefineBuiltinMacro(Buf, "_cdecl="); |
| 523 | DefineBuiltinMacro(Buf, "__ptr64="); |
| 524 | DefineBuiltinMacro(Buf, "__w64="); |
| 525 | DefineBuiltinMacro(Buf, "__forceinline="); |
| 526 | DefineBuiltinMacro(Buf, "__int8=char"); |
| 527 | DefineBuiltinMacro(Buf, "__int16=short"); |
| 528 | DefineBuiltinMacro(Buf, "__int32=int"); |
| 529 | DefineBuiltinMacro(Buf, "__int64=long long"); |
| 530 | DefineBuiltinMacro(Buf, "__declspec(X)="); |
| 531 | } |
| 532 | |
| 533 | |
| 534 | // Initialize target-specific preprocessor defines. |
Chris Lattner | 9b53316 | 2008-10-05 19:44:25 +0000 | [diff] [blame] | 535 | const TargetInfo &TI = PP.getTargetInfo(); |
| 536 | |
| 537 | // Define type sizing macros based on the target properties. |
| 538 | assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far"); |
| 539 | DefineBuiltinMacro(Buf, "__CHAR_BIT__=8"); |
| 540 | DefineBuiltinMacro(Buf, "__SCHAR_MAX__=127"); |
Chris Lattner | 2db78dd | 2008-10-05 20:40:30 +0000 | [diff] [blame] | 541 | |
| 542 | assert(TI.getWCharWidth() == 32 && "Only support 32-bit wchar so far"); |
| 543 | DefineBuiltinMacro(Buf, "__WCHAR_MAX__=2147483647"); |
| 544 | DefineBuiltinMacro(Buf, "__WCHAR_TYPE__=int"); |
| 545 | DefineBuiltinMacro(Buf, "__WINT_TYPE__=int"); |
Chris Lattner | 9b53316 | 2008-10-05 19:44:25 +0000 | [diff] [blame] | 546 | |
| 547 | assert(TI.getShortWidth() == 16 && "Only support 16-bit short so far"); |
Chris Lattner | 9b53316 | 2008-10-05 19:44:25 +0000 | [diff] [blame] | 548 | DefineBuiltinMacro(Buf, "__SHRT_MAX__=32767"); |
| 549 | |
Chris Lattner | 0e5d4ef | 2008-10-05 20:06:37 +0000 | [diff] [blame] | 550 | if (TI.getIntWidth() == 32) |
| 551 | DefineBuiltinMacro(Buf, "__INT_MAX__=2147483647"); |
| 552 | else if (TI.getIntWidth() == 16) |
| 553 | DefineBuiltinMacro(Buf, "__INT_MAX__=32767"); |
| 554 | else |
| 555 | assert(0 && "Unknown integer size"); |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 556 | |
| 557 | if (TI.getLongLongWidth() == 64) |
| 558 | DefineBuiltinMacro(Buf, "__LONG_LONG_MAX__=9223372036854775807LL"); |
| 559 | else if (TI.getLongLongWidth() == 32) |
| 560 | DefineBuiltinMacro(Buf, "__LONG_LONG_MAX__=2147483647L"); |
Chris Lattner | 9b53316 | 2008-10-05 19:44:25 +0000 | [diff] [blame] | 561 | |
Chris Lattner | 0e5d4ef | 2008-10-05 20:06:37 +0000 | [diff] [blame] | 562 | if (TI.getLongWidth() == 32) |
| 563 | DefineBuiltinMacro(Buf, "__LONG_MAX__=2147483647L"); |
| 564 | else if (TI.getLongWidth() == 64) |
| 565 | DefineBuiltinMacro(Buf, "__LONG_MAX__=9223372036854775807L"); |
| 566 | else if (TI.getLongWidth() == 16) |
| 567 | DefineBuiltinMacro(Buf, "__LONG_MAX__=32767L"); |
| 568 | else |
| 569 | assert(0 && "Unknown long size"); |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 570 | char MacroBuf[60]; |
| 571 | sprintf(MacroBuf, "__INTMAX_MAX__=%lld", |
| 572 | (TI.getIntMaxType() == TargetInfo::UnsignedLongLong? |
Sanjiv Gupta | 73608a8 | 2008-10-31 10:24:31 +0000 | [diff] [blame] | 573 | (1LL << (TI.getLongLongWidth() - 1)) : |
| 574 | ((1LL << (TI.getLongLongWidth() - 2)) - 1))); |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 575 | DefineBuiltinMacro(Buf, MacroBuf); |
Chris Lattner | 0e5d4ef | 2008-10-05 20:06:37 +0000 | [diff] [blame] | 576 | |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 577 | if (TI.getIntMaxType() == TargetInfo::UnsignedLongLong) |
| 578 | DefineBuiltinMacro(Buf, "__INTMAX_TYPE__=unsigned long long int"); |
| 579 | else if (TI.getIntMaxType() == TargetInfo::SignedLongLong) |
Chris Lattner | 0e5d4ef | 2008-10-05 20:06:37 +0000 | [diff] [blame] | 580 | DefineBuiltinMacro(Buf, "__INTMAX_TYPE__=long long int"); |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 581 | else if (TI.getIntMaxType() == TargetInfo::UnsignedLong) |
| 582 | DefineBuiltinMacro(Buf, "__INTMAX_TYPE__=unsigned long int"); |
| 583 | else if (TI.getIntMaxType() == TargetInfo::SignedLong) |
| 584 | DefineBuiltinMacro(Buf, "__INTMAX_TYPE__=long int"); |
| 585 | else if (TI.getIntMaxType() == TargetInfo::UnsignedInt) |
| 586 | DefineBuiltinMacro(Buf, "__INTMAX_TYPE__=unsigned int"); |
| 587 | else |
| 588 | DefineBuiltinMacro(Buf, "__INTMAX_TYPE__=int"); |
Chris Lattner | 0e5d4ef | 2008-10-05 20:06:37 +0000 | [diff] [blame] | 589 | |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 590 | if (TI.getUIntMaxType() == TargetInfo::UnsignedLongLong) |
| 591 | DefineBuiltinMacro(Buf, "__UINTMAX_TYPE__=unsigned long long int"); |
| 592 | else if (TI.getUIntMaxType() == TargetInfo::SignedLongLong) |
| 593 | DefineBuiltinMacro(Buf, "__UINTMAX_TYPE__=long long int"); |
| 594 | else if (TI.getUIntMaxType() == TargetInfo::UnsignedLong) |
| 595 | DefineBuiltinMacro(Buf, "__UINTMAX_TYPE__=unsigned long int"); |
| 596 | else if (TI.getUIntMaxType() == TargetInfo::SignedLong) |
| 597 | DefineBuiltinMacro(Buf, "__UINTMAX_TYPE__=long int"); |
| 598 | else if (TI.getUIntMaxType() == TargetInfo::UnsignedInt) |
| 599 | DefineBuiltinMacro(Buf, "__UINTMAX_TYPE__=unsigned int"); |
| 600 | else |
| 601 | DefineBuiltinMacro(Buf, "__UINTMAX_TYPE__=int"); |
| 602 | |
| 603 | if (TI.getPtrDiffType(0) == TargetInfo::UnsignedLongLong) |
| 604 | DefineBuiltinMacro(Buf, "__PTRDIFF_TYPE__=unsigned long long int"); |
| 605 | else if (TI.getPtrDiffType(0) == TargetInfo::SignedLongLong) |
| 606 | DefineBuiltinMacro(Buf, "__PTRDIFF_TYPE__=long long int"); |
| 607 | else if (TI.getPtrDiffType(0) == TargetInfo::UnsignedLong) |
| 608 | DefineBuiltinMacro(Buf, "__PTRDIFF_TYPE__=unsigned long int"); |
| 609 | else if (TI.getPtrDiffType(0) == TargetInfo::SignedLong) |
| 610 | DefineBuiltinMacro(Buf, "__PTRDIFF_TYPE__=long int"); |
| 611 | else if (TI.getPtrDiffType(0) == TargetInfo::UnsignedInt) |
| 612 | DefineBuiltinMacro(Buf, "__PTRDIFF_TYPE__=unsigned int"); |
| 613 | else |
| 614 | DefineBuiltinMacro(Buf, "__PTRDIFF_TYPE__=int"); |
| 615 | |
| 616 | if (TI.getSizeType() == TargetInfo::UnsignedLongLong) |
| 617 | DefineBuiltinMacro(Buf, "__SIZE_TYPE__=unsigned long long int"); |
| 618 | else if (TI.getSizeType() == TargetInfo::SignedLongLong) |
| 619 | DefineBuiltinMacro(Buf, "__SIZE_TYPE__=long long int"); |
| 620 | else if (TI.getSizeType() == TargetInfo::UnsignedLong) |
| 621 | DefineBuiltinMacro(Buf, "__SIZE_TYPE__=unsigned long int"); |
| 622 | else if (TI.getSizeType() == TargetInfo::SignedLong) |
| 623 | DefineBuiltinMacro(Buf, "__SIZE_TYPE__=long int"); |
| 624 | else if (TI.getSizeType() == TargetInfo::UnsignedInt) |
| 625 | DefineBuiltinMacro(Buf, "__SIZE_TYPE__=unsigned int"); |
| 626 | else if (TI.getSizeType() == TargetInfo::SignedInt) |
| 627 | DefineBuiltinMacro(Buf, "__SIZE_TYPE__=int"); |
| 628 | else |
| 629 | DefineBuiltinMacro(Buf, "__SIZE_TYPE__=unsigned short"); |
| 630 | |
Chris Lattner | 2db78dd | 2008-10-05 20:40:30 +0000 | [diff] [blame] | 631 | DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat()); |
| 632 | DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat()); |
| 633 | DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat()); |
Chris Lattner | 0e5d4ef | 2008-10-05 20:06:37 +0000 | [diff] [blame] | 634 | |
Chris Lattner | 048dd94 | 2008-09-30 00:48:48 +0000 | [diff] [blame] | 635 | |
Chris Lattner | d19144b | 2007-10-10 17:48:53 +0000 | [diff] [blame] | 636 | // Add __builtin_va_list typedef. |
| 637 | { |
Chris Lattner | 9b53316 | 2008-10-05 19:44:25 +0000 | [diff] [blame] | 638 | const char *VAList = TI.getVAListDeclaration(); |
Chris Lattner | d19144b | 2007-10-10 17:48:53 +0000 | [diff] [blame] | 639 | Buf.insert(Buf.end(), VAList, VAList+strlen(VAList)); |
| 640 | Buf.push_back('\n'); |
| 641 | } |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 642 | |
Chris Lattner | 9b53316 | 2008-10-05 19:44:25 +0000 | [diff] [blame] | 643 | if (const char *Prefix = TI.getUserLabelPrefix()) { |
Chris Lattner | 12f0926 | 2008-10-05 21:49:27 +0000 | [diff] [blame] | 644 | sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix); |
| 645 | DefineBuiltinMacro(Buf, MacroBuf); |
Chris Lattner | 3fdf467 | 2008-10-05 19:22:37 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Chris Lattner | 9b53316 | 2008-10-05 19:44:25 +0000 | [diff] [blame] | 648 | // Build configuration options. FIXME: these should be controlled by |
| 649 | // command line options or something. |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 650 | DefineBuiltinMacro(Buf, "__DYNAMIC__=1"); |
| 651 | DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0"); |
| 652 | DefineBuiltinMacro(Buf, "__NO_INLINE__=1"); |
| 653 | DefineBuiltinMacro(Buf, "__PIC__=1"); |
Chris Lattner | 9b53316 | 2008-10-05 19:44:25 +0000 | [diff] [blame] | 654 | |
Chris Lattner | 12f0926 | 2008-10-05 21:49:27 +0000 | [diff] [blame] | 655 | // Macros to control C99 numerics and <float.h> |
| 656 | DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0"); |
| 657 | DefineBuiltinMacro(Buf, "__FLT_RADIX__=2"); |
| 658 | sprintf(MacroBuf, "__DECIMAL_DIG__=%d", |
| 659 | PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33)); |
| 660 | DefineBuiltinMacro(Buf, MacroBuf); |
| 661 | |
Chris Lattner | 9b53316 | 2008-10-05 19:44:25 +0000 | [diff] [blame] | 662 | // Get other target #defines. |
| 663 | TI.getTargetDefines(Buf); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 664 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 665 | // FIXME: Should emit a #line directive here. |
| 666 | } |
| 667 | |
| 668 | |
| 669 | /// EnterMainSourceFile - Enter the specified FileID as the main source file, |
Nate Begeman | 6b61602 | 2008-01-07 04:01:26 +0000 | [diff] [blame] | 670 | /// which implicitly adds the builtin defines etc. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 671 | void Preprocessor::EnterMainSourceFile() { |
| 672 | |
| 673 | unsigned MainFileID = SourceMgr.getMainFileID(); |
| 674 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 675 | // Enter the main file source buffer. |
| 676 | EnterSourceFile(MainFileID, 0); |
| 677 | |
Chris Lattner | b283298 | 2007-11-15 19:07:47 +0000 | [diff] [blame] | 678 | // Tell the header info that the main file was entered. If the file is later |
| 679 | // #imported, it won't be re-entered. |
| 680 | if (const FileEntry *FE = |
| 681 | SourceMgr.getFileEntryForLoc(SourceLocation::getFileLoc(MainFileID, 0))) |
| 682 | HeaderInfo.IncrementIncludeCount(FE); |
| 683 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 684 | std::vector<char> PrologFile; |
| 685 | PrologFile.reserve(4080); |
| 686 | |
| 687 | // Install things like __POWERPC__, __GNUC__, etc into the macro table. |
| 688 | InitializePredefinedMacros(*this, PrologFile); |
| 689 | |
| 690 | // Add on the predefines from the driver. |
Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 691 | PrologFile.insert(PrologFile.end(), Predefines.begin(), Predefines.end()); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 692 | |
| 693 | // Memory buffer must end with a null byte! |
| 694 | PrologFile.push_back(0); |
| 695 | |
| 696 | // Now that we have emitted the predefined macros, #includes, etc into |
| 697 | // PrologFile, preprocess it to populate the initial preprocessor state. |
| 698 | llvm::MemoryBuffer *SB = |
| 699 | llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(), |
| 700 | "<predefines>"); |
| 701 | assert(SB && "Cannot fail to create predefined source buffer"); |
| 702 | unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB); |
| 703 | assert(FileID && "Could not create FileID for predefines?"); |
| 704 | |
| 705 | // Start parsing the predefines. |
| 706 | EnterSourceFile(FileID, 0); |
| 707 | } |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 708 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 709 | |
| 710 | //===----------------------------------------------------------------------===// |
| 711 | // Lexer Event Handling. |
| 712 | //===----------------------------------------------------------------------===// |
| 713 | |
| 714 | /// LookUpIdentifierInfo - Given a tok::identifier token, look up the |
| 715 | /// identifier information for the token and install it into the token. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 716 | IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 717 | const char *BufPtr) { |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 718 | assert(Identifier.is(tok::identifier) && "Not an identifier!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 719 | assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!"); |
| 720 | |
| 721 | // Look up this token, see if it is a macro, or if it is a language keyword. |
| 722 | IdentifierInfo *II; |
| 723 | if (BufPtr && !Identifier.needsCleaning()) { |
| 724 | // No cleaning needed, just use the characters from the lexed buffer. |
| 725 | II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength()); |
| 726 | } else { |
| 727 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
Chris Lattner | c35717a | 2007-07-13 17:10:38 +0000 | [diff] [blame] | 728 | llvm::SmallVector<char, 64> IdentifierBuffer; |
| 729 | IdentifierBuffer.resize(Identifier.getLength()); |
| 730 | const char *TmpBuf = &IdentifierBuffer[0]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 731 | unsigned Size = getSpelling(Identifier, TmpBuf); |
| 732 | II = getIdentifierInfo(TmpBuf, TmpBuf+Size); |
| 733 | } |
| 734 | Identifier.setIdentifierInfo(II); |
| 735 | return II; |
| 736 | } |
| 737 | |
| 738 | |
| 739 | /// HandleIdentifier - This callback is invoked when the lexer reads an |
| 740 | /// identifier. This callback looks up the identifier in the map and/or |
| 741 | /// potentially macro expands it or turns it into a named token (like 'for'). |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 742 | void Preprocessor::HandleIdentifier(Token &Identifier) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 743 | assert(Identifier.getIdentifierInfo() && |
| 744 | "Can't handle identifiers without identifier info!"); |
| 745 | |
| 746 | IdentifierInfo &II = *Identifier.getIdentifierInfo(); |
| 747 | |
| 748 | // If this identifier was poisoned, and if it was not produced from a macro |
| 749 | // expansion, emit an error. |
| 750 | if (II.isPoisoned() && CurLexer) { |
| 751 | if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning. |
| 752 | Diag(Identifier, diag::err_pp_used_poisoned_id); |
| 753 | else |
| 754 | Diag(Identifier, diag::ext_pp_bad_vaargs_use); |
| 755 | } |
| 756 | |
| 757 | // If this is a macro to be expanded, do it. |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 758 | if (MacroInfo *MI = getMacroInfo(&II)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 759 | if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) { |
| 760 | if (MI->isEnabled()) { |
| 761 | if (!HandleMacroExpandedIdentifier(Identifier, MI)) |
| 762 | return; |
| 763 | } else { |
| 764 | // C99 6.10.3.4p2 says that a disabled macro may never again be |
| 765 | // expanded, even if it's in a context where it could be expanded in the |
| 766 | // future. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 767 | Identifier.setFlag(Token::DisableExpand); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 768 | } |
| 769 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | // C++ 2.11p2: If this is an alternative representation of a C++ operator, |
| 773 | // then we act as if it is the actual operator and not the textual |
| 774 | // representation of it. |
| 775 | if (II.isCPlusPlusOperatorKeyword()) |
| 776 | Identifier.setIdentifierInfo(0); |
| 777 | |
| 778 | // Change the kind of this identifier to the appropriate token kind, e.g. |
| 779 | // turning "for" into a keyword. |
| 780 | Identifier.setKind(II.getTokenID()); |
| 781 | |
| 782 | // If this is an extension token, diagnose its use. |
Steve Naroff | b4eaf9c | 2008-09-02 18:50:17 +0000 | [diff] [blame] | 783 | // We avoid diagnosing tokens that originate from macro definitions. |
| 784 | if (II.isExtensionToken() && Features.C99 && !DisableMacroExpansion) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 785 | Diag(Identifier, diag::ext_token_used); |
| 786 | } |