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. |
Chris Lattner | f73903a | 2009-02-06 06:45:26 +0000 | [diff] [blame] | 16 | // -d[DNI] - Dump various things. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 33 | #include "clang/Lex/LexDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 35 | #include "clang/Basic/FileManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 2db78dd | 2008-10-05 20:40:30 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/APFloat.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 39 | #include "llvm/Support/MemoryBuffer.h" |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Streams.h" |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 41 | #include <cstdio> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 42 | using namespace clang; |
| 43 | |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | |
Ted Kremenek | ec6c574 | 2008-04-17 21:23:07 +0000 | [diff] [blame] | 46 | PreprocessorFactory::~PreprocessorFactory() {} |
| 47 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts, |
| 49 | TargetInfo &target, SourceManager &SM, |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 50 | HeaderSearch &Headers, |
| 51 | IdentifierInfoLookup* IILookup) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()), |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 53 | SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts, IILookup), |
Ted Kremenek | 4b71e3e | 2008-11-19 00:44:06 +0000 | [diff] [blame] | 54 | CurPPLexer(0), CurDirLookup(0), Callbacks(0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | ScratchBuf = new ScratchBuffer(SourceMgr); |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 56 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 57 | // Clear stats. |
| 58 | NumDirectives = NumDefined = NumUndefined = NumPragma = 0; |
| 59 | NumIf = NumElse = NumEndif = 0; |
| 60 | NumEnteredSourceFiles = 0; |
| 61 | NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0; |
| 62 | NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0; |
| 63 | MaxIncludeStackDepth = 0; |
| 64 | NumSkipped = 0; |
| 65 | |
| 66 | // Default to discarding comments. |
| 67 | KeepComments = false; |
| 68 | KeepMacroComments = false; |
| 69 | |
| 70 | // Macro expansion is enabled. |
| 71 | DisableMacroExpansion = false; |
| 72 | InMacroArgs = false; |
Chris Lattner | 6cfe759 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 73 | NumCachedTokenLexers = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | |
Argyrios Kyrtzidis | 03db1b3 | 2008-08-10 13:15:22 +0000 | [diff] [blame] | 75 | CachedLexPos = 0; |
| 76 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 78 | // This gets unpoisoned where it is allowed. |
| 79 | (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned(); |
| 80 | |
| 81 | // Initialize the pragma handlers. |
| 82 | PragmaHandlers = new PragmaNamespace(0); |
| 83 | RegisterBuiltinPragmas(); |
| 84 | |
| 85 | // Initialize builtin macros like __LINE__ and friends. |
| 86 | RegisterBuiltinMacros(); |
| 87 | } |
| 88 | |
| 89 | Preprocessor::~Preprocessor() { |
Argyrios Kyrtzidis | 2174a4f | 2008-08-23 12:12:06 +0000 | [diff] [blame] | 90 | assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!"); |
| 91 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 92 | while (!IncludeMacroStack.empty()) { |
| 93 | delete IncludeMacroStack.back().TheLexer; |
Chris Lattner | 6cfe759 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 94 | delete IncludeMacroStack.back().TheTokenLexer; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 95 | IncludeMacroStack.pop_back(); |
| 96 | } |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 97 | |
| 98 | // Free any macro definitions. |
| 99 | for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I = |
| 100 | Macros.begin(), E = Macros.end(); I != E; ++I) { |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 101 | // We don't need to free the MacroInfo objects directly. These |
| 102 | // will be released when the BumpPtrAllocator 'BP' object gets |
Ted Kremenek | 9ee7d45 | 2009-01-19 07:45:44 +0000 | [diff] [blame] | 103 | // destroyed. We still need to run the dstor, however, to free |
| 104 | // memory alocated by MacroInfo. |
Chris Lattner | 685befe | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 105 | I->second->Destroy(BP); |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 106 | I->first->setHasMacroDefinition(false); |
| 107 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 109 | // Free any cached macro expanders. |
Chris Lattner | 6cfe759 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 110 | for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i) |
| 111 | delete TokenLexerCache[i]; |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 112 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 113 | // Release pragma information. |
| 114 | delete PragmaHandlers; |
| 115 | |
| 116 | // Delete the scratch buffer info. |
| 117 | delete ScratchBuf; |
Chris Lattner | eb50ed8 | 2008-03-14 06:07:05 +0000 | [diff] [blame] | 118 | |
| 119 | delete Callbacks; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 122 | void Preprocessor::setPTHManager(PTHManager* pm) { |
| 123 | PTH.reset(pm); |
| 124 | FileMgr.setStatCache(PTH->createStatCache()); |
| 125 | } |
| 126 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 127 | void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const { |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 128 | llvm::cerr << tok::getTokenName(Tok.getKind()) << " '" |
| 129 | << getSpelling(Tok) << "'"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | |
| 131 | if (!DumpFlags) return; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 132 | |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 133 | llvm::cerr << "\t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 134 | if (Tok.isAtStartOfLine()) |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 135 | llvm::cerr << " [StartOfLine]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 136 | if (Tok.hasLeadingSpace()) |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 137 | llvm::cerr << " [LeadingSpace]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 138 | if (Tok.isExpandDisabled()) |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 139 | llvm::cerr << " [ExpandDisabled]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 140 | if (Tok.needsCleaning()) { |
| 141 | const char *Start = SourceMgr.getCharacterData(Tok.getLocation()); |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 142 | llvm::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength()) |
| 143 | << "']"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 144 | } |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 145 | |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 146 | llvm::cerr << "\tLoc=<"; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 147 | DumpLocation(Tok.getLocation()); |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 148 | llvm::cerr << ">"; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void Preprocessor::DumpLocation(SourceLocation Loc) const { |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 152 | Loc.dump(SourceMgr); |
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!"); |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 198 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 199 | // If this token contains nothing interesting, return it directly. |
Ted Kremenek | 277faca | 2009-01-27 00:01:05 +0000 | [diff] [blame] | 200 | const char* TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 | } |
Ted Kremenek | b70e3da | 2009-01-08 02:47:16 +0000 | [diff] [blame] | 239 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 240 | // Otherwise, compute the start of the token in the input lexer buffer. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 241 | const char *TokStart = 0; |
| 242 | |
| 243 | if (Tok.isLiteral()) |
| 244 | TokStart = Tok.getLiteralData(); |
| 245 | |
| 246 | if (TokStart == 0) |
| 247 | TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 248 | |
| 249 | // If this token contains nothing interesting, return it directly. |
| 250 | if (!Tok.needsCleaning()) { |
| 251 | Buffer = TokStart; |
| 252 | return Tok.getLength(); |
| 253 | } |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 254 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 255 | // Otherwise, hard case, relex the characters into the string. |
| 256 | char *OutBuf = const_cast<char*>(Buffer); |
| 257 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 258 | Ptr != End; ) { |
| 259 | unsigned CharSize; |
| 260 | *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features); |
| 261 | Ptr += CharSize; |
| 262 | } |
| 263 | assert(unsigned(OutBuf-Buffer) != Tok.getLength() && |
| 264 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 265 | |
| 266 | return OutBuf-Buffer; |
| 267 | } |
| 268 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 269 | /// CreateString - Plop the specified string into a scratch buffer and return a |
| 270 | /// location for it. If specified, the source location provides a source |
| 271 | /// location for the token. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 272 | void Preprocessor::CreateString(const char *Buf, unsigned Len, Token &Tok, |
| 273 | SourceLocation InstantiationLoc) { |
| 274 | Tok.setLength(Len); |
| 275 | |
| 276 | const char *DestPtr; |
| 277 | SourceLocation Loc = ScratchBuf->getToken(Buf, Len, DestPtr); |
| 278 | |
| 279 | if (InstantiationLoc.isValid()) |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 280 | Loc = SourceMgr.createInstantiationLoc(Loc, InstantiationLoc, |
| 281 | InstantiationLoc, Len); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 282 | Tok.setLocation(Loc); |
| 283 | |
| 284 | // If this is a literal token, set the pointer data. |
| 285 | if (Tok.isLiteral()) |
| 286 | Tok.setLiteralData(DestPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 290 | /// AdvanceToTokenCharacter - Given a location that specifies the start of a |
| 291 | /// token, return a new location that specifies a character within the token. |
| 292 | SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart, |
| 293 | unsigned CharNo) { |
Chris Lattner | 6dcf63e | 2009-02-18 18:56:29 +0000 | [diff] [blame] | 294 | // If they request the first char of the token, we're trivially done. |
Chris Lattner | 2197c96 | 2009-02-18 18:52:52 +0000 | [diff] [blame] | 295 | if (CharNo == 0) return TokStart; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 296 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 297 | // Figure out how many physical characters away the specified instantiation |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 298 | // character is. This needs to take into consideration newlines and |
| 299 | // trigraphs. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 300 | const char *TokPtr = SourceMgr.getCharacterData(TokStart); |
| 301 | unsigned PhysOffset = 0; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 302 | |
| 303 | // The usual case is that tokens don't contain anything interesting. Skip |
| 304 | // over the uninteresting characters. If a token only consists of simple |
| 305 | // chars, this method is extremely fast. |
| 306 | while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr)) |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 307 | ++TokPtr, --CharNo, ++PhysOffset; |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 308 | |
Chris Lattner | 28c90ad | 2009-01-17 07:57:25 +0000 | [diff] [blame] | 309 | // If we have a character that may be a trigraph or escaped newline, use a |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 310 | // lexer to parse it correctly. |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 311 | if (CharNo != 0) { |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 312 | // Skip over characters the remaining characters. |
Chris Lattner | 28c90ad | 2009-01-17 07:57:25 +0000 | [diff] [blame] | 313 | for (; CharNo; --CharNo) { |
| 314 | unsigned Size; |
| 315 | Lexer::getCharAndSizeNoWarn(TokPtr, Size, Features); |
| 316 | TokPtr += Size; |
| 317 | PhysOffset += Size; |
| 318 | } |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 319 | } |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 320 | |
| 321 | return TokStart.getFileLocWithOffset(PhysOffset); |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Douglas Gregor | b2fb6de | 2009-02-27 17:53:17 +0000 | [diff] [blame] | 324 | /// \brief Computes the source location just past the end of the |
| 325 | /// token at this source location. |
| 326 | /// |
| 327 | /// This routine can be used to produce a source location that |
| 328 | /// points just past the end of the token referenced by \p Loc, and |
| 329 | /// is generally used when a diagnostic needs to point just after a |
| 330 | /// token where it expected something different that it received. If |
| 331 | /// the returned source location would not be meaningful (e.g., if |
| 332 | /// it points into a macro), this routine returns an invalid |
| 333 | /// source location. |
| 334 | SourceLocation Preprocessor::getLocForEndOfToken(SourceLocation Loc) { |
| 335 | if (Loc.isInvalid() || !Loc.isFileID()) |
| 336 | return SourceLocation(); |
| 337 | |
| 338 | unsigned Len = Lexer::MeasureTokenLength(Loc, getSourceManager()); |
| 339 | return AdvanceToTokenCharacter(Loc, Len); |
| 340 | } |
| 341 | |
| 342 | |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 343 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 344 | //===----------------------------------------------------------------------===// |
| 345 | // Preprocessor Initialization Methods |
| 346 | //===----------------------------------------------------------------------===// |
| 347 | |
| 348 | // Append a #define line to Buf for Macro. Macro should be of the form XXX, |
| 349 | // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit |
| 350 | // "#define XXX Y z W". To get a #define with no value, use "XXX=". |
| 351 | static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro, |
| 352 | const char *Command = "#define ") { |
| 353 | Buf.insert(Buf.end(), Command, Command+strlen(Command)); |
| 354 | if (const char *Equal = strchr(Macro, '=')) { |
| 355 | // Turn the = into ' '. |
| 356 | Buf.insert(Buf.end(), Macro, Equal); |
| 357 | Buf.push_back(' '); |
| 358 | Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal)); |
| 359 | } else { |
| 360 | // Push "macroname 1". |
| 361 | Buf.insert(Buf.end(), Macro, Macro+strlen(Macro)); |
| 362 | Buf.push_back(' '); |
| 363 | Buf.push_back('1'); |
| 364 | } |
| 365 | Buf.push_back('\n'); |
| 366 | } |
| 367 | |
Chris Lattner | 2db78dd | 2008-10-05 20:40:30 +0000 | [diff] [blame] | 368 | /// PickFP - This is used to pick a value based on the FP semantics of the |
| 369 | /// specified FP model. |
| 370 | template <typename T> |
| 371 | static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal, |
| 372 | T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal) { |
| 373 | if (Sem == &llvm::APFloat::IEEEsingle) |
| 374 | return IEEESingleVal; |
| 375 | if (Sem == &llvm::APFloat::IEEEdouble) |
| 376 | return IEEEDoubleVal; |
| 377 | if (Sem == &llvm::APFloat::x87DoubleExtended) |
| 378 | return X87DoubleExtendedVal; |
| 379 | assert(Sem == &llvm::APFloat::PPCDoubleDouble); |
| 380 | return PPCDoubleDoubleVal; |
| 381 | } |
| 382 | |
| 383 | static void DefineFloatMacros(std::vector<char> &Buf, const char *Prefix, |
| 384 | const llvm::fltSemantics *Sem) { |
Chris Lattner | e9863ca | 2008-10-05 21:40:58 +0000 | [diff] [blame] | 385 | const char *DenormMin, *Epsilon, *Max, *Min; |
| 386 | DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324", |
| 387 | "3.64519953188247460253e-4951L", |
| 388 | "4.94065645841246544176568792868221e-324L"); |
| 389 | int Digits = PickFP(Sem, 6, 15, 18, 31); |
| 390 | Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16", |
| 391 | "1.08420217248550443401e-19L", |
| 392 | "4.94065645841246544176568792868221e-324L"); |
| 393 | int HasInifinity = 1, HasQuietNaN = 1; |
| 394 | int MantissaDigits = PickFP(Sem, 24, 53, 64, 106); |
| 395 | int Min10Exp = PickFP(Sem, -37, -307, -4931, -291); |
| 396 | int Max10Exp = PickFP(Sem, 38, 308, 4932, 308); |
| 397 | int MinExp = PickFP(Sem, -125, -1021, -16381, -968); |
| 398 | int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024); |
| 399 | Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308", |
| 400 | "3.36210314311209350626e-4932L", |
| 401 | "2.00416836000897277799610805135016e-292L"); |
| 402 | Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308", |
| 403 | "1.18973149535723176502e+4932L", |
| 404 | "1.79769313486231580793728971405301e+308L"); |
Chris Lattner | 2db78dd | 2008-10-05 20:40:30 +0000 | [diff] [blame] | 405 | |
| 406 | char MacroBuf[60]; |
| 407 | sprintf(MacroBuf, "__%s_DENORM_MIN__=%s", Prefix, DenormMin); |
| 408 | DefineBuiltinMacro(Buf, MacroBuf); |
Chris Lattner | e9863ca | 2008-10-05 21:40:58 +0000 | [diff] [blame] | 409 | sprintf(MacroBuf, "__%s_DIG__=%d", Prefix, Digits); |
| 410 | DefineBuiltinMacro(Buf, MacroBuf); |
| 411 | sprintf(MacroBuf, "__%s_EPSILON__=%s", Prefix, Epsilon); |
| 412 | DefineBuiltinMacro(Buf, MacroBuf); |
| 413 | sprintf(MacroBuf, "__%s_HAS_INFINITY__=%d", Prefix, HasInifinity); |
| 414 | DefineBuiltinMacro(Buf, MacroBuf); |
| 415 | sprintf(MacroBuf, "__%s_HAS_QUIET_NAN__=%d", Prefix, HasQuietNaN); |
| 416 | DefineBuiltinMacro(Buf, MacroBuf); |
| 417 | sprintf(MacroBuf, "__%s_MANT_DIG__=%d", Prefix, MantissaDigits); |
| 418 | DefineBuiltinMacro(Buf, MacroBuf); |
| 419 | sprintf(MacroBuf, "__%s_MAX_10_EXP__=%d", Prefix, Max10Exp); |
| 420 | DefineBuiltinMacro(Buf, MacroBuf); |
| 421 | sprintf(MacroBuf, "__%s_MAX_EXP__=%d", Prefix, MaxExp); |
| 422 | DefineBuiltinMacro(Buf, MacroBuf); |
| 423 | sprintf(MacroBuf, "__%s_MAX__=%s", Prefix, Max); |
| 424 | DefineBuiltinMacro(Buf, MacroBuf); |
| 425 | sprintf(MacroBuf, "__%s_MIN_10_EXP__=(%d)", Prefix, Min10Exp); |
| 426 | DefineBuiltinMacro(Buf, MacroBuf); |
| 427 | sprintf(MacroBuf, "__%s_MIN_EXP__=(%d)", Prefix, MinExp); |
| 428 | DefineBuiltinMacro(Buf, MacroBuf); |
| 429 | sprintf(MacroBuf, "__%s_MIN__=%s", Prefix, Min); |
| 430 | DefineBuiltinMacro(Buf, MacroBuf); |
Chris Lattner | d427ad4 | 2009-02-05 07:19:24 +0000 | [diff] [blame] | 431 | sprintf(MacroBuf, "__%s_HAS_DENORM__=1", Prefix); |
| 432 | DefineBuiltinMacro(Buf, MacroBuf); |
Chris Lattner | 2db78dd | 2008-10-05 20:40:30 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 435 | |
Chris Lattner | 996fecc | 2009-02-06 04:50:25 +0000 | [diff] [blame] | 436 | /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro |
| 437 | /// named MacroName with the max value for a type with width 'TypeWidth' a |
| 438 | /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL). |
| 439 | static void DefineTypeSize(const char *MacroName, unsigned TypeWidth, |
| 440 | const char *ValSuffix, bool isSigned, |
| 441 | std::vector<char> &Buf) { |
| 442 | char MacroBuf[60]; |
Mike Stump | 2ace928 | 2009-03-07 18:35:41 +0000 | [diff] [blame] | 443 | long long MaxVal; |
Chris Lattner | 996fecc | 2009-02-06 04:50:25 +0000 | [diff] [blame] | 444 | if (isSigned) |
| 445 | MaxVal = (1LL << (TypeWidth - 1)) - 1; |
| 446 | else |
| 447 | MaxVal = ~0LL >> (64-TypeWidth); |
| 448 | |
| 449 | sprintf(MacroBuf, "%s=%llu%s", MacroName, MaxVal, ValSuffix); |
| 450 | DefineBuiltinMacro(Buf, MacroBuf); |
| 451 | } |
| 452 | |
Chris Lattner | 2b5abf5 | 2009-02-06 05:04:11 +0000 | [diff] [blame] | 453 | static void DefineType(const char *MacroName, TargetInfo::IntType Ty, |
| 454 | std::vector<char> &Buf) { |
| 455 | char MacroBuf[60]; |
| 456 | sprintf(MacroBuf, "%s=%s", MacroName, TargetInfo::getTypeName(Ty)); |
| 457 | DefineBuiltinMacro(Buf, MacroBuf); |
| 458 | } |
| 459 | |
| 460 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 461 | static void InitializePredefinedMacros(Preprocessor &PP, |
| 462 | std::vector<char> &Buf) { |
Chris Lattner | 03c9727 | 2009-02-06 22:59:26 +0000 | [diff] [blame] | 463 | char MacroBuf[60]; |
Chris Lattner | 62213d9 | 2008-10-05 19:32:22 +0000 | [diff] [blame] | 464 | // Compiler version introspection macros. |
| 465 | DefineBuiltinMacro(Buf, "__llvm__=1"); // LLVM Backend |
| 466 | DefineBuiltinMacro(Buf, "__clang__=1"); // Clang Frontend |
| 467 | |
| 468 | // Currently claim to be compatible with GCC 4.2.1-5621. |
| 469 | DefineBuiltinMacro(Buf, "__APPLE_CC__=5621"); |
| 470 | DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2"); |
| 471 | DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1"); |
| 472 | DefineBuiltinMacro(Buf, "__GNUC__=4"); |
| 473 | DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002"); |
| 474 | DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 (Apple Computer, Inc. " |
| 475 | "build 5621) (dot 3)\""); |
| 476 | |
| 477 | |
| 478 | // Initialize language-specific preprocessor defines. |
| 479 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 480 | // These should all be defined in the preprocessor according to the |
| 481 | // current language configuration. |
Steve Naroff | 7e0fbb2 | 2008-12-18 22:37:25 +0000 | [diff] [blame] | 482 | if (!PP.getLangOptions().Microsoft) |
| 483 | DefineBuiltinMacro(Buf, "__STDC__=1"); |
Daniel Dunbar | c157145 | 2008-12-01 18:55:22 +0000 | [diff] [blame] | 484 | if (PP.getLangOptions().AsmPreprocessor) |
| 485 | DefineBuiltinMacro(Buf, "__ASSEMBLER__=1"); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 486 | if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus) |
| 487 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L"); |
| 488 | else if (0) // STDC94 ? |
| 489 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L"); |
| 490 | |
Chris Lattner | 8d4a9d3 | 2009-03-09 21:50:12 +0000 | [diff] [blame] | 491 | if (PP.getLangOptions().Freestanding) |
| 492 | DefineBuiltinMacro(Buf, "__STDC_HOSTED__=0"); |
| 493 | else |
| 494 | DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1"); |
| 495 | |
Daniel Dunbar | fba5cb1 | 2008-08-12 00:21:46 +0000 | [diff] [blame] | 496 | if (PP.getLangOptions().ObjC1) { |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 497 | DefineBuiltinMacro(Buf, "__OBJC__=1"); |
Fariborz Jahanian | 2639e4f | 2009-02-16 18:28:48 +0000 | [diff] [blame] | 498 | if (PP.getLangOptions().ObjCNonFragileABI) |
| 499 | DefineBuiltinMacro(Buf, "__OBJC2__=1"); |
Daniel Dunbar | fba5cb1 | 2008-08-12 00:21:46 +0000 | [diff] [blame] | 500 | |
| 501 | if (PP.getLangOptions().getGCMode() == LangOptions::NonGC) { |
| 502 | DefineBuiltinMacro(Buf, "__weak="); |
| 503 | DefineBuiltinMacro(Buf, "__strong="); |
| 504 | } else { |
| 505 | DefineBuiltinMacro(Buf, "__weak=__attribute__((objc_gc(weak)))"); |
| 506 | DefineBuiltinMacro(Buf, "__strong=__attribute__((objc_gc(strong)))"); |
| 507 | DefineBuiltinMacro(Buf, "__OBJC_GC__=1"); |
| 508 | } |
| 509 | |
| 510 | if (PP.getLangOptions().NeXTRuntime) |
| 511 | DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1"); |
Daniel Dunbar | fba5cb1 | 2008-08-12 00:21:46 +0000 | [diff] [blame] | 512 | } |
Chris Lattner | 9b53316 | 2008-10-05 19:44:25 +0000 | [diff] [blame] | 513 | |
Chris Lattner | eb52b44 | 2008-10-06 07:43:09 +0000 | [diff] [blame] | 514 | // darwin_constant_cfstrings controls this. This is also dependent |
| 515 | // on other things like the runtime I believe. This is set even for C code. |
| 516 | DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1"); |
| 517 | |
Steve Naroff | 73b17cd | 2008-05-15 21:12:10 +0000 | [diff] [blame] | 518 | if (PP.getLangOptions().ObjC2) |
| 519 | DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES"); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 520 | |
Chris Lattner | 048dd94 | 2008-09-30 00:48:48 +0000 | [diff] [blame] | 521 | if (PP.getLangOptions().PascalStrings) |
| 522 | DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__"); |
| 523 | |
Chris Lattner | 62213d9 | 2008-10-05 19:32:22 +0000 | [diff] [blame] | 524 | if (PP.getLangOptions().Blocks) { |
| 525 | DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))"); |
| 526 | DefineBuiltinMacro(Buf, "__BLOCKS__=1"); |
Chris Lattner | 2b43ad9 | 2008-10-05 19:32:52 +0000 | [diff] [blame] | 527 | } |
Chris Lattner | 62213d9 | 2008-10-05 19:32:22 +0000 | [diff] [blame] | 528 | |
| 529 | if (PP.getLangOptions().CPlusPlus) { |
| 530 | DefineBuiltinMacro(Buf, "__DEPRECATED=1"); |
| 531 | DefineBuiltinMacro(Buf, "__EXCEPTIONS=1"); |
| 532 | DefineBuiltinMacro(Buf, "__GNUG__=4"); |
| 533 | DefineBuiltinMacro(Buf, "__GXX_WEAK__=1"); |
| 534 | DefineBuiltinMacro(Buf, "__cplusplus=1"); |
| 535 | DefineBuiltinMacro(Buf, "__private_extern__=extern"); |
| 536 | } |
| 537 | |
| 538 | // Filter out some microsoft extensions when trying to parse in ms-compat |
| 539 | // mode. |
| 540 | if (PP.getLangOptions().Microsoft) { |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 541 | DefineBuiltinMacro(Buf, "_cdecl=__cdecl"); |
Chris Lattner | 03c9727 | 2009-02-06 22:59:26 +0000 | [diff] [blame] | 542 | DefineBuiltinMacro(Buf, "__int8=__INT8_TYPE__"); |
| 543 | DefineBuiltinMacro(Buf, "__int16=__INT16_TYPE__"); |
| 544 | DefineBuiltinMacro(Buf, "__int32=__INT32_TYPE__"); |
| 545 | DefineBuiltinMacro(Buf, "__int64=__INT64_TYPE__"); |
Chris Lattner | 62213d9 | 2008-10-05 19:32:22 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Chris Lattner | 62213d9 | 2008-10-05 19:32:22 +0000 | [diff] [blame] | 548 | // Initialize target-specific preprocessor defines. |
Chris Lattner | 9b53316 | 2008-10-05 19:44:25 +0000 | [diff] [blame] | 549 | const TargetInfo &TI = PP.getTargetInfo(); |
| 550 | |
| 551 | // Define type sizing macros based on the target properties. |
| 552 | assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far"); |
| 553 | DefineBuiltinMacro(Buf, "__CHAR_BIT__=8"); |
Chris Lattner | 2db78dd | 2008-10-05 20:40:30 +0000 | [diff] [blame] | 554 | |
Chris Lattner | 659dc14 | 2009-02-05 07:27:41 +0000 | [diff] [blame] | 555 | unsigned IntMaxWidth; |
| 556 | const char *IntMaxSuffix; |
| 557 | if (TI.getIntMaxType() == TargetInfo::SignedLongLong) { |
| 558 | IntMaxWidth = TI.getLongLongWidth(); |
| 559 | IntMaxSuffix = "LL"; |
| 560 | } else if (TI.getIntMaxType() == TargetInfo::SignedLong) { |
| 561 | IntMaxWidth = TI.getLongWidth(); |
| 562 | IntMaxSuffix = "L"; |
| 563 | } else { |
| 564 | assert(TI.getIntMaxType() == TargetInfo::SignedInt); |
| 565 | IntMaxWidth = TI.getIntWidth(); |
| 566 | IntMaxSuffix = ""; |
| 567 | } |
| 568 | |
Chris Lattner | 86d85b8 | 2009-02-06 04:55:18 +0000 | [diff] [blame] | 569 | DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Buf); |
| 570 | DefineTypeSize("__SHRT_MAX__", TI.getShortWidth(), "", true, Buf); |
| 571 | DefineTypeSize("__INT_MAX__", TI.getIntWidth(), "", true, Buf); |
| 572 | DefineTypeSize("__LONG_MAX__", TI.getLongWidth(), "L", true, Buf); |
| 573 | DefineTypeSize("__LONG_LONG_MAX__", TI.getLongLongWidth(), "LL", true, Buf); |
| 574 | DefineTypeSize("__WCHAR_MAX__", TI.getWCharWidth(), "", true, Buf); |
Chris Lattner | 996fecc | 2009-02-06 04:50:25 +0000 | [diff] [blame] | 575 | DefineTypeSize("__INTMAX_MAX__", IntMaxWidth, IntMaxSuffix, true, Buf); |
| 576 | |
Chris Lattner | 2b5abf5 | 2009-02-06 05:04:11 +0000 | [diff] [blame] | 577 | DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Buf); |
| 578 | DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Buf); |
| 579 | DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Buf); |
Chris Lattner | 6ad474f | 2009-02-13 22:28:55 +0000 | [diff] [blame] | 580 | DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Buf); |
Chris Lattner | 2b5abf5 | 2009-02-06 05:04:11 +0000 | [diff] [blame] | 581 | DefineType("__SIZE_TYPE__", TI.getSizeType(), Buf); |
Chris Lattner | 3c3b155 | 2009-02-06 05:06:07 +0000 | [diff] [blame] | 582 | DefineType("__WCHAR_TYPE__", TI.getWCharType(), Buf); |
| 583 | // FIXME: TargetInfo hookize __WINT_TYPE__. |
| 584 | DefineBuiltinMacro(Buf, "__WINT_TYPE__=int"); |
| 585 | |
Chris Lattner | 2db78dd | 2008-10-05 20:40:30 +0000 | [diff] [blame] | 586 | DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat()); |
| 587 | DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat()); |
| 588 | DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat()); |
Chris Lattner | 03c9727 | 2009-02-06 22:59:26 +0000 | [diff] [blame] | 589 | |
| 590 | // Define a __POINTER_WIDTH__ macro for stdint.h. |
| 591 | sprintf(MacroBuf, "__POINTER_WIDTH__=%d", (int)TI.getPointerWidth(0)); |
| 592 | DefineBuiltinMacro(Buf, MacroBuf); |
| 593 | |
| 594 | if (!TI.isCharSigned()) |
| 595 | DefineBuiltinMacro(Buf, "__CHAR_UNSIGNED__"); |
| 596 | |
| 597 | // Define fixed-sized integer types for stdint.h |
| 598 | assert(TI.getCharWidth() == 8 && "unsupported target types"); |
| 599 | assert(TI.getShortWidth() == 16 && "unsupported target types"); |
| 600 | DefineBuiltinMacro(Buf, "__INT8_TYPE__=char"); |
| 601 | DefineBuiltinMacro(Buf, "__INT16_TYPE__=short"); |
| 602 | |
| 603 | if (TI.getIntWidth() == 32) |
| 604 | DefineBuiltinMacro(Buf, "__INT32_TYPE__=int"); |
| 605 | else { |
| 606 | assert(TI.getLongLongWidth() == 32 && "unsupported target types"); |
| 607 | DefineBuiltinMacro(Buf, "__INT32_TYPE__=long long"); |
| 608 | } |
| 609 | |
| 610 | // 16-bit targets doesn't necessarily have a 64-bit type. |
| 611 | if (TI.getLongLongWidth() == 64) |
| 612 | DefineBuiltinMacro(Buf, "__INT64_TYPE__=long long"); |
Chris Lattner | 0e5d4ef | 2008-10-05 20:06:37 +0000 | [diff] [blame] | 613 | |
Chris Lattner | d19144b | 2007-10-10 17:48:53 +0000 | [diff] [blame] | 614 | // Add __builtin_va_list typedef. |
| 615 | { |
Chris Lattner | 9b53316 | 2008-10-05 19:44:25 +0000 | [diff] [blame] | 616 | const char *VAList = TI.getVAListDeclaration(); |
Chris Lattner | d19144b | 2007-10-10 17:48:53 +0000 | [diff] [blame] | 617 | Buf.insert(Buf.end(), VAList, VAList+strlen(VAList)); |
| 618 | Buf.push_back('\n'); |
| 619 | } |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 620 | |
Chris Lattner | 9b53316 | 2008-10-05 19:44:25 +0000 | [diff] [blame] | 621 | if (const char *Prefix = TI.getUserLabelPrefix()) { |
Chris Lattner | 12f0926 | 2008-10-05 21:49:27 +0000 | [diff] [blame] | 622 | sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix); |
| 623 | DefineBuiltinMacro(Buf, MacroBuf); |
Chris Lattner | 3fdf467 | 2008-10-05 19:22:37 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Chris Lattner | 9b53316 | 2008-10-05 19:44:25 +0000 | [diff] [blame] | 626 | // Build configuration options. FIXME: these should be controlled by |
| 627 | // command line options or something. |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 628 | DefineBuiltinMacro(Buf, "__DYNAMIC__=1"); |
| 629 | DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0"); |
| 630 | DefineBuiltinMacro(Buf, "__NO_INLINE__=1"); |
| 631 | DefineBuiltinMacro(Buf, "__PIC__=1"); |
Chris Lattner | 9b53316 | 2008-10-05 19:44:25 +0000 | [diff] [blame] | 632 | |
Chris Lattner | 12f0926 | 2008-10-05 21:49:27 +0000 | [diff] [blame] | 633 | // Macros to control C99 numerics and <float.h> |
| 634 | DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0"); |
| 635 | DefineBuiltinMacro(Buf, "__FLT_RADIX__=2"); |
| 636 | sprintf(MacroBuf, "__DECIMAL_DIG__=%d", |
| 637 | PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33)); |
| 638 | DefineBuiltinMacro(Buf, MacroBuf); |
| 639 | |
Chris Lattner | 9b53316 | 2008-10-05 19:44:25 +0000 | [diff] [blame] | 640 | // Get other target #defines. |
| 641 | TI.getTargetDefines(Buf); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 642 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 643 | // FIXME: Should emit a #line directive here. |
| 644 | } |
| 645 | |
| 646 | |
| 647 | /// EnterMainSourceFile - Enter the specified FileID as the main source file, |
Nate Begeman | 6b61602 | 2008-01-07 04:01:26 +0000 | [diff] [blame] | 648 | /// which implicitly adds the builtin defines etc. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 649 | void Preprocessor::EnterMainSourceFile() { |
Chris Lattner | 05db427 | 2009-02-13 19:33:24 +0000 | [diff] [blame] | 650 | // We do not allow the preprocessor to reenter the main file. Doing so will |
| 651 | // cause FileID's to accumulate information from both runs (e.g. #line |
| 652 | // information) and predefined macros aren't guaranteed to be set properly. |
| 653 | assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!"); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 654 | FileID MainFileID = SourceMgr.getMainFileID(); |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 655 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 656 | // Enter the main file source buffer. |
| 657 | EnterSourceFile(MainFileID, 0); |
| 658 | |
Chris Lattner | b283298 | 2007-11-15 19:07:47 +0000 | [diff] [blame] | 659 | // Tell the header info that the main file was entered. If the file is later |
| 660 | // #imported, it won't be re-entered. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 661 | if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID)) |
Chris Lattner | b283298 | 2007-11-15 19:07:47 +0000 | [diff] [blame] | 662 | HeaderInfo.IncrementIncludeCount(FE); |
| 663 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 664 | std::vector<char> PrologFile; |
| 665 | PrologFile.reserve(4080); |
| 666 | |
| 667 | // Install things like __POWERPC__, __GNUC__, etc into the macro table. |
| 668 | InitializePredefinedMacros(*this, PrologFile); |
| 669 | |
| 670 | // Add on the predefines from the driver. |
Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 671 | PrologFile.insert(PrologFile.end(), Predefines.begin(), Predefines.end()); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 672 | |
| 673 | // Memory buffer must end with a null byte! |
| 674 | PrologFile.push_back(0); |
| 675 | |
| 676 | // Now that we have emitted the predefined macros, #includes, etc into |
| 677 | // PrologFile, preprocess it to populate the initial preprocessor state. |
| 678 | llvm::MemoryBuffer *SB = |
| 679 | llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(), |
| 680 | "<predefines>"); |
| 681 | assert(SB && "Cannot fail to create predefined source buffer"); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 682 | FileID FID = SourceMgr.createFileIDForMemBuffer(SB); |
| 683 | assert(!FID.isInvalid() && "Could not create FileID for predefines?"); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 684 | |
| 685 | // Start parsing the predefines. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 686 | EnterSourceFile(FID, 0); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 687 | } |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 688 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 689 | |
| 690 | //===----------------------------------------------------------------------===// |
| 691 | // Lexer Event Handling. |
| 692 | //===----------------------------------------------------------------------===// |
| 693 | |
| 694 | /// LookUpIdentifierInfo - Given a tok::identifier token, look up the |
| 695 | /// identifier information for the token and install it into the token. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 696 | IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 697 | const char *BufPtr) { |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 698 | assert(Identifier.is(tok::identifier) && "Not an identifier!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 699 | assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!"); |
| 700 | |
| 701 | // Look up this token, see if it is a macro, or if it is a language keyword. |
| 702 | IdentifierInfo *II; |
| 703 | if (BufPtr && !Identifier.needsCleaning()) { |
| 704 | // No cleaning needed, just use the characters from the lexed buffer. |
| 705 | II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength()); |
| 706 | } else { |
| 707 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
Chris Lattner | c35717a | 2007-07-13 17:10:38 +0000 | [diff] [blame] | 708 | llvm::SmallVector<char, 64> IdentifierBuffer; |
| 709 | IdentifierBuffer.resize(Identifier.getLength()); |
| 710 | const char *TmpBuf = &IdentifierBuffer[0]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 711 | unsigned Size = getSpelling(Identifier, TmpBuf); |
| 712 | II = getIdentifierInfo(TmpBuf, TmpBuf+Size); |
| 713 | } |
| 714 | Identifier.setIdentifierInfo(II); |
| 715 | return II; |
| 716 | } |
| 717 | |
| 718 | |
| 719 | /// HandleIdentifier - This callback is invoked when the lexer reads an |
| 720 | /// identifier. This callback looks up the identifier in the map and/or |
| 721 | /// potentially macro expands it or turns it into a named token (like 'for'). |
Chris Lattner | 6a170eb | 2009-01-21 07:43:11 +0000 | [diff] [blame] | 722 | /// |
| 723 | /// Note that callers of this method are guarded by checking the |
| 724 | /// IdentifierInfo's 'isHandleIdentifierCase' bit. If this method changes, the |
| 725 | /// IdentifierInfo methods that compute these properties will need to change to |
| 726 | /// match. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 727 | void Preprocessor::HandleIdentifier(Token &Identifier) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 728 | assert(Identifier.getIdentifierInfo() && |
| 729 | "Can't handle identifiers without identifier info!"); |
| 730 | |
| 731 | IdentifierInfo &II = *Identifier.getIdentifierInfo(); |
| 732 | |
| 733 | // If this identifier was poisoned, and if it was not produced from a macro |
| 734 | // expansion, emit an error. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 735 | if (II.isPoisoned() && CurPPLexer) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 736 | if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning. |
| 737 | Diag(Identifier, diag::err_pp_used_poisoned_id); |
| 738 | else |
| 739 | Diag(Identifier, diag::ext_pp_bad_vaargs_use); |
| 740 | } |
| 741 | |
| 742 | // If this is a macro to be expanded, do it. |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 743 | if (MacroInfo *MI = getMacroInfo(&II)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 744 | if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) { |
| 745 | if (MI->isEnabled()) { |
| 746 | if (!HandleMacroExpandedIdentifier(Identifier, MI)) |
| 747 | return; |
| 748 | } else { |
| 749 | // C99 6.10.3.4p2 says that a disabled macro may never again be |
| 750 | // expanded, even if it's in a context where it could be expanded in the |
| 751 | // future. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 752 | Identifier.setFlag(Token::DisableExpand); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 753 | } |
| 754 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | // C++ 2.11p2: If this is an alternative representation of a C++ operator, |
| 758 | // then we act as if it is the actual operator and not the textual |
| 759 | // representation of it. |
| 760 | if (II.isCPlusPlusOperatorKeyword()) |
| 761 | Identifier.setIdentifierInfo(0); |
| 762 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 763 | // If this is an extension token, diagnose its use. |
Steve Naroff | b4eaf9c | 2008-09-02 18:50:17 +0000 | [diff] [blame] | 764 | // We avoid diagnosing tokens that originate from macro definitions. |
| 765 | if (II.isExtensionToken() && Features.C99 && !DisableMacroExpansion) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 766 | Diag(Identifier, diag::ext_token_used); |
| 767 | } |