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