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