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