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