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