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