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 { |
| 885 | struct UnusedIdentifierReporter : public IdentifierVisitor { |
| 886 | Preprocessor &PP; |
| 887 | UnusedIdentifierReporter(Preprocessor &pp) : PP(pp) {} |
| 888 | |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 889 | void VisitIdentifier(IdentifierInfo &II) const { |
| 890 | if (II.getMacroInfo() && !II.getMacroInfo()->isUsed()) |
| 891 | PP.Diag(II.getMacroInfo()->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 892 | } |
| 893 | }; |
| 894 | } |
| 895 | |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 896 | //===----------------------------------------------------------------------===// |
| 897 | // Lexer Event Handling. |
| 898 | //===----------------------------------------------------------------------===// |
| 899 | |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 900 | /// LookUpIdentifierInfo - Given a tok::identifier token, look up the |
| 901 | /// identifier information for the token and install it into the token. |
| 902 | IdentifierInfo *Preprocessor::LookUpIdentifierInfo(LexerToken &Identifier, |
| 903 | const char *BufPtr) { |
| 904 | assert(Identifier.getKind() == tok::identifier && "Not an identifier!"); |
| 905 | assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!"); |
| 906 | |
| 907 | // Look up this token, see if it is a macro, or if it is a language keyword. |
| 908 | IdentifierInfo *II; |
| 909 | if (BufPtr && !Identifier.needsCleaning()) { |
| 910 | // No cleaning needed, just use the characters from the lexed buffer. |
| 911 | II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength()); |
| 912 | } else { |
| 913 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
| 914 | const char *TmpBuf = (char*)alloca(Identifier.getLength()); |
| 915 | unsigned Size = getSpelling(Identifier, TmpBuf); |
| 916 | II = getIdentifierInfo(TmpBuf, TmpBuf+Size); |
| 917 | } |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 918 | Identifier.setIdentifierInfo(II); |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 919 | return II; |
| 920 | } |
| 921 | |
| 922 | |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 923 | /// HandleIdentifier - This callback is invoked when the lexer reads an |
| 924 | /// identifier. This callback looks up the identifier in the map and/or |
| 925 | /// potentially macro expands it or turns it into a named token (like 'for'). |
| 926 | void Preprocessor::HandleIdentifier(LexerToken &Identifier) { |
Chris Lattner | 0f1f505 | 2006-07-20 04:16:23 +0000 | [diff] [blame] | 927 | assert(Identifier.getIdentifierInfo() && |
| 928 | "Can't handle identifiers without identifier info!"); |
| 929 | |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 930 | IdentifierInfo &II = *Identifier.getIdentifierInfo(); |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 931 | |
| 932 | // If this identifier was poisoned, and if it was not produced from a macro |
| 933 | // expansion, emit an error. |
Chris Lattner | 8ff7199 | 2006-07-06 05:17:39 +0000 | [diff] [blame] | 934 | if (II.isPoisoned() && CurLexer) { |
| 935 | if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning. |
| 936 | Diag(Identifier, diag::err_pp_used_poisoned_id); |
| 937 | else |
| 938 | Diag(Identifier, diag::ext_pp_bad_vaargs_use); |
| 939 | } |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 940 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 941 | // If this is a macro to be expanded, do it. |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 942 | if (MacroInfo *MI = II.getMacroInfo()) { |
Chris Lattner | 6e4bf52 | 2006-07-27 06:59:25 +0000 | [diff] [blame] | 943 | if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) { |
| 944 | if (MI->isEnabled()) { |
| 945 | if (!HandleMacroExpandedIdentifier(Identifier, MI)) |
| 946 | return; |
| 947 | } else { |
| 948 | // C99 6.10.3.4p2 says that a disabled macro may never again be |
| 949 | // expanded, even if it's in a context where it could be expanded in the |
| 950 | // future. |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 951 | Identifier.setFlag(LexerToken::DisableExpand); |
Chris Lattner | 6e4bf52 | 2006-07-27 06:59:25 +0000 | [diff] [blame] | 952 | } |
| 953 | } |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 954 | } else if (II.isOtherTargetMacro() && !DisableMacroExpansion) { |
| 955 | // If this identifier is a macro on some other target, emit a diagnostic. |
| 956 | // This diagnosic is only emitted when macro expansion is enabled, because |
| 957 | // the macro would not have been expanded for the other target either. |
| 958 | II.setIsOtherTargetMacro(false); // Don't warn on second use. |
| 959 | getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(), |
| 960 | diag::port_target_macro_use); |
| 961 | |
| 962 | } |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 963 | |
| 964 | // Change the kind of this identifier to the appropriate token kind, e.g. |
| 965 | // turning "for" into a keyword. |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 966 | Identifier.setKind(II.getTokenID()); |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 967 | |
| 968 | // If this is an extension token, diagnose its use. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 969 | if (II.isExtensionToken()) Diag(Identifier, diag::ext_token_used); |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 972 | /// HandleEndOfFile - This callback is invoked when the lexer hits the end of |
| 973 | /// the current file. This either returns the EOF token or pops a level off |
| 974 | /// the include stack and keeps going. |
Chris Lattner | 2183a6e | 2006-07-18 06:36:12 +0000 | [diff] [blame] | 975 | bool Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 976 | assert(!CurMacroExpander && |
| 977 | "Ending a file when currently in a macro!"); |
| 978 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 979 | // See if this file had a controlling macro. |
Chris Lattner | 3665f16 | 2006-07-04 07:26:10 +0000 | [diff] [blame] | 980 | if (CurLexer) { // Not ending a macro, ignore it. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 981 | if (const IdentifierInfo *ControllingMacro = |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 982 | CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) { |
Chris Lattner | 3665f16 | 2006-07-04 07:26:10 +0000 | [diff] [blame] | 983 | // Okay, this has a controlling macro, remember in PerFileInfo. |
| 984 | if (const FileEntry *FE = |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 985 | SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID())) |
| 986 | HeaderInfo.SetFileControllingMacro(FE, ControllingMacro); |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 990 | // If this is a #include'd file, pop it off the include stack and continue |
| 991 | // lexing the #includer file. |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 992 | if (!IncludeMacroStack.empty()) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 993 | // We're done with the #included file. |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 994 | RemoveTopOfLexerStack(); |
Chris Lattner | 0c885f5 | 2006-06-21 06:50:18 +0000 | [diff] [blame] | 995 | |
| 996 | // 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] | 997 | if (FileChangeHandler && !isEndOfMacro && CurLexer) { |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 998 | DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir; |
| 999 | |
| 1000 | // Get the file entry for the current file. |
| 1001 | if (const FileEntry *FE = |
| 1002 | SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID())) |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1003 | FileType = HeaderInfo.getFileDirFlavor(FE); |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1004 | |
Chris Lattner | 0c885f5 | 2006-06-21 06:50:18 +0000 | [diff] [blame] | 1005 | FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr), |
Chris Lattner | 55a6095 | 2006-06-25 04:20:34 +0000 | [diff] [blame] | 1006 | ExitFile, FileType); |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1007 | } |
Chris Lattner | 2183a6e | 2006-07-18 06:36:12 +0000 | [diff] [blame] | 1008 | |
| 1009 | // Client should lex another token. |
| 1010 | return false; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 1013 | Result.startToken(); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 1014 | CurLexer->BufferPtr = CurLexer->BufferEnd; |
| 1015 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd); |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 1016 | Result.setKind(tok::eof); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1017 | |
| 1018 | // We're done with the #included file. |
| 1019 | delete CurLexer; |
| 1020 | CurLexer = 0; |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 1021 | |
Chris Lattner | 03f8348 | 2006-07-10 06:16:26 +0000 | [diff] [blame] | 1022 | // This is the end of the top-level file. If the diag::pp_macro_not_used |
| 1023 | // diagnostic is enabled, walk all of the identifiers, looking for macros that |
| 1024 | // have not been used. |
| 1025 | if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored) |
| 1026 | Identifiers.VisitIdentifiers(UnusedIdentifierReporter(*this)); |
Chris Lattner | 2183a6e | 2006-07-18 06:36:12 +0000 | [diff] [blame] | 1027 | |
| 1028 | return true; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /// HandleEndOfMacro - This callback is invoked when the lexer hits the end of |
Chris Lattner | 7667d0d | 2006-07-16 18:16:58 +0000 | [diff] [blame] | 1032 | /// the current macro expansion or token stream expansion. |
Chris Lattner | 2183a6e | 2006-07-18 06:36:12 +0000 | [diff] [blame] | 1033 | bool Preprocessor::HandleEndOfMacro(LexerToken &Result) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1034 | assert(CurMacroExpander && !CurLexer && |
| 1035 | "Ending a macro when currently in a #include file!"); |
| 1036 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1037 | delete CurMacroExpander; |
| 1038 | |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 1039 | // Handle this like a #include file being popped off the stack. |
| 1040 | CurMacroExpander = 0; |
| 1041 | return HandleEndOfFile(Result, true); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | |
| 1045 | //===----------------------------------------------------------------------===// |
| 1046 | // Utility Methods for Preprocessor Directive Handling. |
| 1047 | //===----------------------------------------------------------------------===// |
| 1048 | |
| 1049 | /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the |
| 1050 | /// current line until the tok::eom token is found. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1051 | void Preprocessor::DiscardUntilEndOfDirective() { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1052 | LexerToken Tmp; |
| 1053 | do { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1054 | LexUnexpandedToken(Tmp); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1055 | } while (Tmp.getKind() != tok::eom); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | /// ReadMacroName - Lex and validate a macro name, which occurs after a |
| 1059 | /// #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] | 1060 | /// of the macro line if the macro name is invalid. isDefineUndef is 1 if |
| 1061 | /// 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] | 1062 | /// else (e.g. #ifdef). |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 1063 | void Preprocessor::ReadMacroName(LexerToken &MacroNameTok, char isDefineUndef) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1064 | // Read the token, don't allow macro expansion on it. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1065 | LexUnexpandedToken(MacroNameTok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1066 | |
| 1067 | // Missing macro name? |
| 1068 | if (MacroNameTok.getKind() == tok::eom) |
| 1069 | return Diag(MacroNameTok, diag::err_pp_missing_macro_name); |
| 1070 | |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 1071 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
| 1072 | if (II == 0) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1073 | Diag(MacroNameTok, diag::err_pp_macro_not_identifier); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1074 | // Fall through on error. |
| 1075 | } else if (0) { |
Chris Lattner | 4d5e1a7 | 2006-07-03 01:01:29 +0000 | [diff] [blame] | 1076 | // FIXME: C++. Error if defining a C++ named operator. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1077 | |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 1078 | } else if (isDefineUndef && II->getName()[0] == 'd' && // defined |
| 1079 | !strcmp(II->getName()+1, "efined")) { |
Chris Lattner | 44f8a66 | 2006-07-03 01:27:27 +0000 | [diff] [blame] | 1080 | // Error if defining "defined": C99 6.10.8.4. |
Chris Lattner | aaf0911 | 2006-07-03 01:17:59 +0000 | [diff] [blame] | 1081 | Diag(MacroNameTok, diag::err_defined_macro_name); |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 1082 | } else if (isDefineUndef && II->getMacroInfo() && |
| 1083 | II->getMacroInfo()->isBuiltinMacro()) { |
Chris Lattner | 44f8a66 | 2006-07-03 01:27:27 +0000 | [diff] [blame] | 1084 | // Error if defining "__LINE__" and other builtins: C99 6.10.8.4. |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 1085 | if (isDefineUndef == 1) |
| 1086 | Diag(MacroNameTok, diag::pp_redef_builtin_macro); |
| 1087 | else |
| 1088 | Diag(MacroNameTok, diag::pp_undef_builtin_macro); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1089 | } else { |
| 1090 | // Okay, we got a good identifier node. Return it. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1091 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1094 | // Invalid macro name, read and discard the rest of the line. Then set the |
| 1095 | // token kind to tok::eom. |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 1096 | MacroNameTok.setKind(tok::eom); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1097 | return DiscardUntilEndOfDirective(); |
| 1098 | } |
| 1099 | |
| 1100 | /// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If |
| 1101 | /// not, emit a diagnostic and consume up until the eom. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1102 | void Preprocessor::CheckEndOfDirective(const char *DirType) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1103 | LexerToken Tmp; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1104 | Lex(Tmp); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1105 | // There should be no tokens after the directive, but we allow them as an |
| 1106 | // extension. |
Chris Lattner | bcb416b | 2006-10-27 05:43:50 +0000 | [diff] [blame] | 1107 | while (Tmp.getKind() == tok::comment) // Skip comments in -C mode. |
| 1108 | Lex(Tmp); |
| 1109 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1110 | if (Tmp.getKind() != tok::eom) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1111 | Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType); |
| 1112 | DiscardUntilEndOfDirective(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1113 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | |
| 1117 | |
| 1118 | /// SkipExcludedConditionalBlock - We just read a #if or related directive and |
| 1119 | /// decided that the subsequent tokens are in the #if'd out portion of the |
| 1120 | /// file. Lex the rest of the file, until we see an #endif. If |
| 1121 | /// FoundNonSkipPortion is true, then we have already emitted code for part of |
| 1122 | /// this #if directive, so #else/#elif blocks should never be entered. If ElseOk |
| 1123 | /// is true, then #else directives are ok, if not, then we have already seen one |
| 1124 | /// so a #else directive is a duplicate. When this returns, the caller can lex |
| 1125 | /// the first valid token. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 1126 | void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1127 | bool FoundNonSkipPortion, |
| 1128 | bool FoundElse) { |
| 1129 | ++NumSkipped; |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 1130 | assert(CurMacroExpander == 0 && CurLexer && |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1131 | "Lexing a macro, not a file?"); |
| 1132 | |
| 1133 | CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false, |
| 1134 | FoundNonSkipPortion, FoundElse); |
| 1135 | |
Chris Lattner | 3ebcf4e | 2006-07-11 05:39:23 +0000 | [diff] [blame] | 1136 | // Enter raw mode to disable identifier lookup (and thus macro expansion), |
| 1137 | // disabling warnings, etc. |
| 1138 | CurLexer->LexingRawMode = true; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1139 | LexerToken Tok; |
| 1140 | while (1) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1141 | CurLexer->Lex(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1142 | |
Chris Lattner | d8aee0e | 2006-07-11 05:04:55 +0000 | [diff] [blame] | 1143 | // If this is the end of the buffer, we have an error. |
| 1144 | if (Tok.getKind() == tok::eof) { |
| 1145 | // Emit errors for each unterminated conditional on the stack, including |
| 1146 | // the current one. |
| 1147 | while (!CurLexer->ConditionalStack.empty()) { |
| 1148 | Diag(CurLexer->ConditionalStack.back().IfLoc, |
| 1149 | diag::err_pp_unterminated_conditional); |
| 1150 | CurLexer->ConditionalStack.pop_back(); |
| 1151 | } |
| 1152 | |
| 1153 | // Just return and let the caller lex after this #include. |
| 1154 | break; |
| 1155 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1156 | |
| 1157 | // If this token is not a preprocessor directive, just skip it. |
| 1158 | if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine()) |
| 1159 | continue; |
| 1160 | |
| 1161 | // We just parsed a # character at the start of a line, so we're in |
| 1162 | // directive mode. Tell the lexer this so any newlines we see will be |
| 1163 | // converted into an EOM token (this terminates the macro). |
| 1164 | CurLexer->ParsingPreprocessorDirective = true; |
Chris Lattner | 457fc15 | 2006-07-29 06:30:25 +0000 | [diff] [blame] | 1165 | CurLexer->KeepCommentMode = false; |
| 1166 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1167 | |
| 1168 | // Read the next token, the directive flavor. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1169 | LexUnexpandedToken(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1170 | |
| 1171 | // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or |
| 1172 | // something bogus), skip it. |
| 1173 | if (Tok.getKind() != tok::identifier) { |
| 1174 | CurLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 457fc15 | 2006-07-29 06:30:25 +0000 | [diff] [blame] | 1175 | // Restore comment saving mode. |
| 1176 | CurLexer->KeepCommentMode = Features.KeepComments; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1177 | continue; |
| 1178 | } |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1179 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1180 | // If the first letter isn't i or e, it isn't intesting to us. We know that |
| 1181 | // this is safe in the face of spelling differences, because there is no way |
| 1182 | // 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] | 1183 | // allows us to avoid looking up the identifier info for #define/#undef and |
| 1184 | // other common directives. |
| 1185 | const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation()); |
| 1186 | char FirstChar = RawCharData[0]; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1187 | if (FirstChar >= 'a' && FirstChar <= 'z' && |
| 1188 | FirstChar != 'i' && FirstChar != 'e') { |
| 1189 | CurLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 457fc15 | 2006-07-29 06:30:25 +0000 | [diff] [blame] | 1190 | // Restore comment saving mode. |
| 1191 | CurLexer->KeepCommentMode = Features.KeepComments; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1192 | continue; |
| 1193 | } |
| 1194 | |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1195 | // Get the identifier name without trigraphs or embedded newlines. Note |
| 1196 | // that we can't use Tok.getIdentifierInfo() because its lookup is disabled |
| 1197 | // when skipping. |
| 1198 | // TODO: could do this with zero copies in the no-clean case by using |
| 1199 | // strncmp below. |
| 1200 | char Directive[20]; |
| 1201 | unsigned IdLen; |
| 1202 | if (!Tok.needsCleaning() && Tok.getLength() < 20) { |
| 1203 | IdLen = Tok.getLength(); |
| 1204 | memcpy(Directive, RawCharData, IdLen); |
| 1205 | Directive[IdLen] = 0; |
| 1206 | } else { |
| 1207 | std::string DirectiveStr = getSpelling(Tok); |
| 1208 | IdLen = DirectiveStr.size(); |
| 1209 | if (IdLen >= 20) { |
| 1210 | CurLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 457fc15 | 2006-07-29 06:30:25 +0000 | [diff] [blame] | 1211 | // Restore comment saving mode. |
| 1212 | CurLexer->KeepCommentMode = Features.KeepComments; |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1213 | continue; |
| 1214 | } |
| 1215 | memcpy(Directive, &DirectiveStr[0], IdLen); |
| 1216 | Directive[IdLen] = 0; |
| 1217 | } |
| 1218 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1219 | if (FirstChar == 'i' && Directive[1] == 'f') { |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1220 | if ((IdLen == 2) || // "if" |
| 1221 | (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef" |
| 1222 | (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1223 | // We know the entire #if/#ifdef/#ifndef block will be skipped, don't |
| 1224 | // bother parsing the condition. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1225 | DiscardUntilEndOfDirective(); |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 1226 | CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true, |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 1227 | /*foundnonskip*/false, |
| 1228 | /*fnddelse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1229 | } |
| 1230 | } else if (FirstChar == 'e') { |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1231 | if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif" |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1232 | CheckEndOfDirective("#endif"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1233 | PPConditionalInfo CondInfo; |
| 1234 | CondInfo.WasSkipping = true; // Silence bogus warning. |
| 1235 | bool InCond = CurLexer->popConditionalLevel(CondInfo); |
| 1236 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
| 1237 | |
| 1238 | // If we popped the outermost skipping block, we're done skipping! |
| 1239 | if (!CondInfo.WasSkipping) |
| 1240 | break; |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1241 | } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else". |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1242 | // #else directive in a skipping conditional. If not in some other |
| 1243 | // skipping conditional, and if #else hasn't already been seen, enter it |
| 1244 | // as a non-skipping conditional. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1245 | CheckEndOfDirective("#else"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1246 | PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel(); |
| 1247 | |
| 1248 | // 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] | 1249 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1250 | |
| 1251 | // Note that we've seen a #else in this conditional. |
| 1252 | CondInfo.FoundElse = true; |
| 1253 | |
| 1254 | // If the conditional is at the top level, and the #if block wasn't |
| 1255 | // entered, enter the #else block now. |
| 1256 | if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { |
| 1257 | CondInfo.FoundNonSkip = true; |
| 1258 | break; |
| 1259 | } |
Chris Lattner | e60165f | 2006-06-22 06:36:29 +0000 | [diff] [blame] | 1260 | } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif". |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1261 | PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel(); |
| 1262 | |
| 1263 | bool ShouldEnter; |
| 1264 | // If this is in a skipping block or if we're already handled this #if |
| 1265 | // block, don't bother parsing the condition. |
| 1266 | if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1267 | DiscardUntilEndOfDirective(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1268 | ShouldEnter = false; |
| 1269 | } else { |
Chris Lattner | 3ebcf4e | 2006-07-11 05:39:23 +0000 | [diff] [blame] | 1270 | // Restore the value of LexingRawMode so that identifiers are |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1271 | // looked up, etc, inside the #elif expression. |
Chris Lattner | 3ebcf4e | 2006-07-11 05:39:23 +0000 | [diff] [blame] | 1272 | assert(CurLexer->LexingRawMode && "We have to be skipping here!"); |
| 1273 | CurLexer->LexingRawMode = false; |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 1274 | IdentifierInfo *IfNDefMacro = 0; |
Chris Lattner | a8654ca | 2006-07-04 17:42:08 +0000 | [diff] [blame] | 1275 | ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); |
Chris Lattner | 3ebcf4e | 2006-07-11 05:39:23 +0000 | [diff] [blame] | 1276 | CurLexer->LexingRawMode = true; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | // 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] | 1280 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1281 | |
| 1282 | // If this condition is true, enter it! |
| 1283 | if (ShouldEnter) { |
| 1284 | CondInfo.FoundNonSkip = true; |
| 1285 | break; |
| 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | CurLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 457fc15 | 2006-07-29 06:30:25 +0000 | [diff] [blame] | 1291 | // Restore comment saving mode. |
| 1292 | CurLexer->KeepCommentMode = Features.KeepComments; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | // Finally, if we are out of the conditional (saw an #endif or ran off the end |
| 1296 | // of the file, just stop skipping and return to lexing whatever came after |
| 1297 | // the #if block. |
Chris Lattner | 3ebcf4e | 2006-07-11 05:39:23 +0000 | [diff] [blame] | 1298 | CurLexer->LexingRawMode = false; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | //===----------------------------------------------------------------------===// |
| 1302 | // Preprocessor Directive Handling. |
| 1303 | //===----------------------------------------------------------------------===// |
| 1304 | |
| 1305 | /// HandleDirective - This callback is invoked when the lexer sees a # token |
| 1306 | /// at the start of a line. This consumes the directive, modifies the |
| 1307 | /// lexer/preprocessor state, and advances the lexer(s) so that the next token |
| 1308 | /// read is the correct one. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1309 | void Preprocessor::HandleDirective(LexerToken &Result) { |
Chris Lattner | 4d5e1a7 | 2006-07-03 01:01:29 +0000 | [diff] [blame] | 1310 | // FIXME: Traditional: # with whitespace before it not recognized by K&R? |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1311 | |
| 1312 | // We just parsed a # character at the start of a line, so we're in directive |
| 1313 | // 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] | 1314 | // EOM token (which terminates the directive). |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1315 | CurLexer->ParsingPreprocessorDirective = true; |
| 1316 | |
| 1317 | ++NumDirectives; |
| 1318 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1319 | // We are about to read a token. For the multiple-include optimization FA to |
| 1320 | // work, we have to remember if we had read any tokens *before* this |
| 1321 | // pp-directive. |
| 1322 | bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal(); |
| 1323 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 1324 | // Read the next token, the directive flavor. This isn't expanded due to |
| 1325 | // C99 6.10.3p8. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1326 | LexUnexpandedToken(Result); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1327 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 1328 | // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.: |
| 1329 | // #define A(x) #x |
| 1330 | // A(abc |
| 1331 | // #warning blah |
| 1332 | // def) |
| 1333 | // 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] | 1334 | if (InMacroArgs) |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 1335 | Diag(Result, diag::ext_embedded_directive); |
| 1336 | |
Chris Lattner | bcb416b | 2006-10-27 05:43:50 +0000 | [diff] [blame] | 1337 | TryAgain: |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1338 | switch (Result.getKind()) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1339 | case tok::eom: |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1340 | return; // null directive. |
Chris Lattner | bcb416b | 2006-10-27 05:43:50 +0000 | [diff] [blame] | 1341 | case tok::comment: |
| 1342 | // Handle stuff like "# /*foo*/ define X" in -E -C mode. |
| 1343 | LexUnexpandedToken(Result); |
| 1344 | goto TryAgain; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1345 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1346 | case tok::numeric_constant: |
| 1347 | // FIXME: implement # 7 line numbers! |
Chris Lattner | 6e5b2a0 | 2006-10-17 02:53:32 +0000 | [diff] [blame] | 1348 | DiscardUntilEndOfDirective(); |
| 1349 | return; |
Chris Lattner | 87d3bec | 2006-10-17 03:44:32 +0000 | [diff] [blame] | 1350 | default: |
| 1351 | IdentifierInfo *II = Result.getIdentifierInfo(); |
| 1352 | if (II == 0) break; // Not an identifier. |
| 1353 | |
| 1354 | // Ask what the preprocessor keyword ID is. |
| 1355 | switch (II->getPPKeywordID()) { |
| 1356 | default: break; |
| 1357 | // C99 6.10.1 - Conditional Inclusion. |
| 1358 | case tok::pp_if: |
| 1359 | return HandleIfDirective(Result, ReadAnyTokensBeforeDirective); |
| 1360 | case tok::pp_ifdef: |
| 1361 | return HandleIfdefDirective(Result, false, true/*not valid for miopt*/); |
| 1362 | case tok::pp_ifndef: |
| 1363 | return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective); |
| 1364 | case tok::pp_elif: |
| 1365 | return HandleElifDirective(Result); |
| 1366 | case tok::pp_else: |
| 1367 | return HandleElseDirective(Result); |
| 1368 | case tok::pp_endif: |
| 1369 | return HandleEndifDirective(Result); |
| 1370 | |
| 1371 | // C99 6.10.2 - Source File Inclusion. |
| 1372 | case tok::pp_include: |
| 1373 | return HandleIncludeDirective(Result); // Handle #include. |
| 1374 | |
| 1375 | // C99 6.10.3 - Macro Replacement. |
| 1376 | case tok::pp_define: |
| 1377 | return HandleDefineDirective(Result, false); |
| 1378 | case tok::pp_undef: |
| 1379 | return HandleUndefDirective(Result); |
| 1380 | |
| 1381 | // C99 6.10.4 - Line Control. |
| 1382 | case tok::pp_line: |
| 1383 | // FIXME: implement #line |
| 1384 | DiscardUntilEndOfDirective(); |
| 1385 | return; |
| 1386 | |
| 1387 | // C99 6.10.5 - Error Directive. |
| 1388 | case tok::pp_error: |
| 1389 | return HandleUserDiagnosticDirective(Result, false); |
| 1390 | |
| 1391 | // C99 6.10.6 - Pragma Directive. |
| 1392 | case tok::pp_pragma: |
| 1393 | return HandlePragmaDirective(); |
| 1394 | |
| 1395 | // GNU Extensions. |
| 1396 | case tok::pp_import: |
| 1397 | return HandleImportDirective(Result); |
| 1398 | case tok::pp_include_next: |
| 1399 | return HandleIncludeNextDirective(Result); |
| 1400 | |
| 1401 | case tok::pp_warning: |
| 1402 | Diag(Result, diag::ext_pp_warning_directive); |
| 1403 | return HandleUserDiagnosticDirective(Result, true); |
| 1404 | case tok::pp_ident: |
| 1405 | return HandleIdentSCCSDirective(Result); |
| 1406 | case tok::pp_sccs: |
| 1407 | return HandleIdentSCCSDirective(Result); |
| 1408 | case tok::pp_assert: |
| 1409 | //isExtension = true; // FIXME: implement #assert |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1410 | break; |
Chris Lattner | 87d3bec | 2006-10-17 03:44:32 +0000 | [diff] [blame] | 1411 | case tok::pp_unassert: |
| 1412 | //isExtension = true; // FIXME: implement #unassert |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1413 | break; |
Chris Lattner | 87d3bec | 2006-10-17 03:44:32 +0000 | [diff] [blame] | 1414 | |
| 1415 | // clang extensions. |
| 1416 | case tok::pp_define_target: |
| 1417 | return HandleDefineDirective(Result, true); |
| 1418 | case tok::pp_define_other_target: |
| 1419 | return HandleDefineOtherTargetDirective(Result); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1420 | } |
| 1421 | break; |
| 1422 | } |
| 1423 | |
| 1424 | // If we reached here, the preprocessing token is not valid! |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1425 | Diag(Result, diag::err_pp_invalid_directive); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1426 | |
| 1427 | // Read the rest of the PP line. |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1428 | DiscardUntilEndOfDirective(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1429 | |
| 1430 | // Okay, we're done parsing the directive. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Chris Lattner | 01d66cc | 2006-07-03 22:16:27 +0000 | [diff] [blame] | 1433 | void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Tok, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1434 | bool isWarning) { |
| 1435 | // Read the rest of the line raw. We do this because we don't want macros |
| 1436 | // to be expanded and we don't require that the tokens be valid preprocessing |
| 1437 | // tokens. For example, this is allowed: "#warning ` 'foo". GCC does |
| 1438 | // collapse multiple consequtive white space between tokens, but this isn't |
| 1439 | // specified by the standard. |
| 1440 | std::string Message = CurLexer->ReadToEndOfLine(); |
| 1441 | |
| 1442 | unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error; |
Chris Lattner | 01d66cc | 2006-07-03 22:16:27 +0000 | [diff] [blame] | 1443 | return Diag(Tok, DiagID, Message); |
| 1444 | } |
| 1445 | |
| 1446 | /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive. |
| 1447 | /// |
| 1448 | void Preprocessor::HandleIdentSCCSDirective(LexerToken &Tok) { |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1449 | // Yes, this directive is an extension. |
Chris Lattner | 01d66cc | 2006-07-03 22:16:27 +0000 | [diff] [blame] | 1450 | Diag(Tok, diag::ext_pp_ident_directive); |
| 1451 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1452 | // Read the string argument. |
Chris Lattner | 01d66cc | 2006-07-03 22:16:27 +0000 | [diff] [blame] | 1453 | LexerToken StrTok; |
| 1454 | Lex(StrTok); |
| 1455 | |
| 1456 | // 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] | 1457 | if (StrTok.getKind() != tok::string_literal && |
| 1458 | StrTok.getKind() != tok::wide_string_literal) |
Chris Lattner | 01d66cc | 2006-07-03 22:16:27 +0000 | [diff] [blame] | 1459 | return Diag(StrTok, diag::err_pp_malformed_ident); |
| 1460 | |
| 1461 | // Verify that there is nothing after the string, other than EOM. |
| 1462 | CheckEndOfDirective("#ident"); |
| 1463 | |
| 1464 | if (IdentHandler) |
| 1465 | IdentHandler(Tok.getLocation(), getSpelling(StrTok)); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1468 | //===----------------------------------------------------------------------===// |
| 1469 | // Preprocessor Include Directive Handling. |
| 1470 | //===----------------------------------------------------------------------===// |
| 1471 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1472 | /// HandleIncludeDirective - The "#include" tokens have just been read, read the |
| 1473 | /// file to be included from the lexer, then include it! This is a common |
| 1474 | /// routine with functionality shared between #include, #include_next and |
| 1475 | /// #import. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1476 | void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1477 | const DirectoryLookup *LookupFrom, |
| 1478 | bool isImport) { |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1479 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1480 | LexerToken FilenameTok; |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 1481 | std::string Filename = CurLexer->LexIncludeFilename(FilenameTok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1482 | |
| 1483 | // If the token kind is EOM, the error has already been diagnosed. |
| 1484 | if (FilenameTok.getKind() == tok::eom) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1485 | return; |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 1486 | |
| 1487 | // Verify that there is nothing after the filename, other than EOM. Use the |
| 1488 | // preprocessor to lex this in case lexing the filename entered a macro. |
| 1489 | CheckEndOfDirective("#include"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1490 | |
| 1491 | // Check that we don't have infinite #include recursion. |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 1492 | if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1493 | return Diag(FilenameTok, diag::err_pp_include_too_deep); |
| 1494 | |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 1495 | // Find out whether the filename is <x> or "x". |
| 1496 | bool isAngled = Filename[0] == '<'; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1497 | |
| 1498 | // Remove the quotes. |
| 1499 | Filename = std::string(Filename.begin()+1, Filename.end()-1); |
| 1500 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1501 | // Search include directories. |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1502 | const DirectoryLookup *CurDir; |
| 1503 | const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1504 | if (File == 0) |
| 1505 | return Diag(FilenameTok, diag::err_pp_file_not_found); |
| 1506 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1507 | // Ask HeaderInfo if we should enter this #include file. |
| 1508 | if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) { |
| 1509 | // If it returns true, #including this file will have no effect. |
Chris Lattner | 3665f16 | 2006-07-04 07:26:10 +0000 | [diff] [blame] | 1510 | return; |
| 1511 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1512 | |
| 1513 | // Look up the file, create a File ID for it. |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1514 | unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation()); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1515 | if (FileID == 0) |
| 1516 | return Diag(FilenameTok, diag::err_pp_file_not_found); |
| 1517 | |
| 1518 | // Finally, if all is good, enter the new file! |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1519 | EnterSourceFile(FileID, CurDir); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | /// HandleIncludeNextDirective - Implements #include_next. |
| 1523 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1524 | void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) { |
| 1525 | Diag(IncludeNextTok, diag::ext_pp_include_next_directive); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1526 | |
| 1527 | // #include_next is like #include, except that we start searching after |
| 1528 | // the current found directory. If we can't do this, issue a |
| 1529 | // diagnostic. |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1530 | const DirectoryLookup *Lookup = CurDirLookup; |
Chris Lattner | 69772b0 | 2006-07-02 20:34:39 +0000 | [diff] [blame] | 1531 | if (isInPrimaryFile()) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1532 | Lookup = 0; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1533 | Diag(IncludeNextTok, diag::pp_include_next_in_primary); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1534 | } else if (Lookup == 0) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1535 | Diag(IncludeNextTok, diag::pp_include_next_absolute_path); |
Chris Lattner | c899718 | 2006-06-22 05:52:16 +0000 | [diff] [blame] | 1536 | } else { |
| 1537 | // Start looking up in the next directory. |
| 1538 | ++Lookup; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | return HandleIncludeDirective(IncludeNextTok, Lookup); |
| 1542 | } |
| 1543 | |
| 1544 | /// HandleImportDirective - Implements #import. |
| 1545 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1546 | void Preprocessor::HandleImportDirective(LexerToken &ImportTok) { |
| 1547 | Diag(ImportTok, diag::ext_pp_import_directive); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1548 | |
| 1549 | return HandleIncludeDirective(ImportTok, 0, true); |
| 1550 | } |
| 1551 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1552 | //===----------------------------------------------------------------------===// |
| 1553 | // Preprocessor Macro Directive Handling. |
| 1554 | //===----------------------------------------------------------------------===// |
| 1555 | |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1556 | /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro |
| 1557 | /// definition has just been read. Lex the rest of the arguments and the |
| 1558 | /// closing ), updating MI with what we learn. Return true if an error occurs |
| 1559 | /// parsing the arg list. |
| 1560 | bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) { |
| 1561 | LexerToken Tok; |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1562 | while (1) { |
| 1563 | LexUnexpandedToken(Tok); |
| 1564 | switch (Tok.getKind()) { |
| 1565 | case tok::r_paren: |
| 1566 | // Found the end of the argument list. |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1567 | if (MI->arg_begin() == MI->arg_end()) return false; // #define FOO() |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1568 | // Otherwise we have #define FOO(A,) |
| 1569 | Diag(Tok, diag::err_pp_expected_ident_in_arg_list); |
| 1570 | return true; |
| 1571 | case tok::ellipsis: // #define X(... -> C99 varargs |
| 1572 | // Warn if use of C99 feature in non-C99 mode. |
| 1573 | if (!Features.C99) Diag(Tok, diag::ext_variadic_macro); |
| 1574 | |
| 1575 | // Lex the token after the identifier. |
| 1576 | LexUnexpandedToken(Tok); |
| 1577 | if (Tok.getKind() != tok::r_paren) { |
| 1578 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1579 | return true; |
| 1580 | } |
Chris Lattner | 95a06b3 | 2006-07-30 08:40:43 +0000 | [diff] [blame] | 1581 | // Add the __VA_ARGS__ identifier as an argument. |
| 1582 | MI->addArgument(Ident__VA_ARGS__); |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1583 | MI->setIsC99Varargs(); |
| 1584 | return false; |
| 1585 | case tok::eom: // #define X( |
| 1586 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1587 | return true; |
Chris Lattner | 62aa0d4 | 2006-10-20 05:08:24 +0000 | [diff] [blame] | 1588 | default: |
| 1589 | // Handle keywords and identifiers here to accept things like |
| 1590 | // #define Foo(for) for. |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1591 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Chris Lattner | 62aa0d4 | 2006-10-20 05:08:24 +0000 | [diff] [blame] | 1592 | if (II == 0) { |
| 1593 | // #define X(1 |
| 1594 | Diag(Tok, diag::err_pp_invalid_tok_in_arg_list); |
| 1595 | return true; |
| 1596 | } |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1597 | |
| 1598 | // If this is already used as an argument, it is used multiple times (e.g. |
| 1599 | // #define X(A,A. |
Chris Lattner | c653246 | 2006-07-15 06:55:18 +0000 | [diff] [blame] | 1600 | if (MI->getArgumentNum(II) != -1) { // C99 6.10.3p6 |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1601 | Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName()); |
| 1602 | return true; |
| 1603 | } |
| 1604 | |
| 1605 | // Add the argument to the macro info. |
| 1606 | MI->addArgument(II); |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1607 | |
| 1608 | // Lex the token after the identifier. |
| 1609 | LexUnexpandedToken(Tok); |
| 1610 | |
| 1611 | switch (Tok.getKind()) { |
| 1612 | default: // #define X(A B |
| 1613 | Diag(Tok, diag::err_pp_expected_comma_in_arg_list); |
| 1614 | return true; |
| 1615 | case tok::r_paren: // #define X(A) |
| 1616 | return false; |
| 1617 | case tok::comma: // #define X(A, |
| 1618 | break; |
| 1619 | case tok::ellipsis: // #define X(A... -> GCC extension |
| 1620 | // Diagnose extension. |
| 1621 | Diag(Tok, diag::ext_named_variadic_macro); |
| 1622 | |
| 1623 | // Lex the token after the identifier. |
| 1624 | LexUnexpandedToken(Tok); |
| 1625 | if (Tok.getKind() != tok::r_paren) { |
| 1626 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1627 | return true; |
| 1628 | } |
| 1629 | |
| 1630 | MI->setIsGNUVarargs(); |
| 1631 | return false; |
| 1632 | } |
| 1633 | } |
| 1634 | } |
| 1635 | } |
| 1636 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1637 | /// HandleDefineDirective - Implements #define. This consumes the entire macro |
Chris Lattner | 81278c6 | 2006-10-14 19:03:49 +0000 | [diff] [blame] | 1638 | /// line then lets the caller lex the next real token. If 'isTargetSpecific' is |
| 1639 | /// true, then this is a "#define_target", otherwise this is a "#define". |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1640 | /// |
Chris Lattner | 81278c6 | 2006-10-14 19:03:49 +0000 | [diff] [blame] | 1641 | void Preprocessor::HandleDefineDirective(LexerToken &DefineTok, |
| 1642 | bool isTargetSpecific) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1643 | ++NumDefined; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1644 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1645 | LexerToken MacroNameTok; |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 1646 | ReadMacroName(MacroNameTok, 1); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1647 | |
| 1648 | // Error reading macro name? If so, diagnostic already issued. |
| 1649 | if (MacroNameTok.getKind() == tok::eom) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1650 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1651 | |
Chris Lattner | 457fc15 | 2006-07-29 06:30:25 +0000 | [diff] [blame] | 1652 | // If we are supposed to keep comments in #defines, reenable comment saving |
| 1653 | // mode. |
| 1654 | CurLexer->KeepCommentMode = Features.KeepMacroComments; |
| 1655 | |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 1656 | // Create the new macro. |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 1657 | MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation()); |
Chris Lattner | 81278c6 | 2006-10-14 19:03:49 +0000 | [diff] [blame] | 1658 | if (isTargetSpecific) MI->setIsTargetSpecific(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1659 | |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 1660 | // If the identifier is an 'other target' macro, clear this bit. |
| 1661 | MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false); |
| 1662 | |
| 1663 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1664 | LexerToken Tok; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1665 | LexUnexpandedToken(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1666 | |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1667 | // If this is a function-like macro definition, parse the argument list, |
| 1668 | // marking each of the identifiers as being used as macro arguments. Also, |
| 1669 | // check other constraints on the first token of the macro body. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1670 | if (Tok.getKind() == tok::eom) { |
| 1671 | // If there is no body to this macro, we have no special handling here. |
| 1672 | } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) { |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1673 | // This is a function-like macro definition. Read the argument list. |
| 1674 | MI->setIsFunctionLike(); |
| 1675 | if (ReadMacroDefinitionArgList(MI)) { |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1676 | // Forget about MI. |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1677 | delete MI; |
Chris Lattner | 6e0d42c | 2006-07-08 20:32:52 +0000 | [diff] [blame] | 1678 | // Throw away the rest of the line. |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 1679 | if (CurLexer->ParsingPreprocessorDirective) |
| 1680 | DiscardUntilEndOfDirective(); |
| 1681 | return; |
| 1682 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1683 | |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1684 | // Read the first token after the arg list for down below. |
| 1685 | LexUnexpandedToken(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1686 | } else if (!Tok.hasLeadingSpace()) { |
| 1687 | // C99 requires whitespace between the macro definition and the body. Emit |
| 1688 | // a diagnostic for something like "#define X+". |
| 1689 | if (Features.C99) { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1690 | Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1691 | } else { |
| 1692 | // FIXME: C90/C++ do not get this diagnostic, but it does get a similar |
| 1693 | // one in some cases! |
| 1694 | } |
| 1695 | } else { |
| 1696 | // This is a normal token with leading space. Clear the leading space |
| 1697 | // marker on the first token to get proper expansion. |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 1698 | Tok.clearFlag(LexerToken::LeadingSpace); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Chris Lattner | 7e37483 | 2006-07-29 03:46:57 +0000 | [diff] [blame] | 1701 | // If this is a definition of a variadic C99 function-like macro, not using |
| 1702 | // the GNU named varargs extension, enabled __VA_ARGS__. |
| 1703 | |
| 1704 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 1705 | // This gets unpoisoned where it is allowed. |
| 1706 | assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!"); |
| 1707 | if (MI->isC99Varargs()) |
| 1708 | Ident__VA_ARGS__->setIsPoisoned(false); |
| 1709 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1710 | // Read the rest of the macro body. |
| 1711 | while (Tok.getKind() != tok::eom) { |
| 1712 | MI->AddTokenToBody(Tok); |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1713 | |
| 1714 | // 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] | 1715 | // parameters in function-like macro expansions. |
| 1716 | if (Tok.getKind() != tok::hash || MI->isObjectLike()) { |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1717 | // Get the next token of the macro. |
| 1718 | LexUnexpandedToken(Tok); |
| 1719 | continue; |
| 1720 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1721 | |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1722 | // Get the next token of the macro. |
| 1723 | LexUnexpandedToken(Tok); |
| 1724 | |
| 1725 | // Not a macro arg identifier? |
Chris Lattner | c653246 | 2006-07-15 06:55:18 +0000 | [diff] [blame] | 1726 | if (!Tok.getIdentifierInfo() || |
Chris Lattner | 95a06b3 | 2006-07-30 08:40:43 +0000 | [diff] [blame] | 1727 | MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) { |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1728 | Diag(Tok, diag::err_pp_stringize_not_parameter); |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1729 | delete MI; |
Chris Lattner | 7e37483 | 2006-07-29 03:46:57 +0000 | [diff] [blame] | 1730 | |
| 1731 | // Disable __VA_ARGS__ again. |
| 1732 | Ident__VA_ARGS__->setIsPoisoned(true); |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1733 | return; |
| 1734 | } |
| 1735 | |
| 1736 | // Things look ok, add the param name token to the macro. |
| 1737 | MI->AddTokenToBody(Tok); |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1738 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1739 | // Get the next token of the macro. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1740 | LexUnexpandedToken(Tok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1741 | } |
Chris Lattner | 7e37483 | 2006-07-29 03:46:57 +0000 | [diff] [blame] | 1742 | |
| 1743 | // Disable __VA_ARGS__ again. |
| 1744 | Ident__VA_ARGS__->setIsPoisoned(true); |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1745 | |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1746 | // Check that there is no paste (##) operator at the begining or end of the |
| 1747 | // replacement list. |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 1748 | unsigned NumTokens = MI->getNumTokens(); |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1749 | if (NumTokens != 0) { |
| 1750 | if (MI->getReplacementToken(0).getKind() == tok::hashhash) { |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1751 | Diag(MI->getReplacementToken(0), diag::err_paste_at_start); |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1752 | delete MI; |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1753 | return; |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1754 | } |
| 1755 | if (MI->getReplacementToken(NumTokens-1).getKind() == tok::hashhash) { |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1756 | Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end); |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1757 | delete MI; |
Chris Lattner | 815a1f9 | 2006-07-08 20:48:04 +0000 | [diff] [blame] | 1758 | return; |
Chris Lattner | bff18d5 | 2006-07-06 04:49:18 +0000 | [diff] [blame] | 1759 | } |
| 1760 | } |
| 1761 | |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 1762 | // If this is the primary source file, remember that this macro hasn't been |
| 1763 | // used yet. |
| 1764 | if (isInPrimaryFile()) |
| 1765 | MI->setIsUsed(false); |
| 1766 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1767 | // Finally, if this identifier already had a macro defined for it, verify that |
| 1768 | // the macro bodies are identical and free the old definition. |
| 1769 | if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) { |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 1770 | if (!OtherMI->isUsed()) |
| 1771 | Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used); |
| 1772 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1773 | // Macros must be identical. This means all tokes and whitespace separation |
Chris Lattner | 21284df | 2006-07-08 07:16:08 +0000 | [diff] [blame] | 1774 | // must be the same. C99 6.10.3.2. |
| 1775 | if (!MI->isIdenticalTo(*OtherMI, *this)) { |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 1776 | Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef, |
| 1777 | MacroNameTok.getIdentifierInfo()->getName()); |
| 1778 | Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2); |
| 1779 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1780 | delete OtherMI; |
| 1781 | } |
| 1782 | |
| 1783 | MacroNameTok.getIdentifierInfo()->setMacroInfo(MI); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 1786 | /// HandleDefineOtherTargetDirective - Implements #define_other_target. |
| 1787 | void Preprocessor::HandleDefineOtherTargetDirective(LexerToken &Tok) { |
| 1788 | LexerToken MacroNameTok; |
| 1789 | ReadMacroName(MacroNameTok, 1); |
| 1790 | |
| 1791 | // Error reading macro name? If so, diagnostic already issued. |
| 1792 | if (MacroNameTok.getKind() == tok::eom) |
| 1793 | return; |
| 1794 | |
| 1795 | // Check to see if this is the last token on the #undef line. |
| 1796 | CheckEndOfDirective("#define_other_target"); |
| 1797 | |
| 1798 | // If there is already a macro defined by this name, turn it into a |
| 1799 | // target-specific define. |
| 1800 | if (MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) { |
| 1801 | MI->setIsTargetSpecific(true); |
| 1802 | return; |
| 1803 | } |
| 1804 | |
| 1805 | // Mark the identifier as being a macro on some other target. |
| 1806 | MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(); |
| 1807 | } |
| 1808 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1809 | |
| 1810 | /// HandleUndefDirective - Implements #undef. |
| 1811 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1812 | void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1813 | ++NumUndefined; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1814 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1815 | LexerToken MacroNameTok; |
Chris Lattner | e8eef32 | 2006-07-08 07:01:00 +0000 | [diff] [blame] | 1816 | ReadMacroName(MacroNameTok, 2); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1817 | |
| 1818 | // Error reading macro name? If so, diagnostic already issued. |
| 1819 | if (MacroNameTok.getKind() == tok::eom) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1820 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1821 | |
| 1822 | // 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] | 1823 | CheckEndOfDirective("#undef"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1824 | |
| 1825 | // Okay, we finally have a valid identifier to undef. |
| 1826 | MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo(); |
| 1827 | |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 1828 | // #undef untaints an identifier if it were marked by define_other_target. |
| 1829 | MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false); |
| 1830 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1831 | // 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] | 1832 | if (MI == 0) return; |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 1833 | |
Chris Lattner | 13044d9 | 2006-07-03 05:16:44 +0000 | [diff] [blame] | 1834 | if (!MI->isUsed()) |
| 1835 | Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1836 | |
| 1837 | // Free macro definition. |
| 1838 | delete MI; |
| 1839 | MacroNameTok.getIdentifierInfo()->setMacroInfo(0); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 1843 | //===----------------------------------------------------------------------===// |
| 1844 | // Preprocessor Conditional Directive Handling. |
| 1845 | //===----------------------------------------------------------------------===// |
| 1846 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1847 | /// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1848 | /// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true |
| 1849 | /// if any tokens have been returned or pp-directives activated before this |
| 1850 | /// #ifndef has been lexed. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1851 | /// |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1852 | void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef, |
| 1853 | bool ReadAnyTokensBeforeDirective) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1854 | ++NumIf; |
| 1855 | LexerToken DirectiveTok = Result; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1856 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1857 | LexerToken MacroNameTok; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1858 | ReadMacroName(MacroNameTok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1859 | |
| 1860 | // Error reading macro name? If so, diagnostic already issued. |
| 1861 | if (MacroNameTok.getKind() == tok::eom) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1862 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1863 | |
| 1864 | // 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] | 1865 | CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef"); |
| 1866 | |
| 1867 | // If the start of a top-level #ifdef, inform MIOpt. |
| 1868 | if (!ReadAnyTokensBeforeDirective && |
| 1869 | CurLexer->getConditionalStackDepth() == 0) { |
| 1870 | assert(isIfndef && "#ifdef shouldn't reach here"); |
| 1871 | CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo()); |
| 1872 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1873 | |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 1874 | IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); |
| 1875 | MacroInfo *MI = MII->getMacroInfo(); |
Chris Lattner | a78a97e | 2006-07-03 05:42:18 +0000 | [diff] [blame] | 1876 | |
Chris Lattner | 81278c6 | 2006-10-14 19:03:49 +0000 | [diff] [blame] | 1877 | // If there is a macro, process it. |
| 1878 | if (MI) { |
| 1879 | // Mark it used. |
| 1880 | MI->setIsUsed(true); |
| 1881 | |
| 1882 | // If this is the first use of a target-specific macro, warn about it. |
| 1883 | if (MI->isTargetSpecific()) { |
| 1884 | MI->setIsTargetSpecific(false); // Don't warn on second use. |
| 1885 | getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(), |
| 1886 | diag::port_target_macro_use); |
| 1887 | } |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 1888 | } else { |
| 1889 | // Use of a target-specific macro for some other target? If so, warn. |
| 1890 | if (MII->isOtherTargetMacro()) { |
| 1891 | MII->setIsOtherTargetMacro(false); // Don't warn on second use. |
| 1892 | getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(), |
| 1893 | diag::port_target_macro_use); |
| 1894 | } |
Chris Lattner | 81278c6 | 2006-10-14 19:03:49 +0000 | [diff] [blame] | 1895 | } |
Chris Lattner | a78a97e | 2006-07-03 05:42:18 +0000 | [diff] [blame] | 1896 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1897 | // Should we include the stuff contained by this directive? |
Chris Lattner | a78a97e | 2006-07-03 05:42:18 +0000 | [diff] [blame] | 1898 | if (!MI == isIfndef) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1899 | // 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] | 1900 | CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1901 | /*foundnonskip*/true, /*foundelse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1902 | } else { |
| 1903 | // 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] | 1904 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1905 | /*Foundnonskip*/false, |
| 1906 | /*FoundElse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1907 | } |
| 1908 | } |
| 1909 | |
| 1910 | /// HandleIfDirective - Implements the #if directive. |
| 1911 | /// |
Chris Lattner | a8654ca | 2006-07-04 17:42:08 +0000 | [diff] [blame] | 1912 | void Preprocessor::HandleIfDirective(LexerToken &IfToken, |
| 1913 | bool ReadAnyTokensBeforeDirective) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1914 | ++NumIf; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1915 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1916 | // Parse and evaluation the conditional expression. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 1917 | IdentifierInfo *IfNDefMacro = 0; |
Chris Lattner | a8654ca | 2006-07-04 17:42:08 +0000 | [diff] [blame] | 1918 | bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1919 | |
| 1920 | // Should we include the stuff contained by this directive? |
| 1921 | if (ConditionalTrue) { |
Chris Lattner | a8654ca | 2006-07-04 17:42:08 +0000 | [diff] [blame] | 1922 | // If this condition is equivalent to #ifndef X, and if this is the first |
| 1923 | // directive seen, handle it for the multiple-include optimization. |
| 1924 | if (!ReadAnyTokensBeforeDirective && |
| 1925 | CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro) |
| 1926 | CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro); |
| 1927 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1928 | // 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] | 1929 | CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1930 | /*foundnonskip*/true, /*foundelse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1931 | } else { |
| 1932 | // 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] | 1933 | SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false, |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1934 | /*FoundElse*/false); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | /// HandleEndifDirective - Implements the #endif directive. |
| 1939 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1940 | void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1941 | ++NumEndif; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1942 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1943 | // Check that this is the whole directive. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1944 | CheckEndOfDirective("#endif"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1945 | |
| 1946 | PPConditionalInfo CondInfo; |
| 1947 | if (CurLexer->popConditionalLevel(CondInfo)) { |
| 1948 | // No conditionals on the stack: this is an #endif without an #if. |
| 1949 | return Diag(EndifToken, diag::err_pp_endif_without_if); |
| 1950 | } |
| 1951 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1952 | // If this the end of a top-level #endif, inform MIOpt. |
| 1953 | if (CurLexer->getConditionalStackDepth() == 0) |
| 1954 | CurLexer->MIOpt.ExitTopLevelConditional(); |
| 1955 | |
Chris Lattner | 538d7f3 | 2006-07-20 04:31:52 +0000 | [diff] [blame] | 1956 | assert(!CondInfo.WasSkipping && !CurLexer->LexingRawMode && |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1957 | "This code should only be reachable in the non-skipping case!"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1961 | void Preprocessor::HandleElseDirective(LexerToken &Result) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1962 | ++NumElse; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1963 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1964 | // #else directive in a non-skipping conditional... start skipping. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1965 | CheckEndOfDirective("#else"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1966 | |
| 1967 | PPConditionalInfo CI; |
| 1968 | if (CurLexer->popConditionalLevel(CI)) |
| 1969 | return Diag(Result, diag::pp_err_else_without_if); |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1970 | |
| 1971 | // If this is a top-level #else, inform the MIOpt. |
| 1972 | if (CurLexer->getConditionalStackDepth() == 0) |
| 1973 | CurLexer->MIOpt.FoundTopLevelElse(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1974 | |
| 1975 | // 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] | 1976 | if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1977 | |
| 1978 | // Finally, skip the rest of the contents of this block and return the first |
| 1979 | // token after it. |
| 1980 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 1981 | /*FoundElse*/true); |
| 1982 | } |
| 1983 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1984 | void Preprocessor::HandleElifDirective(LexerToken &ElifToken) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1985 | ++NumElse; |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1986 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1987 | // #elif directive in a non-skipping conditional... start skipping. |
| 1988 | // We don't care what the condition is, because we will always skip it (since |
| 1989 | // the block immediately before it was included). |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1990 | DiscardUntilEndOfDirective(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1991 | |
| 1992 | PPConditionalInfo CI; |
| 1993 | if (CurLexer->popConditionalLevel(CI)) |
| 1994 | return Diag(ElifToken, diag::pp_err_elif_without_if); |
| 1995 | |
Chris Lattner | 371ac8a | 2006-07-04 07:11:10 +0000 | [diff] [blame] | 1996 | // If this is a top-level #elif, inform the MIOpt. |
| 1997 | if (CurLexer->getConditionalStackDepth() == 0) |
| 1998 | CurLexer->MIOpt.FoundTopLevelElse(); |
| 1999 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2000 | // 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] | 2001 | if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 2002 | |
| 2003 | // Finally, skip the rest of the contents of this block and return the first |
| 2004 | // token after it. |
| 2005 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
| 2006 | /*FoundElse*/CI.FoundElse); |
| 2007 | } |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 2008 | |