Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1 | //===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Preprocessor interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | // |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 14 | // Options to support: |
| 15 | // -H - Print the name of each header file used. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 07b019a | 2006-10-22 07:28:56 +0000 | [diff] [blame] | 29 | #include "clang/Lex/HeaderSearch.h" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 30 | #include "clang/Lex/MacroInfo.h" |
Chris Lattner | b8d6d5a | 2006-11-21 04:09:30 +0000 | [diff] [blame] | 31 | #include "clang/Lex/PPCallbacks.h" |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 32 | #include "clang/Lex/Pragma.h" |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 33 | #include "clang/Lex/ScratchBuffer.h" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 34 | #include "clang/Basic/Diagnostic.h" |
| 35 | #include "clang/Basic/FileManager.h" |
| 36 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 81278c6 | 2006-10-14 19:03:49 +0000 | [diff] [blame] | 37 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 7a4af3b | 2006-07-26 06:26:52 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 8a7003c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 39 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 40 | #include <iostream> |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 41 | using namespace clang; |
| 42 | |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
Chris Lattner | 02dffbd | 2006-10-14 07:50:21 +0000 | [diff] [blame] | 45 | Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts, |
Chris Lattner | ad7cdd3 | 2006-11-21 06:08:20 +0000 | [diff] [blame] | 46 | TargetInfo &target, SourceManager &SM, |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 47 | HeaderSearch &Headers) |
Chris Lattner | ad7cdd3 | 2006-11-21 06:08:20 +0000 | [diff] [blame] | 48 | : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()), |
| 49 | SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts), |
Chris Lattner | b8d6d5a | 2006-11-21 04:09:30 +0000 | [diff] [blame] | 50 | CurLexer(0), CurDirLookup(0), CurMacroExpander(0), Callbacks(0) { |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 51 | ScratchBuf = new ScratchBuffer(SourceMgr); |
Chris Lattner | c02c4ab | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 53 | // Clear stats. |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 54 | NumDirectives = NumDefined = NumUndefined = NumPragma = 0; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 55 | NumIf = NumElse = NumEndif = 0; |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 56 | NumEnteredSourceFiles = 0; |
| 57 | NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0; |
Chris Lattner | 510ab61 | 2006-07-20 04:47:30 +0000 | [diff] [blame] | 58 | NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 59 | MaxIncludeStackDepth = 0; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 60 | NumSkipped = 0; |
Chris Lattner | b352e3e | 2006-11-21 06:17:10 +0000 | [diff] [blame] | 61 | |
| 62 | // Default to discarding comments. |
| 63 | KeepComments = false; |
| 64 | KeepMacroComments = false; |
| 65 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 66 | // Macro expansion is enabled. |
| 67 | DisableMacroExpansion = false; |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 68 | InMacroArgs = false; |
Chris Lattner | c02c4ab | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 69 | NumCachedMacroExpanders = 0; |
Chris Lattner | 0c885f5 | 2006-06-21 06:50:18 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 8ff7199 | 2006-07-06 05:17:39 +0000 | [diff] [blame] | 71 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 72 | // This gets unpoisoned where it is allowed. |
| 73 | (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned(); |
| 74 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 75 | // Initialize the pragma handlers. |
| 76 | PragmaHandlers = new PragmaNamespace(0); |
| 77 | RegisterBuiltinPragmas(); |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 78 | |
| 79 | // Initialize builtin macros like __LINE__ and friends. |
| 80 | RegisterBuiltinMacros(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | Preprocessor::~Preprocessor() { |
| 84 | // Free any active lexers. |
| 85 | delete CurLexer; |
| 86 | |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 87 | while (!IncludeMacroStack.empty()) { |
| 88 | delete IncludeMacroStack.back().TheLexer; |
| 89 | delete IncludeMacroStack.back().TheMacroExpander; |
| 90 | IncludeMacroStack.pop_back(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 91 | } |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 92 | |
Chris Lattner | c02c4ab | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 93 | // Free any cached macro expanders. |
| 94 | for (unsigned i = 0, e = NumCachedMacroExpanders; i != e; ++i) |
| 95 | delete MacroExpanderCache[i]; |
| 96 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 97 | // Release pragma information. |
| 98 | delete PragmaHandlers; |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 99 | |
| 100 | // Delete the scratch buffer info. |
| 101 | delete ScratchBuf; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Chris Lattner | b8d6d5a | 2006-11-21 04:09:30 +0000 | [diff] [blame] | 104 | PPCallbacks::~PPCallbacks() { |
| 105 | } |
Chris Lattner | 87d3bec | 2006-10-17 03:44:32 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 107 | /// Diag - Forwarding function for diagnostics. This emits a diagnostic at |
| 108 | /// the specified LexerToken's location, translating the token's start |
| 109 | /// position in the current buffer into a SourcePosition object for rendering. |
Chris Lattner | 36982e4 | 2007-05-16 17:49:37 +0000 | [diff] [blame] | 110 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID) { |
| 111 | Diags.Report(Loc, DiagID); |
| 112 | } |
| 113 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 114 | void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 115 | const std::string &Msg) { |
Chris Lattner | 36982e4 | 2007-05-16 17:49:37 +0000 | [diff] [blame] | 116 | Diags.Report(Loc, DiagID, &Msg, 1); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 117 | } |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 118 | |
| 119 | void Preprocessor::DumpToken(const LexerToken &Tok, bool DumpFlags) const { |
| 120 | std::cerr << tok::getTokenName(Tok.getKind()) << " '" |
| 121 | << getSpelling(Tok) << "'"; |
| 122 | |
| 123 | if (!DumpFlags) return; |
| 124 | std::cerr << "\t"; |
| 125 | if (Tok.isAtStartOfLine()) |
| 126 | std::cerr << " [StartOfLine]"; |
| 127 | if (Tok.hasLeadingSpace()) |
| 128 | std::cerr << " [LeadingSpace]"; |
Chris Lattner | 6e4bf52 | 2006-07-27 06:59:25 +0000 | [diff] [blame] | 129 | if (Tok.isExpandDisabled()) |
| 130 | std::cerr << " [ExpandDisabled]"; |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 131 | if (Tok.needsCleaning()) { |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 132 | const char *Start = SourceMgr.getCharacterData(Tok.getLocation()); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 133 | std::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength()) |
| 134 | << "']"; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void Preprocessor::DumpMacro(const MacroInfo &MI) const { |
| 139 | std::cerr << "MACRO: "; |
| 140 | for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) { |
| 141 | DumpToken(MI.getReplacementToken(i)); |
| 142 | std::cerr << " "; |
| 143 | } |
| 144 | std::cerr << "\n"; |
| 145 | } |
| 146 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 147 | void Preprocessor::PrintStats() { |
| 148 | std::cerr << "\n*** Preprocessor Stats:\n"; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 149 | std::cerr << NumDirectives << " directives found:\n"; |
| 150 | std::cerr << " " << NumDefined << " #define.\n"; |
| 151 | std::cerr << " " << NumUndefined << " #undef.\n"; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 152 | std::cerr << " #include/#include_next/#import:\n"; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 153 | std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n"; |
| 154 | std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n"; |
| 155 | std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n"; |
| 156 | std::cerr << " " << NumElse << " #else/#elif.\n"; |
| 157 | std::cerr << " " << NumEndif << " #endif.\n"; |
| 158 | std::cerr << " " << NumPragma << " #pragma.\n"; |
| 159 | std::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n"; |
| 160 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 161 | std::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/" |
| 162 | << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, " |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 163 | << NumFastMacroExpanded << " on the fast path.\n"; |
Chris Lattner | 510ab61 | 2006-07-20 04:47:30 +0000 | [diff] [blame] | 164 | std::cerr << (NumFastTokenPaste+NumTokenPaste) |
| 165 | << " token paste (##) operations performed, " |
| 166 | << NumFastTokenPaste << " on the fast path.\n"; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | //===----------------------------------------------------------------------===// |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 170 | // Token Spelling |
| 171 | //===----------------------------------------------------------------------===// |
| 172 | |
| 173 | |
| 174 | /// getSpelling() - Return the 'spelling' of this token. The spelling of a |
| 175 | /// token are the characters used to represent the token in the source file |
| 176 | /// after trigraph expansion and escaped-newline folding. In particular, this |
| 177 | /// wants to get the true, uncanonicalized, spelling of things like digraphs |
| 178 | /// UCNs, etc. |
| 179 | std::string Preprocessor::getSpelling(const LexerToken &Tok) const { |
| 180 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
| 181 | |
| 182 | // If this token contains nothing interesting, return it directly. |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 183 | const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 184 | if (!Tok.needsCleaning()) |
| 185 | return std::string(TokStart, TokStart+Tok.getLength()); |
| 186 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 187 | std::string Result; |
| 188 | Result.reserve(Tok.getLength()); |
| 189 | |
Chris Lattner | ef9eae1 | 2006-07-04 22:33:12 +0000 | [diff] [blame] | 190 | // Otherwise, hard case, relex the characters into the string. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 191 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 192 | Ptr != End; ) { |
| 193 | unsigned CharSize; |
| 194 | Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features)); |
| 195 | Ptr += CharSize; |
| 196 | } |
| 197 | assert(Result.size() != unsigned(Tok.getLength()) && |
| 198 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 199 | return Result; |
| 200 | } |
| 201 | |
| 202 | /// getSpelling - This method is used to get the spelling of a token into a |
| 203 | /// preallocated buffer, instead of as an std::string. The caller is required |
| 204 | /// to allocate enough space for the token, which is guaranteed to be at least |
| 205 | /// Tok.getLength() bytes long. The actual length of the token is returned. |
Chris Lattner | ef9eae1 | 2006-07-04 22:33:12 +0000 | [diff] [blame] | 206 | /// |
| 207 | /// Note that this method may do two possible things: it may either fill in |
| 208 | /// the buffer specified with characters, or it may *change the input pointer* |
| 209 | /// to point to a constant buffer with the data already in it (avoiding a |
| 210 | /// copy). The caller is not allowed to modify the returned buffer pointer |
| 211 | /// if an internal buffer is returned. |
| 212 | unsigned Preprocessor::getSpelling(const LexerToken &Tok, |
| 213 | const char *&Buffer) const { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 214 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
| 215 | |
Chris Lattner | d3a15f7 | 2006-07-04 23:01:03 +0000 | [diff] [blame] | 216 | // If this token is an identifier, just return the string from the identifier |
| 217 | // table, which is very quick. |
| 218 | if (const IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 219 | Buffer = II->getName(); |
| 220 | return Tok.getLength(); |
| 221 | } |
| 222 | |
| 223 | // Otherwise, compute the start of the token in the input lexer buffer. |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 224 | const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation()); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 225 | |
| 226 | // If this token contains nothing interesting, return it directly. |
| 227 | if (!Tok.needsCleaning()) { |
Chris Lattner | ef9eae1 | 2006-07-04 22:33:12 +0000 | [diff] [blame] | 228 | Buffer = TokStart; |
| 229 | return Tok.getLength(); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 230 | } |
| 231 | // Otherwise, hard case, relex the characters into the string. |
Chris Lattner | ef9eae1 | 2006-07-04 22:33:12 +0000 | [diff] [blame] | 232 | char *OutBuf = const_cast<char*>(Buffer); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 233 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 234 | Ptr != End; ) { |
| 235 | unsigned CharSize; |
| 236 | *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features); |
| 237 | Ptr += CharSize; |
| 238 | } |
| 239 | assert(unsigned(OutBuf-Buffer) != Tok.getLength() && |
| 240 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 241 | |
| 242 | return OutBuf-Buffer; |
| 243 | } |
| 244 | |
Chris Lattner | b94ec7b | 2006-07-14 06:54:10 +0000 | [diff] [blame] | 245 | |
| 246 | /// CreateString - Plop the specified string into a scratch buffer and return a |
| 247 | /// location for it. If specified, the source location provides a source |
| 248 | /// location for the token. |
| 249 | SourceLocation Preprocessor:: |
| 250 | CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) { |
| 251 | if (SLoc.isValid()) |
| 252 | return ScratchBuf->getToken(Buf, Len, SLoc); |
| 253 | return ScratchBuf->getToken(Buf, Len); |
| 254 | } |
| 255 | |
| 256 | |
Chris Lattner | 8a7003c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 257 | /// AdvanceToTokenCharacter - Given a location that specifies the start of a |
| 258 | /// token, return a new location that specifies a character within the token. |
| 259 | SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart, |
| 260 | unsigned CharNo) { |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 261 | // If they request the first char of the token, we're trivially done. If this |
| 262 | // is a macro expansion, it doesn't make sense to point to a character within |
| 263 | // the instantiation point (the name). We could point to the source |
| 264 | // character, but without also pointing to instantiation info, this is |
| 265 | // confusing. |
| 266 | if (CharNo == 0 || TokStart.isMacroID()) return TokStart; |
Chris Lattner | 8a7003c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 267 | |
| 268 | // Figure out how many physical characters away the specified logical |
| 269 | // character is. This needs to take into consideration newlines and |
| 270 | // trigraphs. |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 271 | const char *TokPtr = SourceMgr.getCharacterData(TokStart); |
| 272 | unsigned PhysOffset = 0; |
Chris Lattner | 8a7003c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 273 | |
| 274 | // The usual case is that tokens don't contain anything interesting. Skip |
| 275 | // over the uninteresting characters. If a token only consists of simple |
| 276 | // chars, this method is extremely fast. |
| 277 | while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr)) |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 278 | ++TokPtr, --CharNo, ++PhysOffset; |
Chris Lattner | 8a7003c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 279 | |
| 280 | // If we have a character that may be a trigraph or escaped newline, create a |
| 281 | // lexer to parse it correctly. |
Chris Lattner | 8a7003c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 282 | if (CharNo != 0) { |
| 283 | // Create a lexer starting at this token position. |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 284 | const llvm::MemoryBuffer *SrcBuf =SourceMgr.getBuffer(TokStart.getFileID()); |
| 285 | Lexer TheLexer(SrcBuf, TokStart, *this, TokPtr); |
Chris Lattner | 8a7003c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 286 | LexerToken Tok; |
| 287 | // Skip over characters the remaining characters. |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 288 | const char *TokStartPtr = TokPtr; |
Chris Lattner | 8a7003c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 289 | for (; CharNo; --CharNo) |
| 290 | TheLexer.getAndAdvanceChar(TokPtr, Tok); |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 291 | |
| 292 | PhysOffset += TokPtr-TokStartPtr; |
Chris Lattner | 8a7003c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 293 | } |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 294 | |
| 295 | return TokStart.getFileLocWithOffset(PhysOffset); |
Chris Lattner | 8a7003c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | |
| 299 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 300 | //===----------------------------------------------------------------------===// |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 301 | // Source File Location Methods. |
| 302 | //===----------------------------------------------------------------------===// |
| 303 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 304 | /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, |
| 305 | /// return null on failure. isAngled indicates whether the file reference is |
| 306 | /// for system #include's or not (i.e. using <> instead of ""). |
Chris Lattner | b8b94f1 | 2006-10-30 05:38:06 +0000 | [diff] [blame] | 307 | const FileEntry *Preprocessor::LookupFile(const char *FilenameStart, |
| 308 | const char *FilenameEnd, |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 309 | bool isAngled, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 310 | const DirectoryLookup *FromDir, |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 311 | const DirectoryLookup *&CurDir) { |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 312 | // If the header lookup mechanism may be relative to the current file, pass in |
| 313 | // info about where the current file is. |
| 314 | const FileEntry *CurFileEnt = 0; |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 315 | if (!FromDir) { |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 316 | SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc(); |
| 317 | CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 320 | // Do a standard file entry lookup. |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 321 | CurDir = CurDirLookup; |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 322 | const FileEntry *FE = |
Chris Lattner | 7cdbad9 | 2006-10-30 05:33:15 +0000 | [diff] [blame] | 323 | HeaderInfo.LookupFile(FilenameStart, FilenameEnd, |
| 324 | isAngled, FromDir, CurDir, CurFileEnt); |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 325 | if (FE) return FE; |
| 326 | |
| 327 | // Otherwise, see if this is a subframework header. If so, this is relative |
| 328 | // to one of the headers on the #include stack. Walk the list of the current |
| 329 | // headers on the #include stack and pass them to HeaderInfo. |
Chris Lattner | 5c683b2 | 2006-10-20 05:12:14 +0000 | [diff] [blame] | 330 | if (CurLexer && !CurLexer->Is_PragmaLexer) { |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 331 | CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()); |
Chris Lattner | 7cdbad9 | 2006-10-30 05:33:15 +0000 | [diff] [blame] | 332 | if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd, |
| 333 | CurFileEnt))) |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 334 | return FE; |
| 335 | } |
| 336 | |
| 337 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 338 | IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1]; |
Chris Lattner | 5c683b2 | 2006-10-20 05:12:14 +0000 | [diff] [blame] | 339 | if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) { |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 340 | CurFileEnt = SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc()); |
Chris Lattner | 7cdbad9 | 2006-10-30 05:33:15 +0000 | [diff] [blame] | 341 | if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd, |
| 342 | CurFileEnt))) |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 343 | return FE; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // Otherwise, we really couldn't find the file. |
| 348 | return 0; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Chris Lattner | ecfeafe | 2006-07-02 21:26:45 +0000 | [diff] [blame] | 351 | /// isInPrimaryFile - Return true if we're in the top-level file, not in a |
| 352 | /// #include. |
| 353 | bool Preprocessor::isInPrimaryFile() const { |
Chris Lattner | ecfeafe | 2006-07-02 21:26:45 +0000 | [diff] [blame] | 354 | if (CurLexer && !CurLexer->Is_PragmaLexer) |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 355 | return CurLexer->isMainFile(); |
Chris Lattner | ecfeafe | 2006-07-02 21:26:45 +0000 | [diff] [blame] | 356 | |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 357 | // If there are any stacked lexers, we're in a #include. |
Chris Lattner | ecfeafe | 2006-07-02 21:26:45 +0000 | [diff] [blame] | 358 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 359 | if (IncludeMacroStack[i].TheLexer && |
| 360 | !IncludeMacroStack[i].TheLexer->Is_PragmaLexer) |
| 361 | return IncludeMacroStack[i].TheLexer->isMainFile(); |
| 362 | return false; |
Chris Lattner | ecfeafe | 2006-07-02 21:26:45 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /// getCurrentLexer - Return the current file lexer being lexed from. Note |
| 366 | /// that this ignores any potentially active macro expansions and _Pragma |
| 367 | /// expansions going on at the time. |
| 368 | Lexer *Preprocessor::getCurrentFileLexer() const { |
| 369 | if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer; |
| 370 | |
| 371 | // Look for a stacked lexer. |
| 372 | for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { |
Chris Lattner | f88c53a | 2006-07-03 05:26:05 +0000 | [diff] [blame] | 373 | Lexer *L = IncludeMacroStack[i-1].TheLexer; |
Chris Lattner | ecfeafe | 2006-07-02 21:26:45 +0000 | [diff] [blame] | 374 | if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions. |
| 375 | return L; |
| 376 | } |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 381 | /// EnterSourceFile - Add a source file to the top of the include stack and |
| 382 | /// start lexing tokens from it instead of the current buffer. Return true |
| 383 | /// on failure. |
| 384 | void Preprocessor::EnterSourceFile(unsigned FileID, |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 385 | const DirectoryLookup *CurDir, |
| 386 | bool isMainFile) { |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 387 | assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 388 | ++NumEnteredSourceFiles; |
| 389 | |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 390 | if (MaxIncludeStackDepth < IncludeMacroStack.size()) |
| 391 | MaxIncludeStackDepth = IncludeMacroStack.size(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 392 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 393 | const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(FileID); |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 394 | Lexer *TheLexer = new Lexer(Buffer, SourceLocation::getFileLoc(FileID, 0), |
| 395 | *this); |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 396 | if (isMainFile) TheLexer->setIsMainFile(); |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 397 | EnterSourceFileWithLexer(TheLexer, CurDir); |
| 398 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 399 | |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 400 | /// EnterSourceFile - Add a source file to the top of the include stack and |
| 401 | /// start lexing tokens from it instead of the current buffer. |
| 402 | void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer, |
| 403 | const DirectoryLookup *CurDir) { |
| 404 | |
| 405 | // Add the current lexer to the include stack. |
| 406 | if (CurLexer || CurMacroExpander) |
| 407 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
| 408 | CurMacroExpander)); |
| 409 | |
| 410 | CurLexer = TheLexer; |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 411 | CurDirLookup = CurDir; |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 412 | CurMacroExpander = 0; |
Chris Lattner | 0c885f5 | 2006-06-21 06:50:18 +0000 | [diff] [blame] | 413 | |
| 414 | // Notify the client, if desired, that we are in a new source file. |
Chris Lattner | b8d6d5a | 2006-11-21 04:09:30 +0000 | [diff] [blame] | 415 | if (Callbacks && !CurLexer->Is_PragmaLexer) { |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 416 | DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir; |
| 417 | |
| 418 | // Get the file entry for the current file. |
| 419 | if (const FileEntry *FE = |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 420 | SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())) |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 421 | FileType = HeaderInfo.getFileDirFlavor(FE); |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 422 | |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 423 | Callbacks->FileChanged(CurLexer->getFileLoc(), |
Chris Lattner | b8d6d5a | 2006-11-21 04:09:30 +0000 | [diff] [blame] | 424 | PPCallbacks::EnterFile, FileType); |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 425 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 428 | |
| 429 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 430 | /// EnterMacro - Add a Macro to the top of the include stack and start lexing |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 431 | /// tokens from it instead of the current buffer. |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 432 | void Preprocessor::EnterMacro(LexerToken &Tok, MacroArgs *Args) { |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 433 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
| 434 | CurMacroExpander)); |
| 435 | CurLexer = 0; |
| 436 | CurDirLookup = 0; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 437 | |
Chris Lattner | c02c4ab | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 438 | if (NumCachedMacroExpanders == 0) { |
| 439 | CurMacroExpander = new MacroExpander(Tok, Args, *this); |
| 440 | } else { |
| 441 | CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders]; |
| 442 | CurMacroExpander->Init(Tok, Args); |
| 443 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 446 | /// EnterTokenStream - Add a "macro" context to the top of the include stack, |
| 447 | /// which will cause the lexer to start returning the specified tokens. Note |
| 448 | /// that these tokens will be re-macro-expanded when/if expansion is enabled. |
| 449 | /// This method assumes that the specified stream of tokens has a permanent |
| 450 | /// owner somewhere, so they do not need to be copied. |
Chris Lattner | 7021657 | 2006-07-26 03:50:40 +0000 | [diff] [blame] | 451 | void Preprocessor::EnterTokenStream(const LexerToken *Toks, unsigned NumToks) { |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 452 | // Save our current state. |
| 453 | IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup, |
| 454 | CurMacroExpander)); |
| 455 | CurLexer = 0; |
| 456 | CurDirLookup = 0; |
| 457 | |
| 458 | // Create a macro expander to expand from the specified token stream. |
Chris Lattner | c02c4ab | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 459 | if (NumCachedMacroExpanders == 0) { |
| 460 | CurMacroExpander = new MacroExpander(Toks, NumToks, *this); |
| 461 | } else { |
| 462 | CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders]; |
| 463 | CurMacroExpander->Init(Toks, NumToks); |
| 464 | } |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the |
| 468 | /// lexer stack. This should only be used in situations where the current |
| 469 | /// state of the top-of-stack lexer is known. |
| 470 | void Preprocessor::RemoveTopOfLexerStack() { |
| 471 | assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load"); |
Chris Lattner | c02c4ab | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 472 | |
| 473 | if (CurMacroExpander) { |
| 474 | // Delete or cache the now-dead macro expander. |
| 475 | if (NumCachedMacroExpanders == MacroExpanderCacheSize) |
| 476 | delete CurMacroExpander; |
| 477 | else |
| 478 | MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander; |
| 479 | } else { |
| 480 | delete CurLexer; |
| 481 | } |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 482 | CurLexer = IncludeMacroStack.back().TheLexer; |
| 483 | CurDirLookup = IncludeMacroStack.back().TheDirLookup; |
| 484 | CurMacroExpander = IncludeMacroStack.back().TheMacroExpander; |
| 485 | IncludeMacroStack.pop_back(); |
| 486 | } |
| 487 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 488 | //===----------------------------------------------------------------------===// |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 489 | // Macro Expansion Handling. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 490 | //===----------------------------------------------------------------------===// |
| 491 | |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 492 | /// RegisterBuiltinMacro - Register the specified identifier in the identifier |
| 493 | /// table and mark it as a builtin macro to be expanded. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 494 | IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) { |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 495 | // Get the identifier. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 496 | IdentifierInfo *Id = getIdentifierInfo(Name); |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 497 | |
| 498 | // Mark it as being a macro that is builtin. |
| 499 | MacroInfo *MI = new MacroInfo(SourceLocation()); |
| 500 | MI->setIsBuiltinMacro(); |
| 501 | Id->setMacroInfo(MI); |
| 502 | return Id; |
| 503 | } |
| 504 | |
| 505 | |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 506 | /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the |
| 507 | /// identifier table. |
| 508 | void Preprocessor::RegisterBuiltinMacros() { |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 509 | Ident__LINE__ = RegisterBuiltinMacro("__LINE__"); |
Chris Lattner | 630b33c | 2006-07-01 22:46:53 +0000 | [diff] [blame] | 510 | Ident__FILE__ = RegisterBuiltinMacro("__FILE__"); |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 511 | Ident__DATE__ = RegisterBuiltinMacro("__DATE__"); |
| 512 | Ident__TIME__ = RegisterBuiltinMacro("__TIME__"); |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 513 | Ident_Pragma = RegisterBuiltinMacro("_Pragma"); |
Chris Lattner | c1283b9 | 2006-07-01 23:16:30 +0000 | [diff] [blame] | 514 | |
| 515 | // GCC Extensions. |
| 516 | Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__"); |
| 517 | Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__"); |
Chris Lattner | 847e0e4 | 2006-07-01 23:49:16 +0000 | [diff] [blame] | 518 | Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Chris Lattner | c239583 | 2006-07-09 00:57:04 +0000 | [diff] [blame] | 521 | /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token |
| 522 | /// in its expansion, currently expands to that token literally. |
Chris Lattner | 3ce1d1a | 2006-07-09 01:00:18 +0000 | [diff] [blame] | 523 | static bool isTrivialSingleTokenExpansion(const MacroInfo *MI, |
| 524 | const IdentifierInfo *MacroIdent) { |
Chris Lattner | c239583 | 2006-07-09 00:57:04 +0000 | [diff] [blame] | 525 | IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo(); |
| 526 | |
| 527 | // If the token isn't an identifier, it's always literally expanded. |
| 528 | if (II == 0) return true; |
| 529 | |
| 530 | // If the identifier is a macro, and if that macro is enabled, it may be |
| 531 | // expanded so it's not a trivial expansion. |
Chris Lattner | 3ce1d1a | 2006-07-09 01:00:18 +0000 | [diff] [blame] | 532 | if (II->getMacroInfo() && II->getMacroInfo()->isEnabled() && |
| 533 | // Fast expanding "#define X X" is ok, because X would be disabled. |
| 534 | II != MacroIdent) |
Chris Lattner | c239583 | 2006-07-09 00:57:04 +0000 | [diff] [blame] | 535 | return false; |
| 536 | |
| 537 | // If this is an object-like macro invocation, it is safe to trivially expand |
| 538 | // it. |
| 539 | if (MI->isObjectLike()) return true; |
| 540 | |
| 541 | // If this is a function-like macro invocation, it's safe to trivially expand |
| 542 | // as long as the identifier is not a macro argument. |
| 543 | for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end(); |
| 544 | I != E; ++I) |
| 545 | if (*I == II) |
| 546 | return false; // Identifier is a macro argument. |
Chris Lattner | 273ddd5 | 2006-07-29 07:33:01 +0000 | [diff] [blame] | 547 | |
Chris Lattner | c239583 | 2006-07-09 00:57:04 +0000 | [diff] [blame] | 548 | return true; |
Chris Lattner | d8aee0e | 2006-07-11 05:04:55 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Chris Lattner | c239583 | 2006-07-09 00:57:04 +0000 | [diff] [blame] | 551 | |
Chris Lattner | afe603f | 2006-07-11 04:02:46 +0000 | [diff] [blame] | 552 | /// isNextPPTokenLParen - Determine whether the next preprocessor token to be |
| 553 | /// lexed is a '('. If so, consume the token and return true, if not, this |
| 554 | /// method should have no observable side-effect on the lexed tokens. |
| 555 | bool Preprocessor::isNextPPTokenLParen() { |
Chris Lattner | afe603f | 2006-07-11 04:02:46 +0000 | [diff] [blame] | 556 | // Do some quick tests for rejection cases. |
Chris Lattner | d8aee0e | 2006-07-11 05:04:55 +0000 | [diff] [blame] | 557 | unsigned Val; |
| 558 | if (CurLexer) |
Chris Lattner | 678c880 | 2006-07-11 05:46:12 +0000 | [diff] [blame] | 559 | Val = CurLexer->isNextPPTokenLParen(); |
Chris Lattner | d8aee0e | 2006-07-11 05:04:55 +0000 | [diff] [blame] | 560 | else |
| 561 | Val = CurMacroExpander->isNextTokenLParen(); |
| 562 | |
| 563 | if (Val == 2) { |
Chris Lattner | 5c98379 | 2007-07-19 00:07:36 +0000 | [diff] [blame] | 564 | // We have run off the end. If it's a source file we don't |
| 565 | // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the |
| 566 | // macro stack. |
| 567 | if (CurLexer) |
| 568 | return false; |
| 569 | for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { |
Chris Lattner | d8aee0e | 2006-07-11 05:04:55 +0000 | [diff] [blame] | 570 | IncludeStackInfo &Entry = IncludeMacroStack[i-1]; |
| 571 | if (Entry.TheLexer) |
Chris Lattner | 678c880 | 2006-07-11 05:46:12 +0000 | [diff] [blame] | 572 | Val = Entry.TheLexer->isNextPPTokenLParen(); |
Chris Lattner | d8aee0e | 2006-07-11 05:04:55 +0000 | [diff] [blame] | 573 | else |
| 574 | Val = Entry.TheMacroExpander->isNextTokenLParen(); |
Chris Lattner | 5c98379 | 2007-07-19 00:07:36 +0000 | [diff] [blame] | 575 | |
| 576 | if (Val != 2) |
| 577 | break; |
| 578 | |
| 579 | // Ran off the end of a source file? |
| 580 | if (Entry.TheLexer) |
| 581 | return false; |
Chris Lattner | d8aee0e | 2006-07-11 05:04:55 +0000 | [diff] [blame] | 582 | } |
Chris Lattner | afe603f | 2006-07-11 04:02:46 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Chris Lattner | d8aee0e | 2006-07-11 05:04:55 +0000 | [diff] [blame] | 585 | // Okay, if we know that the token is a '(', lex it and return. Otherwise we |
| 586 | // have found something that isn't a '(' or we found the end of the |
| 587 | // translation unit. In either case, return false. |
| 588 | if (Val != 1) |
| 589 | return false; |
Chris Lattner | afe603f | 2006-07-11 04:02:46 +0000 | [diff] [blame] | 590 | |
| 591 | LexerToken Tok; |
| 592 | LexUnexpandedToken(Tok); |
Chris Lattner | d8aee0e | 2006-07-11 05:04:55 +0000 | [diff] [blame] | 593 | assert(Tok.getKind() == tok::l_paren && "Error computing l-paren-ness?"); |
| 594 | return true; |
Chris Lattner | afe603f | 2006-07-11 04:02:46 +0000 | [diff] [blame] | 595 | } |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 596 | |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 597 | /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be |
| 598 | /// expanded as a macro, handle it and return the next token as 'Identifier'. |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 599 | bool Preprocessor::HandleMacroExpandedIdentifier(LexerToken &Identifier, |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 600 | MacroInfo *MI) { |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 601 | |
| 602 | // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. |
| 603 | if (MI->isBuiltinMacro()) { |
| 604 | ExpandBuiltinMacro(Identifier); |
| 605 | return false; |
| 606 | } |
| 607 | |
Chris Lattner | 81278c6 | 2006-10-14 19:03:49 +0000 | [diff] [blame] | 608 | // If this is the first use of a target-specific macro, warn about it. |
| 609 | if (MI->isTargetSpecific()) { |
| 610 | MI->setIsTargetSpecific(false); // Don't warn on second use. |
| 611 | getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(), |
| 612 | diag::port_target_macro_use); |
| 613 | } |
| 614 | |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 615 | /// Args - If this is a function-like macro expansion, this contains, |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 616 | /// for each macro argument, the list of tokens that were provided to the |
| 617 | /// invocation. |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 618 | MacroArgs *Args = 0; |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 619 | |
| 620 | // If this is a function-like macro, read the arguments. |
| 621 | if (MI->isFunctionLike()) { |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 622 | // C99 6.10.3p10: If the preprocessing token immediately after the the macro |
Chris Lattner | 24dbee7 | 2007-07-19 16:11:58 +0000 | [diff] [blame] | 623 | // name isn't a '(', this macro should not be expanded. Otherwise, consume |
| 624 | // it. |
Chris Lattner | afe603f | 2006-07-11 04:02:46 +0000 | [diff] [blame] | 625 | if (!isNextPPTokenLParen()) |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 626 | return true; |
| 627 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 628 | // Remember that we are now parsing the arguments to a macro invocation. |
| 629 | // Preprocessor directives used inside macro arguments are not portable, and |
| 630 | // this enables the warning. |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 631 | InMacroArgs = true; |
| 632 | Args = ReadFunctionLikeMacroArgs(Identifier, MI); |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 633 | |
| 634 | // Finished parsing args. |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 635 | InMacroArgs = false; |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 636 | |
| 637 | // If there was an error parsing the arguments, bail out. |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 638 | if (Args == 0) return false; |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 639 | |
| 640 | ++NumFnMacroExpanded; |
| 641 | } else { |
| 642 | ++NumMacroExpanded; |
| 643 | } |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 644 | |
| 645 | // Notice that this macro has been used. |
| 646 | MI->setIsUsed(true); |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 647 | |
| 648 | // If we started lexing a macro, enter the macro expansion body. |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 649 | |
| 650 | // If this macro expands to no tokens, don't bother to push it onto the |
| 651 | // expansion stack, only to take it right back off. |
| 652 | if (MI->getNumTokens() == 0) { |
Chris Lattner | 2ada5d3 | 2006-07-15 07:51:24 +0000 | [diff] [blame] | 653 | // No need for arg info. |
Chris Lattner | c1410dc | 2006-07-26 05:22:49 +0000 | [diff] [blame] | 654 | if (Args) Args->destroy(); |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 655 | |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 656 | // Ignore this macro use, just return the next token in the current |
| 657 | // buffer. |
| 658 | bool HadLeadingSpace = Identifier.hasLeadingSpace(); |
| 659 | bool IsAtStartOfLine = Identifier.isAtStartOfLine(); |
| 660 | |
| 661 | Lex(Identifier); |
| 662 | |
| 663 | // If the identifier isn't on some OTHER line, inherit the leading |
| 664 | // whitespace/first-on-a-line property of this token. This handles |
| 665 | // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is |
| 666 | // empty. |
| 667 | if (!Identifier.isAtStartOfLine()) { |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 668 | if (IsAtStartOfLine) Identifier.setFlag(LexerToken::StartOfLine); |
| 669 | if (HadLeadingSpace) Identifier.setFlag(LexerToken::LeadingSpace); |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 670 | } |
| 671 | ++NumFastMacroExpanded; |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 672 | return false; |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 673 | |
Chris Lattner | 3ce1d1a | 2006-07-09 01:00:18 +0000 | [diff] [blame] | 674 | } else if (MI->getNumTokens() == 1 && |
| 675 | isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo())){ |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 676 | // Otherwise, if this macro expands into a single trivially-expanded |
| 677 | // token: expand it now. This handles common cases like |
| 678 | // "#define VAL 42". |
| 679 | |
| 680 | // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro |
| 681 | // identifier to the expanded token. |
| 682 | bool isAtStartOfLine = Identifier.isAtStartOfLine(); |
| 683 | bool hasLeadingSpace = Identifier.hasLeadingSpace(); |
| 684 | |
| 685 | // Remember where the token is instantiated. |
| 686 | SourceLocation InstantiateLoc = Identifier.getLocation(); |
| 687 | |
| 688 | // Replace the result token. |
| 689 | Identifier = MI->getReplacementToken(0); |
| 690 | |
| 691 | // Restore the StartOfLine/LeadingSpace markers. |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 692 | Identifier.setFlagValue(LexerToken::StartOfLine , isAtStartOfLine); |
| 693 | Identifier.setFlagValue(LexerToken::LeadingSpace, hasLeadingSpace); |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 694 | |
| 695 | // Update the tokens location to include both its logical and physical |
| 696 | // locations. |
| 697 | SourceLocation Loc = |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 698 | SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc); |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 699 | Identifier.setLocation(Loc); |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 700 | |
Chris Lattner | 6e4bf52 | 2006-07-27 06:59:25 +0000 | [diff] [blame] | 701 | // If this is #define X X, we must mark the result as unexpandible. |
| 702 | if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) |
| 703 | if (NewII->getMacroInfo() == MI) |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 704 | Identifier.setFlag(LexerToken::DisableExpand); |
Chris Lattner | 6e4bf52 | 2006-07-27 06:59:25 +0000 | [diff] [blame] | 705 | |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 706 | // Since this is not an identifier token, it can't be macro expanded, so |
| 707 | // we're done. |
| 708 | ++NumFastMacroExpanded; |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 709 | return false; |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 712 | // Start expanding the macro. |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 713 | EnterMacro(Identifier, Args); |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 714 | |
| 715 | // Now that the macro is at the top of the include stack, ask the |
| 716 | // preprocessor to read the next token from it. |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 717 | Lex(Identifier); |
| 718 | return false; |
| 719 | } |
| 720 | |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 721 | /// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is |
Chris Lattner | 2ada5d3 | 2006-07-15 07:51:24 +0000 | [diff] [blame] | 722 | /// invoked to read all of the actual arguments specified for the macro |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 723 | /// invocation. This returns null on error. |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 724 | MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(LexerToken &MacroName, |
| 725 | MacroInfo *MI) { |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 726 | // The number of fixed arguments to parse. |
| 727 | unsigned NumFixedArgsLeft = MI->getNumArgs(); |
| 728 | bool isVariadic = MI->isVariadic(); |
| 729 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 730 | // Outer loop, while there are more arguments, keep reading them. |
| 731 | LexerToken Tok; |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 732 | Tok.setKind(tok::comma); |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 733 | --NumFixedArgsLeft; // Start reading the first arg. |
Chris Lattner | 36b6e81 | 2006-07-21 06:38:30 +0000 | [diff] [blame] | 734 | |
| 735 | // ArgTokens - Build up a list of tokens that make up each argument. Each |
Chris Lattner | 7a4af3b | 2006-07-26 06:26:52 +0000 | [diff] [blame] | 736 | // argument is separated by an EOF token. Use a SmallVector so we can avoid |
| 737 | // heap allocations in the common case. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 738 | llvm::SmallVector<LexerToken, 64> ArgTokens; |
Chris Lattner | 36b6e81 | 2006-07-21 06:38:30 +0000 | [diff] [blame] | 739 | |
| 740 | unsigned NumActuals = 0; |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 741 | while (Tok.getKind() == tok::comma) { |
Chris Lattner | 24dbee7 | 2007-07-19 16:11:58 +0000 | [diff] [blame] | 742 | // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note |
| 743 | // that we already consumed the first one. |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 744 | unsigned NumParens = 0; |
Chris Lattner | 36b6e81 | 2006-07-21 06:38:30 +0000 | [diff] [blame] | 745 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 746 | while (1) { |
Chris Lattner | afe603f | 2006-07-11 04:02:46 +0000 | [diff] [blame] | 747 | // Read arguments as unexpanded tokens. This avoids issues, e.g., where |
| 748 | // an argument value in a macro could expand to ',' or '(' or ')'. |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 749 | LexUnexpandedToken(Tok); |
| 750 | |
| 751 | if (Tok.getKind() == tok::eof) { |
| 752 | Diag(MacroName, diag::err_unterm_macro_invoc); |
| 753 | // Do not lose the EOF. Return it to the client. |
| 754 | MacroName = Tok; |
| 755 | return 0; |
| 756 | } else if (Tok.getKind() == tok::r_paren) { |
| 757 | // If we found the ) token, the macro arg list is done. |
| 758 | if (NumParens-- == 0) |
| 759 | break; |
| 760 | } else if (Tok.getKind() == tok::l_paren) { |
| 761 | ++NumParens; |
| 762 | } else if (Tok.getKind() == tok::comma && NumParens == 0) { |
| 763 | // Comma ends this argument if there are more fixed arguments expected. |
| 764 | if (NumFixedArgsLeft) |
| 765 | break; |
| 766 | |
Chris Lattner | 2ada5d3 | 2006-07-15 07:51:24 +0000 | [diff] [blame] | 767 | // If this is not a variadic macro, too many args were specified. |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 768 | if (!isVariadic) { |
| 769 | // Emit the diagnostic at the macro name in case there is a missing ). |
| 770 | // Emitting it at the , could be far away from the macro name. |
Chris Lattner | 2ada5d3 | 2006-07-15 07:51:24 +0000 | [diff] [blame] | 771 | Diag(MacroName, diag::err_too_many_args_in_macro_invoc); |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 772 | return 0; |
| 773 | } |
| 774 | // Otherwise, continue to add the tokens to this variable argument. |
Chris Lattner | b352e3e | 2006-11-21 06:17:10 +0000 | [diff] [blame] | 775 | } else if (Tok.getKind() == tok::comment && !KeepMacroComments) { |
Chris Lattner | 457fc15 | 2006-07-29 06:30:25 +0000 | [diff] [blame] | 776 | // If this is a comment token in the argument list and we're just in |
| 777 | // -C mode (not -CC mode), discard the comment. |
| 778 | continue; |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | ArgTokens.push_back(Tok); |
| 782 | } |
| 783 | |
Chris Lattner | a12dd15 | 2006-07-11 04:09:02 +0000 | [diff] [blame] | 784 | // Empty arguments are standard in C99 and supported as an extension in |
| 785 | // other modes. |
| 786 | if (ArgTokens.empty() && !Features.C99) |
| 787 | Diag(Tok, diag::ext_empty_fnmacro_arg); |
Chris Lattner | afe603f | 2006-07-11 04:02:46 +0000 | [diff] [blame] | 788 | |
Chris Lattner | 36b6e81 | 2006-07-21 06:38:30 +0000 | [diff] [blame] | 789 | // Add a marker EOF token to the end of the token list for this argument. |
| 790 | LexerToken EOFTok; |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 791 | EOFTok.startToken(); |
| 792 | EOFTok.setKind(tok::eof); |
| 793 | EOFTok.setLocation(Tok.getLocation()); |
| 794 | EOFTok.setLength(0); |
Chris Lattner | 36b6e81 | 2006-07-21 06:38:30 +0000 | [diff] [blame] | 795 | ArgTokens.push_back(EOFTok); |
| 796 | ++NumActuals; |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 797 | --NumFixedArgsLeft; |
| 798 | }; |
| 799 | |
| 800 | // Okay, we either found the r_paren. Check to see if we parsed too few |
| 801 | // arguments. |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 802 | unsigned MinArgsExpected = MI->getNumArgs(); |
| 803 | |
Chris Lattner | 775d832 | 2006-07-29 04:39:41 +0000 | [diff] [blame] | 804 | // See MacroArgs instance var for description of this. |
| 805 | bool isVarargsElided = false; |
| 806 | |
Chris Lattner | 2ada5d3 | 2006-07-15 07:51:24 +0000 | [diff] [blame] | 807 | if (NumActuals < MinArgsExpected) { |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 808 | // There are several cases where too few arguments is ok, handle them now. |
Chris Lattner | 2ada5d3 | 2006-07-15 07:51:24 +0000 | [diff] [blame] | 809 | if (NumActuals+1 == MinArgsExpected && MI->isVariadic()) { |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 810 | // Varargs where the named vararg parameter is missing: ok as extension. |
| 811 | // #define A(x, ...) |
| 812 | // A("blah") |
| 813 | Diag(Tok, diag::ext_missing_varargs_arg); |
Chris Lattner | 775d832 | 2006-07-29 04:39:41 +0000 | [diff] [blame] | 814 | |
| 815 | // Remember this occurred if this is a C99 macro invocation with at least |
| 816 | // one actual argument. |
Chris Lattner | 95a06b3 | 2006-07-30 08:40:43 +0000 | [diff] [blame] | 817 | isVarargsElided = MI->isC99Varargs() && MI->getNumArgs() > 1; |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 818 | } else if (MI->getNumArgs() == 1) { |
| 819 | // #define A(x) |
| 820 | // A() |
Chris Lattner | e7a5130 | 2006-07-29 01:25:12 +0000 | [diff] [blame] | 821 | // is ok because it is an empty argument. |
Chris Lattner | a12dd15 | 2006-07-11 04:09:02 +0000 | [diff] [blame] | 822 | |
| 823 | // Empty arguments are standard in C99 and supported as an extension in |
| 824 | // other modes. |
| 825 | if (ArgTokens.empty() && !Features.C99) |
| 826 | Diag(Tok, diag::ext_empty_fnmacro_arg); |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 827 | } else { |
| 828 | // Otherwise, emit the error. |
Chris Lattner | 2ada5d3 | 2006-07-15 07:51:24 +0000 | [diff] [blame] | 829 | Diag(Tok, diag::err_too_few_args_in_macro_invoc); |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 830 | return 0; |
| 831 | } |
Chris Lattner | e7a5130 | 2006-07-29 01:25:12 +0000 | [diff] [blame] | 832 | |
| 833 | // Add a marker EOF token to the end of the token list for this argument. |
| 834 | SourceLocation EndLoc = Tok.getLocation(); |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 835 | Tok.startToken(); |
| 836 | Tok.setKind(tok::eof); |
| 837 | Tok.setLocation(EndLoc); |
| 838 | Tok.setLength(0); |
Chris Lattner | e7a5130 | 2006-07-29 01:25:12 +0000 | [diff] [blame] | 839 | ArgTokens.push_back(Tok); |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Chris Lattner | 775d832 | 2006-07-29 04:39:41 +0000 | [diff] [blame] | 842 | return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size(),isVarargsElided); |
Chris Lattner | f373a4a | 2006-06-26 06:16:29 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 845 | /// ComputeDATE_TIME - Compute the current time, enter it into the specified |
| 846 | /// scratch buffer, then return DATELoc/TIMELoc locations with the position of |
| 847 | /// the identifier tokens inserted. |
| 848 | static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc, |
Chris Lattner | b94ec7b | 2006-07-14 06:54:10 +0000 | [diff] [blame] | 849 | Preprocessor &PP) { |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 850 | time_t TT = time(0); |
| 851 | struct tm *TM = localtime(&TT); |
| 852 | |
| 853 | static const char * const Months[] = { |
| 854 | "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" |
| 855 | }; |
| 856 | |
| 857 | char TmpBuffer[100]; |
| 858 | sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday, |
| 859 | TM->tm_year+1900); |
Chris Lattner | b94ec7b | 2006-07-14 06:54:10 +0000 | [diff] [blame] | 860 | DATELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer)); |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 861 | |
| 862 | sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec); |
Chris Lattner | b94ec7b | 2006-07-14 06:54:10 +0000 | [diff] [blame] | 863 | TIMELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer)); |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 866 | /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded |
| 867 | /// as a builtin macro, handle it and return the next token as 'Tok'. |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 868 | void Preprocessor::ExpandBuiltinMacro(LexerToken &Tok) { |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 869 | // Figure out which token this is. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 870 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 871 | assert(II && "Can't be a macro without id info!"); |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 872 | |
| 873 | // If this is an _Pragma directive, expand it, invoke the pragma handler, then |
| 874 | // lex the token after it. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 875 | if (II == Ident_Pragma) |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 876 | return Handle_Pragma(Tok); |
| 877 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 878 | ++NumBuiltinMacroExpanded; |
| 879 | |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 880 | char TmpBuffer[100]; |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 881 | |
| 882 | // Set up the return result. |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 883 | Tok.setIdentifierInfo(0); |
| 884 | Tok.clearFlag(LexerToken::NeedsCleaning); |
Chris Lattner | 630b33c | 2006-07-01 22:46:53 +0000 | [diff] [blame] | 885 | |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 886 | if (II == Ident__LINE__) { |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 887 | // __LINE__ expands to a simple numeric value. |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 888 | sprintf(TmpBuffer, "%u", SourceMgr.getLogicalLineNumber(Tok.getLocation())); |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 889 | unsigned Length = strlen(TmpBuffer); |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 890 | Tok.setKind(tok::numeric_constant); |
| 891 | Tok.setLength(Length); |
| 892 | Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation())); |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 893 | } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) { |
Chris Lattner | c1283b9 | 2006-07-01 23:16:30 +0000 | [diff] [blame] | 894 | SourceLocation Loc = Tok.getLocation(); |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 895 | if (II == Ident__BASE_FILE__) { |
Chris Lattner | c1283b9 | 2006-07-01 23:16:30 +0000 | [diff] [blame] | 896 | Diag(Tok, diag::ext_pp_base_file); |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 897 | SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc); |
| 898 | while (NextLoc.isValid()) { |
Chris Lattner | c1283b9 | 2006-07-01 23:16:30 +0000 | [diff] [blame] | 899 | Loc = NextLoc; |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 900 | NextLoc = SourceMgr.getIncludeLoc(Loc); |
Chris Lattner | c1283b9 | 2006-07-01 23:16:30 +0000 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | |
Chris Lattner | 0766e59 | 2006-07-03 01:07:01 +0000 | [diff] [blame] | 904 | // Escape this filename. Turn '\' -> '\\' '"' -> '\"' |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 905 | std::string FN = SourceMgr.getSourceName(SourceMgr.getLogicalLoc(Loc)); |
Chris Lattner | ecc39e9 | 2006-07-15 05:23:31 +0000 | [diff] [blame] | 906 | FN = '"' + Lexer::Stringify(FN) + '"'; |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 907 | Tok.setKind(tok::string_literal); |
| 908 | Tok.setLength(FN.size()); |
| 909 | Tok.setLocation(CreateString(&FN[0], FN.size(), Tok.getLocation())); |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 910 | } else if (II == Ident__DATE__) { |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 911 | if (!DATELoc.isValid()) |
Chris Lattner | b94ec7b | 2006-07-14 06:54:10 +0000 | [diff] [blame] | 912 | ComputeDATE_TIME(DATELoc, TIMELoc, *this); |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 913 | Tok.setKind(tok::string_literal); |
| 914 | Tok.setLength(strlen("\"Mmm dd yyyy\"")); |
| 915 | Tok.setLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation())); |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 916 | } else if (II == Ident__TIME__) { |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 917 | if (!TIMELoc.isValid()) |
Chris Lattner | b94ec7b | 2006-07-14 06:54:10 +0000 | [diff] [blame] | 918 | ComputeDATE_TIME(DATELoc, TIMELoc, *this); |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 919 | Tok.setKind(tok::string_literal); |
| 920 | Tok.setLength(strlen("\"hh:mm:ss\"")); |
| 921 | Tok.setLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation())); |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 922 | } else if (II == Ident__INCLUDE_LEVEL__) { |
Chris Lattner | c1283b9 | 2006-07-01 23:16:30 +0000 | [diff] [blame] | 923 | Diag(Tok, diag::ext_pp_include_level); |
| 924 | |
| 925 | // Compute the include depth of this token. |
| 926 | unsigned Depth = 0; |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 927 | SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation()); |
| 928 | for (; Loc.isValid(); ++Depth) |
| 929 | Loc = SourceMgr.getIncludeLoc(Loc); |
Chris Lattner | c1283b9 | 2006-07-01 23:16:30 +0000 | [diff] [blame] | 930 | |
| 931 | // __INCLUDE_LEVEL__ expands to a simple numeric value. |
| 932 | sprintf(TmpBuffer, "%u", Depth); |
| 933 | unsigned Length = strlen(TmpBuffer); |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 934 | Tok.setKind(tok::numeric_constant); |
| 935 | Tok.setLength(Length); |
| 936 | Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation())); |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 937 | } else if (II == Ident__TIMESTAMP__) { |
Chris Lattner | 847e0e4 | 2006-07-01 23:49:16 +0000 | [diff] [blame] | 938 | // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be |
| 939 | // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime. |
| 940 | Diag(Tok, diag::ext_pp_timestamp); |
| 941 | |
| 942 | // Get the file that we are lexing out of. If we're currently lexing from |
| 943 | // a macro, dig into the include stack. |
| 944 | const FileEntry *CurFile = 0; |
Chris Lattner | ecfeafe | 2006-07-02 21:26:45 +0000 | [diff] [blame] | 945 | Lexer *TheLexer = getCurrentFileLexer(); |
Chris Lattner | 847e0e4 | 2006-07-01 23:49:16 +0000 | [diff] [blame] | 946 | |
| 947 | if (TheLexer) |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 948 | CurFile = SourceMgr.getFileEntryForLoc(TheLexer->getFileLoc()); |
Chris Lattner | 847e0e4 | 2006-07-01 23:49:16 +0000 | [diff] [blame] | 949 | |
| 950 | // If this file is older than the file it depends on, emit a diagnostic. |
| 951 | const char *Result; |
| 952 | if (CurFile) { |
| 953 | time_t TT = CurFile->getModificationTime(); |
| 954 | struct tm *TM = localtime(&TT); |
| 955 | Result = asctime(TM); |
| 956 | } else { |
| 957 | Result = "??? ??? ?? ??:??:?? ????\n"; |
| 958 | } |
| 959 | TmpBuffer[0] = '"'; |
| 960 | strcpy(TmpBuffer+1, Result); |
| 961 | unsigned Len = strlen(TmpBuffer); |
| 962 | TmpBuffer[Len-1] = '"'; // Replace the newline with a quote. |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 963 | Tok.setKind(tok::string_literal); |
| 964 | Tok.setLength(Len); |
| 965 | Tok.setLocation(CreateString(TmpBuffer, Len, Tok.getLocation())); |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 966 | } else { |
| 967 | assert(0 && "Unknown identifier!"); |
| 968 | } |
| 969 | } |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 970 | |
| 971 | //===----------------------------------------------------------------------===// |
| 972 | // Lexer Event Handling. |
| 973 | //===----------------------------------------------------------------------===// |
| 974 | |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 975 | /// LookUpIdentifierInfo - Given a tok::identifier token, look up the |
| 976 | /// identifier information for the token and install it into the token. |
| 977 | IdentifierInfo *Preprocessor::LookUpIdentifierInfo(LexerToken &Identifier, |
| 978 | const char *BufPtr) { |
| 979 | assert(Identifier.getKind() == tok::identifier && "Not an identifier!"); |
| 980 | assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!"); |
| 981 | |
| 982 | // Look up this token, see if it is a macro, or if it is a language keyword. |
| 983 | IdentifierInfo *II; |
| 984 | if (BufPtr && !Identifier.needsCleaning()) { |
| 985 | // No cleaning needed, just use the characters from the lexed buffer. |
| 986 | II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength()); |
| 987 | } else { |
| 988 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
Chris Lattner | f9aba2c | 2007-07-13 17:10:38 +0000 | [diff] [blame] | 989 | llvm::SmallVector<char, 64> IdentifierBuffer; |
| 990 | IdentifierBuffer.resize(Identifier.getLength()); |
| 991 | const char *TmpBuf = &IdentifierBuffer[0]; |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 992 | unsigned Size = getSpelling(Identifier, TmpBuf); |
| 993 | II = getIdentifierInfo(TmpBuf, TmpBuf+Size); |
| 994 | } |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 995 | Identifier.setIdentifierInfo(II); |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 996 | return II; |
| 997 | } |
| 998 | |
| 999 | |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 1000 | /// HandleIdentifier - This callback is invoked when the lexer reads an |
| 1001 | /// identifier. This callback looks up the identifier in the map and/or |
| 1002 | /// potentially macro expands it or turns it into a named token (like 'for'). |
| 1003 | void Preprocessor::HandleIdentifier(LexerToken &Identifier) { |
Chris Lattner | 0f1f505 | 2006-07-20 04:16:23 +0000 | [diff] [blame] | 1004 | assert(Identifier.getIdentifierInfo() && |
| 1005 | "Can't handle identifiers without identifier info!"); |
| 1006 | |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 1007 | IdentifierInfo &II = *Identifier.getIdentifierInfo(); |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 1008 | |
| 1009 | // If this identifier was poisoned, and if it was not produced from a macro |
| 1010 | // expansion, emit an error. |
Chris Lattner | 8ff7199 | 2006-07-06 05:17:39 +0000 | [diff] [blame] | 1011 | if (II.isPoisoned() && CurLexer) { |
| 1012 | if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning. |
| 1013 | Diag(Identifier, diag::err_pp_used_poisoned_id); |
| 1014 | else |
| 1015 | Diag(Identifier, diag::ext_pp_bad_vaargs_use); |
| 1016 | } |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 1017 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 1018 | // If this is a macro to be expanded, do it. |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 1019 | if (MacroInfo *MI = II.getMacroInfo()) { |
Chris Lattner | 6e4bf52 | 2006-07-27 06:59:25 +0000 | [diff] [blame] | 1020 | if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) { |
| 1021 | if (MI->isEnabled()) { |
| 1022 | if (!HandleMacroExpandedIdentifier(Identifier, MI)) |
| 1023 | return; |
| 1024 | } else { |
| 1025 | // C99 6.10.3.4p2 says that a disabled macro may never again be |
| 1026 | // expanded, even if it's in a context where it could be expanded in the |
| 1027 | // future. |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 1028 | Identifier.setFlag(LexerToken::DisableExpand); |
Chris Lattner | 6e4bf52 | 2006-07-27 06:59:25 +0000 | [diff] [blame] | 1029 | } |
| 1030 | } |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 1031 | } else if (II.isOtherTargetMacro() && !DisableMacroExpansion) { |
| 1032 | // If this identifier is a macro on some other target, emit a diagnostic. |
| 1033 | // This diagnosic is only emitted when macro expansion is enabled, because |
| 1034 | // the macro would not have been expanded for the other target either. |
| 1035 | II.setIsOtherTargetMacro(false); // Don't warn on second use. |
| 1036 | getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(), |
| 1037 | diag::port_target_macro_use); |
| 1038 | |
| 1039 | } |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 1040 | |
Chris Lattner | 5b9f489 | 2006-11-21 17:23:33 +0000 | [diff] [blame] | 1041 | // C++ 2.11p2: If this is an alternative representation of a C++ operator, |
| 1042 | // then we act as if it is the actual operator and not the textual |
| 1043 | // representation of it. |
| 1044 | if (II.isCPlusPlusOperatorKeyword()) |
| 1045 | Identifier.setIdentifierInfo(0); |
| 1046 | |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 1047 | // Change the kind of this identifier to the appropriate token kind, e.g. |
| 1048 | // turning "for" into a keyword. |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 1049 | Identifier.setKind(II.getTokenID()); |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 1050 | |
| 1051 | // If this is an extension token, diagnose its use. |
Steve Naroff | a8fd973 | 2007-06-11 00:35:03 +0000 | [diff] [blame] | 1052 | // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99 |
| 1053 | // For now, I'm just commenting it out (while I work on attributes). |
Chris Lattner | 53621a5 | 2007-06-13 20:44:40 +0000 | [diff] [blame] | 1054 | if (II.isExtensionToken() && Features.C99) |
| 1055 | Diag(Identifier, diag::ext_token_used); |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1058 | /// HandleEndOfFile - This callback is invoked when the lexer hits the end of |
| 1059 | /// the current file. This either returns the EOF token or pops a level off |
| 1060 | /// the include stack and keeps going. |
Chris Lattner | 2183a6e | 2006-07-18 06:36:12 +0000 | [diff] [blame] | 1061 | bool Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1062 | assert(!CurMacroExpander && |
| 1063 | "Ending a file when currently in a macro!"); |
| 1064 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1065 | // See if this file had a controlling macro. |
Chris Lattner | 3665f16 | 2006-07-04 07:26:10 +0000 | [diff] [blame] | 1066 | if (CurLexer) { // Not ending a macro, ignore it. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 1067 | if (const IdentifierInfo *ControllingMacro = |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1068 | CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) { |
Chris Lattner | 3665f16 | 2006-07-04 07:26:10 +0000 | [diff] [blame] | 1069 | // Okay, this has a controlling macro, remember in PerFileInfo. |
| 1070 | if (const FileEntry *FE = |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 1071 | SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())) |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1072 | HeaderInfo.SetFileControllingMacro(FE, ControllingMacro); |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1076 | // If this is a #include'd file, pop it off the include stack and continue |
| 1077 | // lexing the #includer file. |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 1078 | if (!IncludeMacroStack.empty()) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1079 | // We're done with the #included file. |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 1080 | RemoveTopOfLexerStack(); |
Chris Lattner | 0c885f5 | 2006-06-21 06:50:18 +0000 | [diff] [blame] | 1081 | |
| 1082 | // Notify the client, if desired, that we are in a new source file. |
Chris Lattner | b8d6d5a | 2006-11-21 04:09:30 +0000 | [diff] [blame] | 1083 | if (Callbacks && !isEndOfMacro && CurLexer) { |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1084 | DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir; |
| 1085 | |
| 1086 | // Get the file entry for the current file. |
| 1087 | if (const FileEntry *FE = |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame^] | 1088 | SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())) |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1089 | FileType = HeaderInfo.getFileDirFlavor(FE); |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1090 | |
Chris Lattner | b8d6d5a | 2006-11-21 04:09:30 +0000 | [diff] [blame] | 1091 | Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr), |
| 1092 | PPCallbacks::ExitFile, FileType); |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1093 | } |
Chris Lattner | 2183a6e | 2006-07-18 06:36:12 +0000 | [diff] [blame] | 1094 | |
| 1095 | // Client should lex another token. |
| 1096 | return false; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 1099 | Result.startToken(); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 1100 | CurLexer->BufferPtr = CurLexer->BufferEnd; |
| 1101 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd); |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 1102 | Result.setKind(tok::eof); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1103 | |
| 1104 | // We're done with the #included file. |
| 1105 | delete CurLexer; |
| 1106 | CurLexer = 0; |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 1107 | |
Chris Lattner | 03f8348 | 2006-07-10 06:16:26 +0000 | [diff] [blame] | 1108 | // This is the end of the top-level file. If the diag::pp_macro_not_used |
| 1109 | // diagnostic is enabled, walk all of the identifiers, looking for macros that |
| 1110 | // have not been used. |
Chris Lattner | b055f2d | 2007-02-11 08:19:57 +0000 | [diff] [blame] | 1111 | if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){ |
| 1112 | for (IdentifierTable::iterator I = Identifiers.begin(), |
| 1113 | E = Identifiers.end(); I != E; ++I) { |
| 1114 | const IdentifierInfo &II = I->getValue(); |
| 1115 | if (II.getMacroInfo() && !II.getMacroInfo()->isUsed()) |
| 1116 | Diag(II.getMacroInfo()->getDefinitionLoc(), diag::pp_macro_not_used); |
| 1117 | } |
| 1118 | } |
Chris Lattner | 2183a6e | 2006-07-18 06:36:12 +0000 | [diff] [blame] | 1119 | |
| 1120 | return true; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | /// HandleEndOfMacro - This callback is invoked when the lexer hits the end of |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 1124 | /// the current macro expansion or token stream expansion. |
Chris Lattner | 2183a6e | 2006-07-18 06:36:12 +0000 | [diff] [blame] | 1125 | bool Preprocessor::HandleEndOfMacro(LexerToken &Result) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1126 | assert(CurMacroExpander && !CurLexer && |
| 1127 | "Ending a macro when currently in a #include file!"); |
| 1128 | |
Chris Lattner | c02c4ab | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 1129 | // Delete or cache the now-dead macro expander. |
| 1130 | if (NumCachedMacroExpanders == MacroExpanderCacheSize) |
| 1131 | delete CurMacroExpander; |
| 1132 | else |
| 1133 | MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1134 | |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 1135 | // Handle this like a #include file being popped off the stack. |
| 1136 | CurMacroExpander = 0; |
| 1137 | return HandleEndOfFile(Result, true); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | |
| 1141 | //===----------------------------------------------------------------------===// |
| 1142 | // Utility Methods for Preprocessor Directive Handling. |
| 1143 | //===----------------------------------------------------------------------===// |
| 1144 | |
| 1145 | /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the |
| 1146 | /// current line until the tok::eom token is found. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1147 | void Preprocessor::DiscardUntilEndOfDirective() { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1148 | LexerToken Tmp; |
| 1149 | do { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1150 | LexUnexpandedToken(Tmp); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1151 | } while (Tmp.getKind() != tok::eom); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Chris Lattner | 652c169 | 2006-11-21 23:47:30 +0000 | [diff] [blame] | 1154 | /// isCXXNamedOperator - Returns "true" if the token is a named operator in C++. |
| 1155 | static bool isCXXNamedOperator(const std::string &Spelling) { |
| 1156 | return Spelling == "and" || Spelling == "bitand" || Spelling == "bitor" || |
| 1157 | Spelling == "compl" || Spelling == "not" || Spelling == "not_eq" || |
| 1158 | Spelling == "or" || Spelling == "xor"; |
| 1159 | } |
| 1160 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1161 | /// ReadMacroName - Lex and validate a macro name, which occurs after a |
| 1162 | /// #define or #undef. This sets the token kind to eom and discards the rest |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 1163 | /// of the macro line if the macro name is invalid. isDefineUndef is 1 if |
| 1164 | /// this is due to a a #define, 2 if #undef directive, 0 if it is something |
Chris Lattner | 44f8a66 | 2006-07-03 01:27:27 +0000 | [diff] [blame] | 1165 | /// else (e.g. #ifdef). |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 1166 | void Preprocessor::ReadMacroName(LexerToken &MacroNameTok, char isDefineUndef) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1167 | // Read the token, don't allow macro expansion on it. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1168 | LexUnexpandedToken(MacroNameTok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1169 | |
| 1170 | // Missing macro name? |
| 1171 | if (MacroNameTok.getKind() == tok::eom) |
| 1172 | return Diag(MacroNameTok, diag::err_pp_missing_macro_name); |
| 1173 | |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 1174 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
| 1175 | if (II == 0) { |
Chris Lattner | 652c169 | 2006-11-21 23:47:30 +0000 | [diff] [blame] | 1176 | std::string Spelling = getSpelling(MacroNameTok); |
| 1177 | if (isCXXNamedOperator(Spelling)) |
| 1178 | // C++ 2.5p2: Alternative tokens behave the same as its primary token |
| 1179 | // except for their spellings. |
| 1180 | Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name, Spelling); |
| 1181 | else |
| 1182 | Diag(MacroNameTok, diag::err_pp_macro_not_identifier); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1183 | // Fall through on error. |
Chris Lattner | 2bb8a95 | 2006-11-21 22:24:17 +0000 | [diff] [blame] | 1184 | } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) { |
Chris Lattner | 44f8a66 | 2006-07-03 01:27:27 +0000 | [diff] [blame] | 1185 | // Error if defining "defined": C99 6.10.8.4. |
Chris Lattner | aaf0911 | 2006-07-03 01:17:59 +0000 | [diff] [blame] | 1186 | Diag(MacroNameTok, diag::err_defined_macro_name); |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 1187 | } else if (isDefineUndef && II->getMacroInfo() && |
| 1188 | II->getMacroInfo()->isBuiltinMacro()) { |
Chris Lattner | 44f8a66 | 2006-07-03 01:27:27 +0000 | [diff] [blame] | 1189 | // Error if defining "__LINE__" and other builtins: C99 6.10.8.4. |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 1190 | if (isDefineUndef == 1) |
| 1191 | Diag(MacroNameTok, diag::pp_redef_builtin_macro); |
| 1192 | else |
| 1193 | Diag(MacroNameTok, diag::pp_undef_builtin_macro); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1194 | } else { |
| 1195 | // Okay, we got a good identifier node. Return it. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1196 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1199 | // Invalid macro name, read and discard the rest of the line. Then set the |
| 1200 | // token kind to tok::eom. |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 1201 | MacroNameTok.setKind(tok::eom); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1202 | return DiscardUntilEndOfDirective(); |
| 1203 | } |
| 1204 | |
| 1205 | /// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If |
| 1206 | /// not, emit a diagnostic and consume up until the eom. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1207 | void Preprocessor::CheckEndOfDirective(const char *DirType) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1208 | LexerToken Tmp; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1209 | Lex(Tmp); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1210 | // There should be no tokens after the directive, but we allow them as an |
| 1211 | // extension. |
Chris Lattner | bcb416b | 2006-10-27 05:43:50 +0000 | [diff] [blame] | 1212 | while (Tmp.getKind() == tok::comment) // Skip comments in -C mode. |
| 1213 | Lex(Tmp); |
| 1214 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1215 | if (Tmp.getKind() != tok::eom) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1216 | Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType); |
| 1217 | DiscardUntilEndOfDirective(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1218 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | |
| 1222 | |
| 1223 | /// SkipExcludedConditionalBlock - We just read a #if or related directive and |
| 1224 | /// decided that the subsequent tokens are in the #if'd out portion of the |
| 1225 | /// file. Lex the rest of the file, until we see an #endif. If |
| 1226 | /// FoundNonSkipPortion is true, then we have already emitted code for part of |
| 1227 | /// this #if directive, so #else/#elif blocks should never be entered. If ElseOk |
| 1228 | /// is true, then #else directives are ok, if not, then we have already seen one |
| 1229 | /// so a #else directive is a duplicate. When this returns, the caller can lex |
| 1230 | /// the first valid token. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 1231 | void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1232 | bool FoundNonSkipPortion, |
| 1233 | bool FoundElse) { |
| 1234 | ++NumSkipped; |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 1235 | assert(CurMacroExpander == 0 && CurLexer && |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1236 | "Lexing a macro, not a file?"); |
| 1237 | |
| 1238 | CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false, |
| 1239 | FoundNonSkipPortion, FoundElse); |
| 1240 | |
Chris Lattner | 3ebcf4e | 2006-07-11 05:39:23 +0000 | [diff] [blame] | 1241 | // Enter raw mode to disable identifier lookup (and thus macro expansion), |
| 1242 | // disabling warnings, etc. |
| 1243 | CurLexer->LexingRawMode = true; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1244 | LexerToken Tok; |
| 1245 | while (1) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1246 | CurLexer->Lex(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1247 | |
Chris Lattner | d8aee0e | 2006-07-11 05:04:55 +0000 | [diff] [blame] | 1248 | // If this is the end of the buffer, we have an error. |
| 1249 | if (Tok.getKind() == tok::eof) { |
| 1250 | // Emit errors for each unterminated conditional on the stack, including |
| 1251 | // the current one. |
| 1252 | while (!CurLexer->ConditionalStack.empty()) { |
| 1253 | Diag(CurLexer->ConditionalStack.back().IfLoc, |
| 1254 | diag::err_pp_unterminated_conditional); |
| 1255 | CurLexer->ConditionalStack.pop_back(); |
| 1256 | } |
| 1257 | |
| 1258 | // Just return and let the caller lex after this #include. |
| 1259 | break; |
| 1260 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1261 | |
| 1262 | // If this token is not a preprocessor directive, just skip it. |
| 1263 | if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine()) |
| 1264 | continue; |
| 1265 | |
| 1266 | // We just parsed a # character at the start of a line, so we're in |
| 1267 | // directive mode. Tell the lexer this so any newlines we see will be |
| 1268 | // converted into an EOM token (this terminates the macro). |
| 1269 | CurLexer->ParsingPreprocessorDirective = true; |
Chris Lattner | 457fc15 | 2006-07-29 06:30:25 +0000 | [diff] [blame] | 1270 | CurLexer->KeepCommentMode = false; |
| 1271 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1272 | |
| 1273 | // Read the next token, the directive flavor. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1274 | LexUnexpandedToken(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1275 | |
| 1276 | // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or |
| 1277 | // something bogus), skip it. |
| 1278 | if (Tok.getKind() != tok::identifier) { |
| 1279 | CurLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 457fc15 | 2006-07-29 06:30:25 +0000 | [diff] [blame] | 1280 | // Restore comment saving mode. |
Chris Lattner | b352e3e | 2006-11-21 06:17:10 +0000 | [diff] [blame] | 1281 | CurLexer->KeepCommentMode = KeepComments; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1282 | continue; |
| 1283 | } |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1284 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1285 | // If the first letter isn't i or e, it isn't intesting to us. We know that |
| 1286 | // this is safe in the face of spelling differences, because there is no way |
| 1287 | // to spell an i/e in a strange way that is another letter. Skipping this |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1288 | // allows us to avoid looking up the identifier info for #define/#undef and |
| 1289 | // other common directives. |
| 1290 | const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation()); |
| 1291 | char FirstChar = RawCharData[0]; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1292 | if (FirstChar >= 'a' && FirstChar <= 'z' && |
| 1293 | FirstChar != 'i' && FirstChar != 'e') { |
| 1294 | CurLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 457fc15 | 2006-07-29 06:30:25 +0000 | [diff] [blame] | 1295 | // Restore comment saving mode. |
Chris Lattner | b352e3e | 2006-11-21 06:17:10 +0000 | [diff] [blame] | 1296 | CurLexer->KeepCommentMode = KeepComments; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1297 | continue; |
| 1298 | } |
| 1299 | |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1300 | // Get the identifier name without trigraphs or embedded newlines. Note |
| 1301 | // that we can't use Tok.getIdentifierInfo() because its lookup is disabled |
| 1302 | // when skipping. |
| 1303 | // TODO: could do this with zero copies in the no-clean case by using |
| 1304 | // strncmp below. |
| 1305 | char Directive[20]; |
| 1306 | unsigned IdLen; |
| 1307 | if (!Tok.needsCleaning() && Tok.getLength() < 20) { |
| 1308 | IdLen = Tok.getLength(); |
| 1309 | memcpy(Directive, RawCharData, IdLen); |
| 1310 | Directive[IdLen] = 0; |
| 1311 | } else { |
| 1312 | std::string DirectiveStr = getSpelling(Tok); |
| 1313 | IdLen = DirectiveStr.size(); |
| 1314 | if (IdLen >= 20) { |
| 1315 | CurLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 457fc15 | 2006-07-29 06:30:25 +0000 | [diff] [blame] | 1316 | // Restore comment saving mode. |
Chris Lattner | b352e3e | 2006-11-21 06:17:10 +0000 | [diff] [blame] | 1317 | CurLexer->KeepCommentMode = KeepComments; |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1318 | continue; |
| 1319 | } |
| 1320 | memcpy(Directive, &DirectiveStr[0], IdLen); |
| 1321 | Directive[IdLen] = 0; |
| 1322 | } |
| 1323 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1324 | if (FirstChar == 'i' && Directive[1] == 'f') { |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1325 | if ((IdLen == 2) || // "if" |
| 1326 | (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef" |
| 1327 | (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1328 | // We know the entire #if/#ifdef/#ifndef block will be skipped, don't |
| 1329 | // bother parsing the condition. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1330 | DiscardUntilEndOfDirective(); |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 1331 | CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true, |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 1332 | /*foundnonskip*/false, |
| 1333 | /*fnddelse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1334 | } |
| 1335 | } else if (FirstChar == 'e') { |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1336 | if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif" |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1337 | CheckEndOfDirective("#endif"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1338 | PPConditionalInfo CondInfo; |
| 1339 | CondInfo.WasSkipping = true; // Silence bogus warning. |
| 1340 | bool InCond = CurLexer->popConditionalLevel(CondInfo); |
Chris Lattner | cf6bc66 | 2006-11-05 07:59:08 +0000 | [diff] [blame] | 1341 | InCond = InCond; // Silence warning in no-asserts mode. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1342 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
| 1343 | |
| 1344 | // If we popped the outermost skipping block, we're done skipping! |
| 1345 | if (!CondInfo.WasSkipping) |
| 1346 | break; |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1347 | } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else". |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1348 | // #else directive in a skipping conditional. If not in some other |
| 1349 | // skipping conditional, and if #else hasn't already been seen, enter it |
| 1350 | // as a non-skipping conditional. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1351 | CheckEndOfDirective("#else"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1352 | PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel(); |
| 1353 | |
| 1354 | // If this is a #else with a #else before it, report the error. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1355 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1356 | |
| 1357 | // Note that we've seen a #else in this conditional. |
| 1358 | CondInfo.FoundElse = true; |
| 1359 | |
| 1360 | // If the conditional is at the top level, and the #if block wasn't |
| 1361 | // entered, enter the #else block now. |
| 1362 | if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { |
| 1363 | CondInfo.FoundNonSkip = true; |
| 1364 | break; |
| 1365 | } |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1366 | } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif". |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1367 | PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel(); |
| 1368 | |
| 1369 | bool ShouldEnter; |
| 1370 | // If this is in a skipping block or if we're already handled this #if |
| 1371 | // block, don't bother parsing the condition. |
| 1372 | if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1373 | DiscardUntilEndOfDirective(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1374 | ShouldEnter = false; |
| 1375 | } else { |
Chris Lattner | 3ebcf4e | 2006-07-11 05:39:23 +0000 | [diff] [blame] | 1376 | // Restore the value of LexingRawMode so that identifiers are |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1377 | // looked up, etc, inside the #elif expression. |
Chris Lattner | 3ebcf4e | 2006-07-11 05:39:23 +0000 | [diff] [blame] | 1378 | assert(CurLexer->LexingRawMode && "We have to be skipping here!"); |
| 1379 | CurLexer->LexingRawMode = false; |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 1380 | IdentifierInfo *IfNDefMacro = 0; |
Chris Lattner | a8654ca | 2006-07-04 17:42:08 +0000 | [diff] [blame] | 1381 | ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); |
Chris Lattner | 3ebcf4e | 2006-07-11 05:39:23 +0000 | [diff] [blame] | 1382 | CurLexer->LexingRawMode = true; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | // If this is a #elif with a #else before it, report the error. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1386 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1387 | |
| 1388 | // If this condition is true, enter it! |
| 1389 | if (ShouldEnter) { |
| 1390 | CondInfo.FoundNonSkip = true; |
| 1391 | break; |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | CurLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 457fc15 | 2006-07-29 06:30:25 +0000 | [diff] [blame] | 1397 | // Restore comment saving mode. |
Chris Lattner | b352e3e | 2006-11-21 06:17:10 +0000 | [diff] [blame] | 1398 | CurLexer->KeepCommentMode = KeepComments; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | // Finally, if we are out of the conditional (saw an #endif or ran off the end |
| 1402 | // of the file, just stop skipping and return to lexing whatever came after |
| 1403 | // the #if block. |
Chris Lattner | 3ebcf4e | 2006-07-11 05:39:23 +0000 | [diff] [blame] | 1404 | CurLexer->LexingRawMode = false; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | //===----------------------------------------------------------------------===// |
| 1408 | // Preprocessor Directive Handling. |
| 1409 | //===----------------------------------------------------------------------===// |
| 1410 | |
| 1411 | /// HandleDirective - This callback is invoked when the lexer sees a # token |
| 1412 | /// at the start of a line. This consumes the directive, modifies the |
| 1413 | /// lexer/preprocessor state, and advances the lexer(s) so that the next token |
| 1414 | /// read is the correct one. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1415 | void Preprocessor::HandleDirective(LexerToken &Result) { |
Chris Lattner | 4d5e1a7 | 2006-07-03 01:01:29 +0000 | [diff] [blame] | 1416 | // FIXME: Traditional: # with whitespace before it not recognized by K&R? |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1417 | |
| 1418 | // We just parsed a # character at the start of a line, so we're in directive |
| 1419 | // mode. Tell the lexer this so any newlines we see will be converted into an |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 1420 | // EOM token (which terminates the directive). |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1421 | CurLexer->ParsingPreprocessorDirective = true; |
| 1422 | |
| 1423 | ++NumDirectives; |
| 1424 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1425 | // We are about to read a token. For the multiple-include optimization FA to |
| 1426 | // work, we have to remember if we had read any tokens *before* this |
| 1427 | // pp-directive. |
| 1428 | bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal(); |
| 1429 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 1430 | // Read the next token, the directive flavor. This isn't expanded due to |
| 1431 | // C99 6.10.3p8. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1432 | LexUnexpandedToken(Result); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1433 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 1434 | // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.: |
| 1435 | // #define A(x) #x |
| 1436 | // A(abc |
| 1437 | // #warning blah |
| 1438 | // def) |
| 1439 | // If so, the user is relying on non-portable behavior, emit a diagnostic. |
Chris Lattner | ee8760b | 2006-07-15 07:42:55 +0000 | [diff] [blame] | 1440 | if (InMacroArgs) |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 1441 | Diag(Result, diag::ext_embedded_directive); |
| 1442 | |
Chris Lattner | bcb416b | 2006-10-27 05:43:50 +0000 | [diff] [blame] | 1443 | TryAgain: |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1444 | switch (Result.getKind()) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1445 | case tok::eom: |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1446 | return; // null directive. |
Chris Lattner | bcb416b | 2006-10-27 05:43:50 +0000 | [diff] [blame] | 1447 | case tok::comment: |
| 1448 | // Handle stuff like "# /*foo*/ define X" in -E -C mode. |
| 1449 | LexUnexpandedToken(Result); |
| 1450 | goto TryAgain; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1451 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1452 | case tok::numeric_constant: |
| 1453 | // FIXME: implement # 7 line numbers! |
Chris Lattner | 6e5b2a0 | 2006-10-17 02:53:32 +0000 | [diff] [blame] | 1454 | DiscardUntilEndOfDirective(); |
| 1455 | return; |
Chris Lattner | 87d3bec | 2006-10-17 03:44:32 +0000 | [diff] [blame] | 1456 | default: |
| 1457 | IdentifierInfo *II = Result.getIdentifierInfo(); |
| 1458 | if (II == 0) break; // Not an identifier. |
| 1459 | |
| 1460 | // Ask what the preprocessor keyword ID is. |
| 1461 | switch (II->getPPKeywordID()) { |
| 1462 | default: break; |
| 1463 | // C99 6.10.1 - Conditional Inclusion. |
| 1464 | case tok::pp_if: |
| 1465 | return HandleIfDirective(Result, ReadAnyTokensBeforeDirective); |
| 1466 | case tok::pp_ifdef: |
| 1467 | return HandleIfdefDirective(Result, false, true/*not valid for miopt*/); |
| 1468 | case tok::pp_ifndef: |
| 1469 | return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective); |
| 1470 | case tok::pp_elif: |
| 1471 | return HandleElifDirective(Result); |
| 1472 | case tok::pp_else: |
| 1473 | return HandleElseDirective(Result); |
| 1474 | case tok::pp_endif: |
| 1475 | return HandleEndifDirective(Result); |
| 1476 | |
| 1477 | // C99 6.10.2 - Source File Inclusion. |
| 1478 | case tok::pp_include: |
| 1479 | return HandleIncludeDirective(Result); // Handle #include. |
| 1480 | |
| 1481 | // C99 6.10.3 - Macro Replacement. |
| 1482 | case tok::pp_define: |
| 1483 | return HandleDefineDirective(Result, false); |
| 1484 | case tok::pp_undef: |
| 1485 | return HandleUndefDirective(Result); |
| 1486 | |
| 1487 | // C99 6.10.4 - Line Control. |
| 1488 | case tok::pp_line: |
| 1489 | // FIXME: implement #line |
| 1490 | DiscardUntilEndOfDirective(); |
| 1491 | return; |
| 1492 | |
| 1493 | // C99 6.10.5 - Error Directive. |
| 1494 | case tok::pp_error: |
| 1495 | return HandleUserDiagnosticDirective(Result, false); |
| 1496 | |
| 1497 | // C99 6.10.6 - Pragma Directive. |
| 1498 | case tok::pp_pragma: |
| 1499 | return HandlePragmaDirective(); |
| 1500 | |
| 1501 | // GNU Extensions. |
| 1502 | case tok::pp_import: |
| 1503 | return HandleImportDirective(Result); |
| 1504 | case tok::pp_include_next: |
| 1505 | return HandleIncludeNextDirective(Result); |
| 1506 | |
| 1507 | case tok::pp_warning: |
| 1508 | Diag(Result, diag::ext_pp_warning_directive); |
| 1509 | return HandleUserDiagnosticDirective(Result, true); |
| 1510 | case tok::pp_ident: |
| 1511 | return HandleIdentSCCSDirective(Result); |
| 1512 | case tok::pp_sccs: |
| 1513 | return HandleIdentSCCSDirective(Result); |
| 1514 | case tok::pp_assert: |
| 1515 | //isExtension = true; // FIXME: implement #assert |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1516 | break; |
Chris Lattner | 87d3bec | 2006-10-17 03:44:32 +0000 | [diff] [blame] | 1517 | case tok::pp_unassert: |
| 1518 | //isExtension = true; // FIXME: implement #unassert |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1519 | break; |
Chris Lattner | 87d3bec | 2006-10-17 03:44:32 +0000 | [diff] [blame] | 1520 | |
| 1521 | // clang extensions. |
| 1522 | case tok::pp_define_target: |
| 1523 | return HandleDefineDirective(Result, true); |
| 1524 | case tok::pp_define_other_target: |
| 1525 | return HandleDefineOtherTargetDirective(Result); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1526 | } |
| 1527 | break; |
| 1528 | } |
| 1529 | |
| 1530 | // If we reached here, the preprocessing token is not valid! |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1531 | Diag(Result, diag::err_pp_invalid_directive); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1532 | |
| 1533 | // Read the rest of the PP line. |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1534 | DiscardUntilEndOfDirective(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1535 | |
| 1536 | // Okay, we're done parsing the directive. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Chris Lattner | 01d66cc | 2006-07-03 22:16:27 +0000 | [diff] [blame] | 1539 | void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Tok, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1540 | bool isWarning) { |
| 1541 | // Read the rest of the line raw. We do this because we don't want macros |
| 1542 | // to be expanded and we don't require that the tokens be valid preprocessing |
| 1543 | // tokens. For example, this is allowed: "#warning ` 'foo". GCC does |
| 1544 | // collapse multiple consequtive white space between tokens, but this isn't |
| 1545 | // specified by the standard. |
| 1546 | std::string Message = CurLexer->ReadToEndOfLine(); |
| 1547 | |
| 1548 | unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error; |
Chris Lattner | 01d66cc | 2006-07-03 22:16:27 +0000 | [diff] [blame] | 1549 | return Diag(Tok, DiagID, Message); |
| 1550 | } |
| 1551 | |
| 1552 | /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive. |
| 1553 | /// |
| 1554 | void Preprocessor::HandleIdentSCCSDirective(LexerToken &Tok) { |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1555 | // Yes, this directive is an extension. |
Chris Lattner | 01d66cc | 2006-07-03 22:16:27 +0000 | [diff] [blame] | 1556 | Diag(Tok, diag::ext_pp_ident_directive); |
| 1557 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1558 | // Read the string argument. |
Chris Lattner | 01d66cc | 2006-07-03 22:16:27 +0000 | [diff] [blame] | 1559 | LexerToken StrTok; |
| 1560 | Lex(StrTok); |
| 1561 | |
| 1562 | // If the token kind isn't a string, it's a malformed directive. |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 1563 | if (StrTok.getKind() != tok::string_literal && |
| 1564 | StrTok.getKind() != tok::wide_string_literal) |
Chris Lattner | 01d66cc | 2006-07-03 22:16:27 +0000 | [diff] [blame] | 1565 | return Diag(StrTok, diag::err_pp_malformed_ident); |
| 1566 | |
| 1567 | // Verify that there is nothing after the string, other than EOM. |
| 1568 | CheckEndOfDirective("#ident"); |
| 1569 | |
Chris Lattner | b8d6d5a | 2006-11-21 04:09:30 +0000 | [diff] [blame] | 1570 | if (Callbacks) |
| 1571 | Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok)); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1574 | //===----------------------------------------------------------------------===// |
| 1575 | // Preprocessor Include Directive Handling. |
| 1576 | //===----------------------------------------------------------------------===// |
| 1577 | |
Chris Lattner | c07ba1f | 2006-10-30 05:58:32 +0000 | [diff] [blame] | 1578 | /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully |
| 1579 | /// checked and spelled filename, e.g. as an operand of #include. This returns |
| 1580 | /// true if the input filename was in <>'s or false if it were in ""'s. The |
| 1581 | /// caller is expected to provide a buffer that is large enough to hold the |
| 1582 | /// spelling of the filename, but is also expected to handle the case when |
| 1583 | /// this method decides to use a different buffer. |
| 1584 | bool Preprocessor::GetIncludeFilenameSpelling(const LexerToken &FilenameTok, |
| 1585 | const char *&BufStart, |
| 1586 | const char *&BufEnd) { |
| 1587 | // Get the text form of the filename. |
| 1588 | unsigned Len = getSpelling(FilenameTok, BufStart); |
| 1589 | BufEnd = BufStart+Len; |
| 1590 | assert(BufStart != BufEnd && "Can't have tokens with empty spellings!"); |
| 1591 | |
| 1592 | // Make sure the filename is <x> or "x". |
| 1593 | bool isAngled; |
| 1594 | if (BufStart[0] == '<') { |
| 1595 | if (BufEnd[-1] != '>') { |
| 1596 | Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); |
| 1597 | BufStart = 0; |
| 1598 | return true; |
| 1599 | } |
| 1600 | isAngled = true; |
| 1601 | } else if (BufStart[0] == '"') { |
| 1602 | if (BufEnd[-1] != '"') { |
| 1603 | Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); |
| 1604 | BufStart = 0; |
| 1605 | return true; |
| 1606 | } |
| 1607 | isAngled = false; |
| 1608 | } else { |
| 1609 | Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); |
| 1610 | BufStart = 0; |
| 1611 | return true; |
| 1612 | } |
| 1613 | |
| 1614 | // Diagnose #include "" as invalid. |
| 1615 | if (BufEnd-BufStart <= 2) { |
| 1616 | Diag(FilenameTok.getLocation(), diag::err_pp_empty_filename); |
| 1617 | BufStart = 0; |
| 1618 | return ""; |
| 1619 | } |
| 1620 | |
| 1621 | // Skip the brackets. |
| 1622 | ++BufStart; |
| 1623 | --BufEnd; |
| 1624 | return isAngled; |
| 1625 | } |
| 1626 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1627 | /// HandleIncludeDirective - The "#include" tokens have just been read, read the |
| 1628 | /// file to be included from the lexer, then include it! This is a common |
| 1629 | /// routine with functionality shared between #include, #include_next and |
| 1630 | /// #import. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1631 | void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1632 | const DirectoryLookup *LookupFrom, |
| 1633 | bool isImport) { |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1634 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1635 | LexerToken FilenameTok; |
Chris Lattner | c07ba1f | 2006-10-30 05:58:32 +0000 | [diff] [blame] | 1636 | CurLexer->LexIncludeFilename(FilenameTok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1637 | |
| 1638 | // If the token kind is EOM, the error has already been diagnosed. |
| 1639 | if (FilenameTok.getKind() == tok::eom) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1640 | return; |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 1641 | |
Chris Lattner | c07ba1f | 2006-10-30 05:58:32 +0000 | [diff] [blame] | 1642 | // Reserve a buffer to get the spelling. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1643 | llvm::SmallVector<char, 128> FilenameBuffer; |
Chris Lattner | c07ba1f | 2006-10-30 05:58:32 +0000 | [diff] [blame] | 1644 | FilenameBuffer.resize(FilenameTok.getLength()); |
| 1645 | |
| 1646 | const char *FilenameStart = &FilenameBuffer[0], *FilenameEnd; |
| 1647 | bool isAngled = GetIncludeFilenameSpelling(FilenameTok, |
| 1648 | FilenameStart, FilenameEnd); |
| 1649 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 1650 | // error. |
| 1651 | if (FilenameStart == 0) |
| 1652 | return; |
| 1653 | |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 1654 | // Verify that there is nothing after the filename, other than EOM. Use the |
| 1655 | // preprocessor to lex this in case lexing the filename entered a macro. |
| 1656 | CheckEndOfDirective("#include"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1657 | |
| 1658 | // Check that we don't have infinite #include recursion. |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 1659 | if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1660 | return Diag(FilenameTok, diag::err_pp_include_too_deep); |
| 1661 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1662 | // Search include directories. |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1663 | const DirectoryLookup *CurDir; |
Chris Lattner | c07ba1f | 2006-10-30 05:58:32 +0000 | [diff] [blame] | 1664 | const FileEntry *File = LookupFile(FilenameStart, FilenameEnd, |
Chris Lattner | b8b94f1 | 2006-10-30 05:38:06 +0000 | [diff] [blame] | 1665 | isAngled, LookupFrom, CurDir); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1666 | if (File == 0) |
Chris Lattner | 7c718bd | 2007-04-10 06:02:46 +0000 | [diff] [blame] | 1667 | return Diag(FilenameTok, diag::err_pp_file_not_found, |
| 1668 | std::string(FilenameStart, FilenameEnd)); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1669 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1670 | // Ask HeaderInfo if we should enter this #include file. |
| 1671 | if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) { |
| 1672 | // If it returns true, #including this file will have no effect. |
Chris Lattner | 3665f16 | 2006-07-04 07:26:10 +0000 | [diff] [blame] | 1673 | return; |
| 1674 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1675 | |
| 1676 | // Look up the file, create a File ID for it. |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1677 | unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation()); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1678 | if (FileID == 0) |
Chris Lattner | 7c718bd | 2007-04-10 06:02:46 +0000 | [diff] [blame] | 1679 | return Diag(FilenameTok, diag::err_pp_file_not_found, |
| 1680 | std::string(FilenameStart, FilenameEnd)); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1681 | |
| 1682 | // Finally, if all is good, enter the new file! |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1683 | EnterSourceFile(FileID, CurDir); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | /// HandleIncludeNextDirective - Implements #include_next. |
| 1687 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1688 | void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) { |
| 1689 | Diag(IncludeNextTok, diag::ext_pp_include_next_directive); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1690 | |
| 1691 | // #include_next is like #include, except that we start searching after |
| 1692 | // the current found directory. If we can't do this, issue a |
| 1693 | // diagnostic. |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1694 | const DirectoryLookup *Lookup = CurDirLookup; |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 1695 | if (isInPrimaryFile()) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1696 | Lookup = 0; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1697 | Diag(IncludeNextTok, diag::pp_include_next_in_primary); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1698 | } else if (Lookup == 0) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1699 | Diag(IncludeNextTok, diag::pp_include_next_absolute_path); |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1700 | } else { |
| 1701 | // Start looking up in the next directory. |
| 1702 | ++Lookup; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | return HandleIncludeDirective(IncludeNextTok, Lookup); |
| 1706 | } |
| 1707 | |
| 1708 | /// HandleImportDirective - Implements #import. |
| 1709 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1710 | void Preprocessor::HandleImportDirective(LexerToken &ImportTok) { |
| 1711 | Diag(ImportTok, diag::ext_pp_import_directive); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1712 | |
| 1713 | return HandleIncludeDirective(ImportTok, 0, true); |
| 1714 | } |
| 1715 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1716 | //===----------------------------------------------------------------------===// |
| 1717 | // Preprocessor Macro Directive Handling. |
| 1718 | //===----------------------------------------------------------------------===// |
| 1719 | |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1720 | /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro |
| 1721 | /// definition has just been read. Lex the rest of the arguments and the |
| 1722 | /// closing ), updating MI with what we learn. Return true if an error occurs |
| 1723 | /// parsing the arg list. |
| 1724 | bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) { |
Chris Lattner | 564f478 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 1725 | llvm::SmallVector<IdentifierInfo*, 32> Arguments; |
| 1726 | |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1727 | LexerToken Tok; |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1728 | while (1) { |
| 1729 | LexUnexpandedToken(Tok); |
| 1730 | switch (Tok.getKind()) { |
| 1731 | case tok::r_paren: |
| 1732 | // Found the end of the argument list. |
Chris Lattner | 564f478 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 1733 | if (Arguments.empty()) { // #define FOO() |
| 1734 | MI->setArgumentList(Arguments.begin(), Arguments.end()); |
| 1735 | return false; |
| 1736 | } |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1737 | // Otherwise we have #define FOO(A,) |
| 1738 | Diag(Tok, diag::err_pp_expected_ident_in_arg_list); |
| 1739 | return true; |
| 1740 | case tok::ellipsis: // #define X(... -> C99 varargs |
| 1741 | // Warn if use of C99 feature in non-C99 mode. |
| 1742 | if (!Features.C99) Diag(Tok, diag::ext_variadic_macro); |
| 1743 | |
| 1744 | // Lex the token after the identifier. |
| 1745 | LexUnexpandedToken(Tok); |
| 1746 | if (Tok.getKind() != tok::r_paren) { |
| 1747 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1748 | return true; |
| 1749 | } |
Chris Lattner | 95a06b3 | 2006-07-30 08:40:43 +0000 | [diff] [blame] | 1750 | // Add the __VA_ARGS__ identifier as an argument. |
Chris Lattner | 564f478 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 1751 | Arguments.push_back(Ident__VA_ARGS__); |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1752 | MI->setIsC99Varargs(); |
Chris Lattner | 564f478 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 1753 | MI->setArgumentList(Arguments.begin(), Arguments.end()); |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1754 | return false; |
| 1755 | case tok::eom: // #define X( |
| 1756 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1757 | return true; |
Chris Lattner | 62aa0d4 | 2006-10-20 05:08:24 +0000 | [diff] [blame] | 1758 | default: |
| 1759 | // Handle keywords and identifiers here to accept things like |
| 1760 | // #define Foo(for) for. |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1761 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Chris Lattner | 62aa0d4 | 2006-10-20 05:08:24 +0000 | [diff] [blame] | 1762 | if (II == 0) { |
| 1763 | // #define X(1 |
| 1764 | Diag(Tok, diag::err_pp_invalid_tok_in_arg_list); |
| 1765 | return true; |
| 1766 | } |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1767 | |
| 1768 | // If this is already used as an argument, it is used multiple times (e.g. |
| 1769 | // #define X(A,A. |
Chris Lattner | 564f478 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 1770 | if (std::find(Arguments.begin(), Arguments.end(), II) != |
| 1771 | Arguments.end()) { // C99 6.10.3p6 |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1772 | Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName()); |
| 1773 | return true; |
| 1774 | } |
| 1775 | |
| 1776 | // Add the argument to the macro info. |
Chris Lattner | 564f478 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 1777 | Arguments.push_back(II); |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1778 | |
| 1779 | // Lex the token after the identifier. |
| 1780 | LexUnexpandedToken(Tok); |
| 1781 | |
| 1782 | switch (Tok.getKind()) { |
| 1783 | default: // #define X(A B |
| 1784 | Diag(Tok, diag::err_pp_expected_comma_in_arg_list); |
| 1785 | return true; |
| 1786 | case tok::r_paren: // #define X(A) |
Chris Lattner | 564f478 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 1787 | MI->setArgumentList(Arguments.begin(), Arguments.end()); |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1788 | return false; |
| 1789 | case tok::comma: // #define X(A, |
| 1790 | break; |
| 1791 | case tok::ellipsis: // #define X(A... -> GCC extension |
| 1792 | // Diagnose extension. |
| 1793 | Diag(Tok, diag::ext_named_variadic_macro); |
| 1794 | |
| 1795 | // Lex the token after the identifier. |
| 1796 | LexUnexpandedToken(Tok); |
| 1797 | if (Tok.getKind() != tok::r_paren) { |
| 1798 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1799 | return true; |
| 1800 | } |
| 1801 | |
| 1802 | MI->setIsGNUVarargs(); |
Chris Lattner | 564f478 | 2007-07-14 22:46:43 +0000 | [diff] [blame] | 1803 | MI->setArgumentList(Arguments.begin(), Arguments.end()); |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1804 | return false; |
| 1805 | } |
| 1806 | } |
| 1807 | } |
| 1808 | } |
| 1809 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1810 | /// HandleDefineDirective - Implements #define. This consumes the entire macro |
Chris Lattner | 81278c6 | 2006-10-14 19:03:49 +0000 | [diff] [blame] | 1811 | /// line then lets the caller lex the next real token. If 'isTargetSpecific' is |
| 1812 | /// true, then this is a "#define_target", otherwise this is a "#define". |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1813 | /// |
Chris Lattner | 81278c6 | 2006-10-14 19:03:49 +0000 | [diff] [blame] | 1814 | void Preprocessor::HandleDefineDirective(LexerToken &DefineTok, |
| 1815 | bool isTargetSpecific) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1816 | ++NumDefined; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1817 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1818 | LexerToken MacroNameTok; |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 1819 | ReadMacroName(MacroNameTok, 1); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1820 | |
| 1821 | // Error reading macro name? If so, diagnostic already issued. |
| 1822 | if (MacroNameTok.getKind() == tok::eom) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1823 | return; |
Chris Lattner | f40fe99 | 2007-07-14 22:11:41 +0000 | [diff] [blame] | 1824 | |
Chris Lattner | 457fc15 | 2006-07-29 06:30:25 +0000 | [diff] [blame] | 1825 | // If we are supposed to keep comments in #defines, reenable comment saving |
| 1826 | // mode. |
Chris Lattner | b352e3e | 2006-11-21 06:17:10 +0000 | [diff] [blame] | 1827 | CurLexer->KeepCommentMode = KeepMacroComments; |
Chris Lattner | 457fc15 | 2006-07-29 06:30:25 +0000 | [diff] [blame] | 1828 | |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 1829 | // Create the new macro. |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 1830 | MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation()); |
Chris Lattner | 81278c6 | 2006-10-14 19:03:49 +0000 | [diff] [blame] | 1831 | if (isTargetSpecific) MI->setIsTargetSpecific(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1832 | |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 1833 | // If the identifier is an 'other target' macro, clear this bit. |
| 1834 | MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false); |
| 1835 | |
| 1836 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1837 | LexerToken Tok; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1838 | LexUnexpandedToken(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1839 | |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1840 | // If this is a function-like macro definition, parse the argument list, |
| 1841 | // marking each of the identifiers as being used as macro arguments. Also, |
| 1842 | // check other constraints on the first token of the macro body. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1843 | if (Tok.getKind() == tok::eom) { |
| 1844 | // If there is no body to this macro, we have no special handling here. |
| 1845 | } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) { |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1846 | // This is a function-like macro definition. Read the argument list. |
| 1847 | MI->setIsFunctionLike(); |
| 1848 | if (ReadMacroDefinitionArgList(MI)) { |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1849 | // Forget about MI. |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1850 | delete MI; |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1851 | // Throw away the rest of the line. |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1852 | if (CurLexer->ParsingPreprocessorDirective) |
| 1853 | DiscardUntilEndOfDirective(); |
| 1854 | return; |
| 1855 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1856 | |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1857 | // Read the first token after the arg list for down below. |
| 1858 | LexUnexpandedToken(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1859 | } else if (!Tok.hasLeadingSpace()) { |
| 1860 | // C99 requires whitespace between the macro definition and the body. Emit |
| 1861 | // a diagnostic for something like "#define X+". |
| 1862 | if (Features.C99) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1863 | Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1864 | } else { |
| 1865 | // FIXME: C90/C++ do not get this diagnostic, but it does get a similar |
| 1866 | // one in some cases! |
| 1867 | } |
| 1868 | } else { |
| 1869 | // This is a normal token with leading space. Clear the leading space |
| 1870 | // marker on the first token to get proper expansion. |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 1871 | Tok.clearFlag(LexerToken::LeadingSpace); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
Chris Lattner | 7e37483 | 2006-07-29 03:46:57 +0000 | [diff] [blame] | 1874 | // If this is a definition of a variadic C99 function-like macro, not using |
| 1875 | // the GNU named varargs extension, enabled __VA_ARGS__. |
| 1876 | |
| 1877 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 1878 | // This gets unpoisoned where it is allowed. |
| 1879 | assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!"); |
| 1880 | if (MI->isC99Varargs()) |
| 1881 | Ident__VA_ARGS__->setIsPoisoned(false); |
| 1882 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1883 | // Read the rest of the macro body. |
Chris Lattner | a383434 | 2007-07-14 21:54:03 +0000 | [diff] [blame] | 1884 | if (MI->isObjectLike()) { |
| 1885 | // Object-like macros are very simple, just read their body. |
| 1886 | while (Tok.getKind() != tok::eom) { |
| 1887 | MI->AddTokenToBody(Tok); |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1888 | // Get the next token of the macro. |
| 1889 | LexUnexpandedToken(Tok); |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1890 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1891 | |
Chris Lattner | a383434 | 2007-07-14 21:54:03 +0000 | [diff] [blame] | 1892 | } else { |
| 1893 | // Otherwise, read the body of a function-like macro. This has to validate |
| 1894 | // the # (stringize) operator. |
| 1895 | while (Tok.getKind() != tok::eom) { |
| 1896 | MI->AddTokenToBody(Tok); |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1897 | |
Chris Lattner | a383434 | 2007-07-14 21:54:03 +0000 | [diff] [blame] | 1898 | // Check C99 6.10.3.2p1: ensure that # operators are followed by macro |
| 1899 | // parameters in function-like macro expansions. |
| 1900 | if (Tok.getKind() != tok::hash) { |
| 1901 | // Get the next token of the macro. |
| 1902 | LexUnexpandedToken(Tok); |
| 1903 | continue; |
| 1904 | } |
| 1905 | |
| 1906 | // Get the next token of the macro. |
| 1907 | LexUnexpandedToken(Tok); |
| 1908 | |
| 1909 | // Not a macro arg identifier? |
| 1910 | if (!Tok.getIdentifierInfo() || |
| 1911 | MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) { |
| 1912 | Diag(Tok, diag::err_pp_stringize_not_parameter); |
| 1913 | delete MI; |
| 1914 | |
| 1915 | // Disable __VA_ARGS__ again. |
| 1916 | Ident__VA_ARGS__->setIsPoisoned(true); |
| 1917 | return; |
| 1918 | } |
| 1919 | |
| 1920 | // Things look ok, add the param name token to the macro. |
| 1921 | MI->AddTokenToBody(Tok); |
| 1922 | |
| 1923 | // Get the next token of the macro. |
| 1924 | LexUnexpandedToken(Tok); |
| 1925 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1926 | } |
Chris Lattner | 7e37483 | 2006-07-29 03:46:57 +0000 | [diff] [blame] | 1927 | |
Chris Lattner | f40fe99 | 2007-07-14 22:11:41 +0000 | [diff] [blame] | 1928 | |
Chris Lattner | 7e37483 | 2006-07-29 03:46:57 +0000 | [diff] [blame] | 1929 | // Disable __VA_ARGS__ again. |
| 1930 | Ident__VA_ARGS__->setIsPoisoned(true); |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1931 | |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1932 | // Check that there is no paste (##) operator at the begining or end of the |
| 1933 | // replacement list. |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 1934 | unsigned NumTokens = MI->getNumTokens(); |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1935 | if (NumTokens != 0) { |
| 1936 | if (MI->getReplacementToken(0).getKind() == tok::hashhash) { |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1937 | Diag(MI->getReplacementToken(0), diag::err_paste_at_start); |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1938 | delete MI; |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1939 | return; |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1940 | } |
| 1941 | if (MI->getReplacementToken(NumTokens-1).getKind() == tok::hashhash) { |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1942 | Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end); |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1943 | delete MI; |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1944 | return; |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1945 | } |
| 1946 | } |
| 1947 | |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 1948 | // If this is the primary source file, remember that this macro hasn't been |
| 1949 | // used yet. |
| 1950 | if (isInPrimaryFile()) |
| 1951 | MI->setIsUsed(false); |
| 1952 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1953 | // Finally, if this identifier already had a macro defined for it, verify that |
| 1954 | // the macro bodies are identical and free the old definition. |
| 1955 | if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) { |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 1956 | if (!OtherMI->isUsed()) |
| 1957 | Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used); |
| 1958 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1959 | // Macros must be identical. This means all tokes and whitespace separation |
Chris Lattner | 21284df | 2006-07-08 07:16:08 +0000 | [diff] [blame] | 1960 | // must be the same. C99 6.10.3.2. |
| 1961 | if (!MI->isIdenticalTo(*OtherMI, *this)) { |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 1962 | Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef, |
| 1963 | MacroNameTok.getIdentifierInfo()->getName()); |
| 1964 | Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2); |
| 1965 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1966 | delete OtherMI; |
| 1967 | } |
| 1968 | |
| 1969 | MacroNameTok.getIdentifierInfo()->setMacroInfo(MI); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 1972 | /// HandleDefineOtherTargetDirective - Implements #define_other_target. |
| 1973 | void Preprocessor::HandleDefineOtherTargetDirective(LexerToken &Tok) { |
| 1974 | LexerToken MacroNameTok; |
| 1975 | ReadMacroName(MacroNameTok, 1); |
| 1976 | |
| 1977 | // Error reading macro name? If so, diagnostic already issued. |
| 1978 | if (MacroNameTok.getKind() == tok::eom) |
| 1979 | return; |
| 1980 | |
| 1981 | // Check to see if this is the last token on the #undef line. |
| 1982 | CheckEndOfDirective("#define_other_target"); |
| 1983 | |
| 1984 | // If there is already a macro defined by this name, turn it into a |
| 1985 | // target-specific define. |
| 1986 | if (MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) { |
| 1987 | MI->setIsTargetSpecific(true); |
| 1988 | return; |
| 1989 | } |
| 1990 | |
| 1991 | // Mark the identifier as being a macro on some other target. |
| 1992 | MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(); |
| 1993 | } |
| 1994 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1995 | |
| 1996 | /// HandleUndefDirective - Implements #undef. |
| 1997 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1998 | void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1999 | ++NumUndefined; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 2000 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2001 | LexerToken MacroNameTok; |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 2002 | ReadMacroName(MacroNameTok, 2); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2003 | |
| 2004 | // Error reading macro name? If so, diagnostic already issued. |
| 2005 | if (MacroNameTok.getKind() == tok::eom) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 2006 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2007 | |
| 2008 | // Check to see if this is the last token on the #undef line. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 2009 | CheckEndOfDirective("#undef"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2010 | |
| 2011 | // Okay, we finally have a valid identifier to undef. |
| 2012 | MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo(); |
| 2013 | |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 2014 | // #undef untaints an identifier if it were marked by define_other_target. |
| 2015 | MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false); |
| 2016 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2017 | // If the macro is not defined, this is a noop undef, just return. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 2018 | if (MI == 0) return; |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 2019 | |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 2020 | if (!MI->isUsed()) |
| 2021 | Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2022 | |
| 2023 | // Free macro definition. |
| 2024 | delete MI; |
| 2025 | MacroNameTok.getIdentifierInfo()->setMacroInfo(0); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 2029 | //===----------------------------------------------------------------------===// |
| 2030 | // Preprocessor Conditional Directive Handling. |
| 2031 | //===----------------------------------------------------------------------===// |
| 2032 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2033 | /// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 2034 | /// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true |
| 2035 | /// if any tokens have been returned or pp-directives activated before this |
| 2036 | /// #ifndef has been lexed. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2037 | /// |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 2038 | void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef, |
| 2039 | bool ReadAnyTokensBeforeDirective) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2040 | ++NumIf; |
| 2041 | LexerToken DirectiveTok = Result; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 2042 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2043 | LexerToken MacroNameTok; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 2044 | ReadMacroName(MacroNameTok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2045 | |
| 2046 | // Error reading macro name? If so, diagnostic already issued. |
| 2047 | if (MacroNameTok.getKind() == tok::eom) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 2048 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2049 | |
| 2050 | // Check to see if this is the last token on the #if[n]def line. |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 2051 | CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef"); |
| 2052 | |
| 2053 | // If the start of a top-level #ifdef, inform MIOpt. |
| 2054 | if (!ReadAnyTokensBeforeDirective && |
| 2055 | CurLexer->getConditionalStackDepth() == 0) { |
| 2056 | assert(isIfndef && "#ifdef shouldn't reach here"); |
| 2057 | CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo()); |
| 2058 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2059 | |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 2060 | IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); |
| 2061 | MacroInfo *MI = MII->getMacroInfo(); |
Chris Lattner | a78a97e | 2006-07-03 05:42:18 +0000 | [diff] [blame] | 2062 | |
Chris Lattner | 81278c6 | 2006-10-14 19:03:49 +0000 | [diff] [blame] | 2063 | // If there is a macro, process it. |
| 2064 | if (MI) { |
| 2065 | // Mark it used. |
| 2066 | MI->setIsUsed(true); |
| 2067 | |
| 2068 | // If this is the first use of a target-specific macro, warn about it. |
| 2069 | if (MI->isTargetSpecific()) { |
| 2070 | MI->setIsTargetSpecific(false); // Don't warn on second use. |
| 2071 | getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(), |
| 2072 | diag::port_target_macro_use); |
| 2073 | } |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 2074 | } else { |
| 2075 | // Use of a target-specific macro for some other target? If so, warn. |
| 2076 | if (MII->isOtherTargetMacro()) { |
| 2077 | MII->setIsOtherTargetMacro(false); // Don't warn on second use. |
| 2078 | getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(), |
| 2079 | diag::port_target_macro_use); |
| 2080 | } |
Chris Lattner | 81278c6 | 2006-10-14 19:03:49 +0000 | [diff] [blame] | 2081 | } |
Chris Lattner | a78a97e | 2006-07-03 05:42:18 +0000 | [diff] [blame] | 2082 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2083 | // Should we include the stuff contained by this directive? |
Chris Lattner | a78a97e | 2006-07-03 05:42:18 +0000 | [diff] [blame] | 2084 | if (!MI == isIfndef) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2085 | // Yes, remember that we are inside a conditional, then lex the next token. |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 2086 | CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2087 | /*foundnonskip*/true, /*foundelse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2088 | } else { |
| 2089 | // No, skip the contents of this block and return the first token after it. |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 2090 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 2091 | /*Foundnonskip*/false, |
| 2092 | /*FoundElse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | /// HandleIfDirective - Implements the #if directive. |
| 2097 | /// |
Chris Lattner | a8654ca | 2006-07-04 17:42:08 +0000 | [diff] [blame] | 2098 | void Preprocessor::HandleIfDirective(LexerToken &IfToken, |
| 2099 | bool ReadAnyTokensBeforeDirective) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2100 | ++NumIf; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 2101 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 2102 | // Parse and evaluation the conditional expression. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 2103 | IdentifierInfo *IfNDefMacro = 0; |
Chris Lattner | a8654ca | 2006-07-04 17:42:08 +0000 | [diff] [blame] | 2104 | bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2105 | |
| 2106 | // Should we include the stuff contained by this directive? |
| 2107 | if (ConditionalTrue) { |
Chris Lattner | a8654ca | 2006-07-04 17:42:08 +0000 | [diff] [blame] | 2108 | // If this condition is equivalent to #ifndef X, and if this is the first |
| 2109 | // directive seen, handle it for the multiple-include optimization. |
| 2110 | if (!ReadAnyTokensBeforeDirective && |
| 2111 | CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro) |
| 2112 | CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro); |
| 2113 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2114 | // Yes, remember that we are inside a conditional, then lex the next token. |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 2115 | CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2116 | /*foundnonskip*/true, /*foundelse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2117 | } else { |
| 2118 | // No, skip the contents of this block and return the first token after it. |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 2119 | SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false, |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 2120 | /*FoundElse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2121 | } |
| 2122 | } |
| 2123 | |
| 2124 | /// HandleEndifDirective - Implements the #endif directive. |
| 2125 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 2126 | void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2127 | ++NumEndif; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 2128 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2129 | // Check that this is the whole directive. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 2130 | CheckEndOfDirective("#endif"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2131 | |
| 2132 | PPConditionalInfo CondInfo; |
| 2133 | if (CurLexer->popConditionalLevel(CondInfo)) { |
| 2134 | // No conditionals on the stack: this is an #endif without an #if. |
| 2135 | return Diag(EndifToken, diag::err_pp_endif_without_if); |
| 2136 | } |
| 2137 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 2138 | // If this the end of a top-level #endif, inform MIOpt. |
| 2139 | if (CurLexer->getConditionalStackDepth() == 0) |
| 2140 | CurLexer->MIOpt.ExitTopLevelConditional(); |
| 2141 | |
Chris Lattner | 538d7f3 | 2006-07-20 04:31:52 +0000 | [diff] [blame] | 2142 | assert(!CondInfo.WasSkipping && !CurLexer->LexingRawMode && |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2143 | "This code should only be reachable in the non-skipping case!"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 2147 | void Preprocessor::HandleElseDirective(LexerToken &Result) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2148 | ++NumElse; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 2149 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2150 | // #else directive in a non-skipping conditional... start skipping. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 2151 | CheckEndOfDirective("#else"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2152 | |
| 2153 | PPConditionalInfo CI; |
| 2154 | if (CurLexer->popConditionalLevel(CI)) |
| 2155 | return Diag(Result, diag::pp_err_else_without_if); |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 2156 | |
| 2157 | // If this is a top-level #else, inform the MIOpt. |
| 2158 | if (CurLexer->getConditionalStackDepth() == 0) |
| 2159 | CurLexer->MIOpt.FoundTopLevelElse(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2160 | |
| 2161 | // If this is a #else with a #else before it, report the error. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 2162 | if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2163 | |
| 2164 | // Finally, skip the rest of the contents of this block and return the first |
| 2165 | // token after it. |
| 2166 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 2167 | /*FoundElse*/true); |
| 2168 | } |
| 2169 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 2170 | void Preprocessor::HandleElifDirective(LexerToken &ElifToken) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2171 | ++NumElse; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 2172 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2173 | // #elif directive in a non-skipping conditional... start skipping. |
| 2174 | // We don't care what the condition is, because we will always skip it (since |
| 2175 | // the block immediately before it was included). |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 2176 | DiscardUntilEndOfDirective(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2177 | |
| 2178 | PPConditionalInfo CI; |
| 2179 | if (CurLexer->popConditionalLevel(CI)) |
| 2180 | return Diag(ElifToken, diag::pp_err_elif_without_if); |
| 2181 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 2182 | // If this is a top-level #elif, inform the MIOpt. |
| 2183 | if (CurLexer->getConditionalStackDepth() == 0) |
| 2184 | CurLexer->MIOpt.FoundTopLevelElse(); |
| 2185 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2186 | // If this is a #elif with a #else before it, report the error. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 2187 | if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2188 | |
| 2189 | // Finally, skip the rest of the contents of this block and return the first |
| 2190 | // token after it. |
| 2191 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 2192 | /*FoundElse*/CI.FoundElse); |
| 2193 | } |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 2194 | |