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