Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 1 | //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===// |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements # directive processing for the Preprocessor. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 15 | #include "clang/Lex/LiteralSupport.h" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 16 | #include "clang/Lex/HeaderSearch.h" |
| 17 | #include "clang/Lex/MacroInfo.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 18 | #include "clang/Lex/LexDiagnostic.h" |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 19 | #include "clang/Lex/CodeCompletionHandler.h" |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 20 | #include "clang/Lex/ModuleLoader.h" |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Pragma.h" |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 22 | #include "clang/Basic/FileManager.h" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/APInt.h" |
Douglas Gregor | e3a8256 | 2011-11-30 18:02:36 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | // Utility Methods for Preprocessor Directive Handling. |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 32 | MacroInfo *Preprocessor::AllocateMacroInfo() { |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 33 | MacroInfoChain *MIChain; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 35 | if (MICache) { |
| 36 | MIChain = MICache; |
| 37 | MICache = MICache->Next; |
Ted Kremenek | af8fa25 | 2010-10-19 18:16:54 +0000 | [diff] [blame] | 38 | } |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 39 | else { |
| 40 | MIChain = BP.Allocate<MacroInfoChain>(); |
| 41 | } |
| 42 | |
| 43 | MIChain->Next = MIChainHead; |
| 44 | MIChain->Prev = 0; |
| 45 | if (MIChainHead) |
| 46 | MIChainHead->Prev = MIChain; |
| 47 | MIChainHead = MIChain; |
| 48 | |
| 49 | return &(MIChain->MI); |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) { |
| 53 | MacroInfo *MI = AllocateMacroInfo(); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 54 | new (MI) MacroInfo(L); |
| 55 | return MI; |
| 56 | } |
| 57 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 58 | MacroInfo *Preprocessor::CloneMacroInfo(const MacroInfo &MacroToClone) { |
| 59 | MacroInfo *MI = AllocateMacroInfo(); |
| 60 | new (MI) MacroInfo(MacroToClone, BP); |
| 61 | return MI; |
| 62 | } |
| 63 | |
Chris Lattner | 0301b3f | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 64 | /// ReleaseMacroInfo - Release the specified MacroInfo. This memory will |
| 65 | /// be reused for allocating new MacroInfo objects. |
Chris Lattner | 2c1ab90 | 2010-08-18 16:08:51 +0000 | [diff] [blame] | 66 | void Preprocessor::ReleaseMacroInfo(MacroInfo *MI) { |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 67 | MacroInfoChain *MIChain = (MacroInfoChain*) MI; |
| 68 | if (MacroInfoChain *Prev = MIChain->Prev) { |
| 69 | MacroInfoChain *Next = MIChain->Next; |
| 70 | Prev->Next = Next; |
| 71 | if (Next) |
| 72 | Next->Prev = Prev; |
| 73 | } |
| 74 | else { |
| 75 | assert(MIChainHead == MIChain); |
| 76 | MIChainHead = MIChain->Next; |
| 77 | MIChainHead->Prev = 0; |
| 78 | } |
| 79 | MIChain->Next = MICache; |
| 80 | MICache = MIChain; |
Chris Lattner | 0301b3f | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 81 | |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 82 | MI->Destroy(); |
| 83 | } |
Chris Lattner | 0301b3f | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 85 | /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 86 | /// current line until the tok::eod token is found. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 87 | void Preprocessor::DiscardUntilEndOfDirective() { |
| 88 | Token Tmp; |
| 89 | do { |
| 90 | LexUnexpandedToken(Tmp); |
Peter Collingbourne | a5ef584 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 91 | assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens"); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 92 | } while (Tmp.isNot(tok::eod)); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 95 | /// ReadMacroName - Lex and validate a macro name, which occurs after a |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 96 | /// #define or #undef. This sets the token kind to eod and discards the rest |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 97 | /// of the macro line if the macro name is invalid. isDefineUndef is 1 if |
| 98 | /// this is due to a a #define, 2 if #undef directive, 0 if it is something |
| 99 | /// else (e.g. #ifdef). |
| 100 | void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) { |
| 101 | // Read the token, don't allow macro expansion on it. |
| 102 | LexUnexpandedToken(MacroNameTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 104 | if (MacroNameTok.is(tok::code_completion)) { |
| 105 | if (CodeComplete) |
| 106 | CodeComplete->CodeCompleteMacroName(isDefineUndef == 1); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 107 | setCodeCompletionReached(); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 108 | LexUnexpandedToken(MacroNameTok); |
Douglas Gregor | 1fbb447 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 111 | // Missing macro name? |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 112 | if (MacroNameTok.is(tok::eod)) { |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 113 | Diag(MacroNameTok, diag::err_pp_missing_macro_name); |
| 114 | return; |
| 115 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 117 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); |
| 118 | if (II == 0) { |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 119 | bool Invalid = false; |
| 120 | std::string Spelling = getSpelling(MacroNameTok, &Invalid); |
| 121 | if (Invalid) |
| 122 | return; |
| 123 | |
Chris Lattner | 9485d23 | 2008-12-13 20:12:40 +0000 | [diff] [blame] | 124 | const IdentifierInfo &Info = Identifiers.get(Spelling); |
| 125 | if (Info.isCPlusPlusOperatorKeyword()) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 126 | // C++ 2.5p2: Alternative tokens behave the same as its primary token |
| 127 | // except for their spellings. |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 128 | Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 129 | else |
| 130 | Diag(MacroNameTok, diag::err_pp_macro_not_identifier); |
| 131 | // Fall through on error. |
| 132 | } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) { |
| 133 | // Error if defining "defined": C99 6.10.8.4. |
| 134 | Diag(MacroNameTok, diag::err_defined_macro_name); |
| 135 | } else if (isDefineUndef && II->hasMacroDefinition() && |
| 136 | getMacroInfo(II)->isBuiltinMacro()) { |
| 137 | // Error if defining "__LINE__" and other builtins: C99 6.10.8.4. |
| 138 | if (isDefineUndef == 1) |
| 139 | Diag(MacroNameTok, diag::pp_redef_builtin_macro); |
| 140 | else |
| 141 | Diag(MacroNameTok, diag::pp_undef_builtin_macro); |
| 142 | } else { |
| 143 | // Okay, we got a good identifier node. Return it. |
| 144 | return; |
| 145 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 147 | // Invalid macro name, read and discard the rest of the line. Then set the |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 148 | // token kind to tok::eod. |
| 149 | MacroNameTok.setKind(tok::eod); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 150 | return DiscardUntilEndOfDirective(); |
| 151 | } |
| 152 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 153 | /// CheckEndOfDirective - Ensure that the next token is a tok::eod token. If |
| 154 | /// not, emit a diagnostic and consume up until the eod. If EnableMacros is |
Chris Lattner | ab82f41 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 155 | /// true, then we consider macros that expand to zero tokens as being ok. |
| 156 | void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 157 | Token Tmp; |
Chris Lattner | ab82f41 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 158 | // Lex unexpanded tokens for most directives: macros might expand to zero |
| 159 | // tokens, causing us to miss diagnosing invalid lines. Some directives (like |
| 160 | // #line) allow empty macros. |
| 161 | if (EnableMacros) |
| 162 | Lex(Tmp); |
| 163 | else |
| 164 | LexUnexpandedToken(Tmp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 166 | // There should be no tokens after the directive, but we allow them as an |
| 167 | // extension. |
| 168 | while (Tmp.is(tok::comment)) // Skip comments in -C mode. |
| 169 | LexUnexpandedToken(Tmp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 171 | if (Tmp.isNot(tok::eod)) { |
Chris Lattner | 959875a | 2009-04-14 05:15:20 +0000 | [diff] [blame] | 172 | // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89, |
Peter Collingbourne | b2eb53d | 2011-02-22 13:49:00 +0000 | [diff] [blame] | 173 | // or if this is a macro-style preprocessing directive, because it is more |
| 174 | // trouble than it is worth to insert /**/ and check that there is no /**/ |
| 175 | // in the range also. |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 176 | FixItHint Hint; |
Peter Collingbourne | b2eb53d | 2011-02-22 13:49:00 +0000 | [diff] [blame] | 177 | if ((Features.GNUMode || Features.C99 || Features.CPlusPlus) && |
| 178 | !CurTokenLexer) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 179 | Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//"); |
| 180 | Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 181 | DiscardUntilEndOfDirective(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | |
| 186 | |
| 187 | /// SkipExcludedConditionalBlock - We just read a #if or related directive and |
| 188 | /// decided that the subsequent tokens are in the #if'd out portion of the |
| 189 | /// file. Lex the rest of the file, until we see an #endif. If |
| 190 | /// FoundNonSkipPortion is true, then we have already emitted code for part of |
| 191 | /// this #if directive, so #else/#elif blocks should never be entered. If ElseOk |
| 192 | /// is true, then #else directives are ok, if not, then we have already seen one |
| 193 | /// so a #else directive is a duplicate. When this returns, the caller can lex |
| 194 | /// the first valid token. |
| 195 | void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, |
| 196 | bool FoundNonSkipPortion, |
Argyrios Kyrtzidis | 6b4ff04 | 2011-09-27 17:32:05 +0000 | [diff] [blame] | 197 | bool FoundElse, |
| 198 | SourceLocation ElseLoc) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 199 | ++NumSkipped; |
Ted Kremenek | f6452c5 | 2008-11-18 01:04:47 +0000 | [diff] [blame] | 200 | assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 201 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 202 | CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 203 | FoundNonSkipPortion, FoundElse); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 205 | if (CurPTHLexer) { |
| 206 | PTHSkipExcludedConditionalBlock(); |
| 207 | return; |
| 208 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 210 | // Enter raw mode to disable identifier lookup (and thus macro expansion), |
| 211 | // disabling warnings, etc. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 212 | CurPPLexer->LexingRawMode = true; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 213 | Token Tok; |
| 214 | while (1) { |
Chris Lattner | 2c6b193 | 2010-01-18 22:33:01 +0000 | [diff] [blame] | 215 | CurLexer->Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 217 | if (Tok.is(tok::code_completion)) { |
| 218 | if (CodeComplete) |
| 219 | CodeComplete->CodeCompleteInConditionalExclusion(); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 220 | setCodeCompletionReached(); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 221 | continue; |
| 222 | } |
| 223 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 224 | // If this is the end of the buffer, we have an error. |
| 225 | if (Tok.is(tok::eof)) { |
| 226 | // Emit errors for each unterminated conditional on the stack, including |
| 227 | // the current one. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 228 | while (!CurPPLexer->ConditionalStack.empty()) { |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 229 | if (CurLexer->getFileLoc() != CodeCompletionFileLoc) |
Douglas Gregor | 2d474ba | 2010-08-12 17:04:55 +0000 | [diff] [blame] | 230 | Diag(CurPPLexer->ConditionalStack.back().IfLoc, |
| 231 | diag::err_pp_unterminated_conditional); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 232 | CurPPLexer->ConditionalStack.pop_back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 235 | // Just return and let the caller lex after this #include. |
| 236 | break; |
| 237 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 239 | // If this token is not a preprocessor directive, just skip it. |
| 240 | if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) |
| 241 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 243 | // We just parsed a # character at the start of a line, so we're in |
| 244 | // directive mode. Tell the lexer this so any newlines we see will be |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 245 | // converted into an EOD token (this terminates the macro). |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 246 | CurPPLexer->ParsingPreprocessorDirective = true; |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 247 | if (CurLexer) CurLexer->SetCommentRetentionState(false); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 248 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 250 | // Read the next token, the directive flavor. |
| 251 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 253 | // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or |
| 254 | // something bogus), skip it. |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 255 | if (Tok.isNot(tok::raw_identifier)) { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 256 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 257 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 258 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 259 | continue; |
| 260 | } |
| 261 | |
| 262 | // If the first letter isn't i or e, it isn't intesting to us. We know that |
| 263 | // this is safe in the face of spelling differences, because there is no way |
| 264 | // to spell an i/e in a strange way that is another letter. Skipping this |
| 265 | // allows us to avoid looking up the identifier info for #define/#undef and |
| 266 | // other common directives. |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 267 | const char *RawCharData = Tok.getRawIdentifierData(); |
| 268 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 269 | char FirstChar = RawCharData[0]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | if (FirstChar >= 'a' && FirstChar <= 'z' && |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 271 | FirstChar != 'i' && FirstChar != 'e') { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 272 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 273 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 274 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 275 | continue; |
| 276 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 278 | // Get the identifier name without trigraphs or embedded newlines. Note |
| 279 | // that we can't use Tok.getIdentifierInfo() because its lookup is disabled |
| 280 | // when skipping. |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 281 | char DirectiveBuf[20]; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 282 | StringRef Directive; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 283 | if (!Tok.needsCleaning() && Tok.getLength() < 20) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 284 | Directive = StringRef(RawCharData, Tok.getLength()); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 285 | } else { |
| 286 | std::string DirectiveStr = getSpelling(Tok); |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 287 | unsigned IdLen = DirectiveStr.size(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 288 | if (IdLen >= 20) { |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 289 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 290 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 291 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 292 | continue; |
| 293 | } |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 294 | memcpy(DirectiveBuf, &DirectiveStr[0], IdLen); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 295 | Directive = StringRef(DirectiveBuf, IdLen); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 296 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 298 | if (Directive.startswith("if")) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 299 | StringRef Sub = Directive.substr(2); |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 300 | if (Sub.empty() || // "if" |
| 301 | Sub == "def" || // "ifdef" |
| 302 | Sub == "ndef") { // "ifndef" |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 303 | // We know the entire #if/#ifdef/#ifndef block will be skipped, don't |
| 304 | // bother parsing the condition. |
| 305 | DiscardUntilEndOfDirective(); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 306 | CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 307 | /*foundnonskip*/false, |
Chandler Carruth | 3a1a874 | 2011-01-03 17:40:17 +0000 | [diff] [blame] | 308 | /*foundelse*/false); |
| 309 | |
| 310 | if (Callbacks) |
| 311 | Callbacks->Endif(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 312 | } |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 313 | } else if (Directive[0] == 'e') { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 314 | StringRef Sub = Directive.substr(1); |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 315 | if (Sub == "ndif") { // "endif" |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 316 | CheckEndOfDirective("endif"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 317 | PPConditionalInfo CondInfo; |
| 318 | CondInfo.WasSkipping = true; // Silence bogus warning. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 319 | bool InCond = CurPPLexer->popConditionalLevel(CondInfo); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 320 | (void)InCond; // Silence warning in no-asserts mode. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 321 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 323 | // If we popped the outermost skipping block, we're done skipping! |
| 324 | if (!CondInfo.WasSkipping) |
| 325 | break; |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 326 | } else if (Sub == "lse") { // "else". |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 327 | // #else directive in a skipping conditional. If not in some other |
| 328 | // skipping conditional, and if #else hasn't already been seen, enter it |
| 329 | // as a non-skipping conditional. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 330 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 332 | // If this is a #else with a #else before it, report the error. |
| 333 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 335 | // Note that we've seen a #else in this conditional. |
| 336 | CondInfo.FoundElse = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Chandler Carruth | 3a1a874 | 2011-01-03 17:40:17 +0000 | [diff] [blame] | 338 | if (Callbacks) |
| 339 | Callbacks->Else(); |
| 340 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 341 | // If the conditional is at the top level, and the #if block wasn't |
| 342 | // entered, enter the #else block now. |
| 343 | if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { |
| 344 | CondInfo.FoundNonSkip = true; |
Argyrios Kyrtzidis | e26224e | 2011-05-21 04:26:04 +0000 | [diff] [blame] | 345 | CheckEndOfDirective("else"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 346 | break; |
Argyrios Kyrtzidis | e26224e | 2011-05-21 04:26:04 +0000 | [diff] [blame] | 347 | } else { |
| 348 | DiscardUntilEndOfDirective(); // C99 6.10p4. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 349 | } |
Benjamin Kramer | b939a4e | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 350 | } else if (Sub == "lif") { // "elif". |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 351 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 352 | |
| 353 | bool ShouldEnter; |
Chandler Carruth | 3a1a874 | 2011-01-03 17:40:17 +0000 | [diff] [blame] | 354 | const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 355 | // If this is in a skipping block or if we're already handled this #if |
| 356 | // block, don't bother parsing the condition. |
| 357 | if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { |
| 358 | DiscardUntilEndOfDirective(); |
| 359 | ShouldEnter = false; |
| 360 | } else { |
| 361 | // Restore the value of LexingRawMode so that identifiers are |
| 362 | // looked up, etc, inside the #elif expression. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 363 | assert(CurPPLexer->LexingRawMode && "We have to be skipping here!"); |
| 364 | CurPPLexer->LexingRawMode = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 365 | IdentifierInfo *IfNDefMacro = 0; |
| 366 | ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 367 | CurPPLexer->LexingRawMode = true; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 368 | } |
Chandler Carruth | 3a1a874 | 2011-01-03 17:40:17 +0000 | [diff] [blame] | 369 | const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 371 | // If this is a #elif with a #else before it, report the error. |
| 372 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 373 | |
Chandler Carruth | 3a1a874 | 2011-01-03 17:40:17 +0000 | [diff] [blame] | 374 | if (Callbacks) |
| 375 | Callbacks->Elif(SourceRange(ConditionalBegin, ConditionalEnd)); |
| 376 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 377 | // If this condition is true, enter it! |
| 378 | if (ShouldEnter) { |
| 379 | CondInfo.FoundNonSkip = true; |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 385 | CurPPLexer->ParsingPreprocessorDirective = false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 386 | // Restore comment saving mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 387 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // Finally, if we are out of the conditional (saw an #endif or ran off the end |
| 391 | // of the file, just stop skipping and return to lexing whatever came after |
| 392 | // the #if block. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 393 | CurPPLexer->LexingRawMode = false; |
Argyrios Kyrtzidis | 6b4ff04 | 2011-09-27 17:32:05 +0000 | [diff] [blame] | 394 | |
| 395 | if (Callbacks) { |
| 396 | SourceLocation BeginLoc = ElseLoc.isValid() ? ElseLoc : IfTokenLoc; |
| 397 | Callbacks->SourceRangeSkipped(SourceRange(BeginLoc, Tok.getLocation())); |
| 398 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 401 | void Preprocessor::PTHSkipExcludedConditionalBlock() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | |
| 403 | while (1) { |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 404 | assert(CurPTHLexer); |
| 405 | assert(CurPTHLexer->LexingRawMode == false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 407 | // Skip to the next '#else', '#elif', or #endif. |
| 408 | if (CurPTHLexer->SkipBlock()) { |
| 409 | // We have reached an #endif. Both the '#' and 'endif' tokens |
| 410 | // have been consumed by the PTHLexer. Just pop off the condition level. |
| 411 | PPConditionalInfo CondInfo; |
| 412 | bool InCond = CurPTHLexer->popConditionalLevel(CondInfo); |
Jeffrey Yasskin | c6ed729 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 413 | (void)InCond; // Silence warning in no-asserts mode. |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 414 | assert(!InCond && "Can't be skipping if not in a conditional!"); |
| 415 | break; |
| 416 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 418 | // We have reached a '#else' or '#elif'. Lex the next token to get |
| 419 | // the directive flavor. |
| 420 | Token Tok; |
| 421 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 423 | // We can actually look up the IdentifierInfo here since we aren't in |
| 424 | // raw mode. |
| 425 | tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID(); |
| 426 | |
| 427 | if (K == tok::pp_else) { |
| 428 | // #else: Enter the else condition. We aren't in a nested condition |
| 429 | // since we skip those. We're always in the one matching the last |
| 430 | // blocked we skipped. |
| 431 | PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); |
| 432 | // Note that we've seen a #else in this conditional. |
| 433 | CondInfo.FoundElse = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 435 | // If the #if block wasn't entered then enter the #else block now. |
| 436 | if (!CondInfo.FoundNonSkip) { |
| 437 | CondInfo.FoundNonSkip = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 439 | // Scan until the eod token. |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 440 | CurPTHLexer->ParsingPreprocessorDirective = true; |
Daniel Dunbar | 8533bd5 | 2009-04-13 17:57:49 +0000 | [diff] [blame] | 441 | DiscardUntilEndOfDirective(); |
Ted Kremenek | e5680f3 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 442 | CurPTHLexer->ParsingPreprocessorDirective = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 444 | break; |
| 445 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 447 | // Otherwise skip this block. |
| 448 | continue; |
| 449 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 451 | assert(K == tok::pp_elif); |
| 452 | PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); |
| 453 | |
| 454 | // If this is a #elif with a #else before it, report the error. |
| 455 | if (CondInfo.FoundElse) |
| 456 | Diag(Tok, diag::pp_err_elif_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 458 | // If this is in a skipping block or if we're already handled this #if |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | // block, don't bother parsing the condition. We just skip this block. |
Ted Kremenek | 268ee70 | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 460 | if (CondInfo.FoundNonSkip) |
| 461 | continue; |
| 462 | |
| 463 | // Evaluate the condition of the #elif. |
| 464 | IdentifierInfo *IfNDefMacro = 0; |
| 465 | CurPTHLexer->ParsingPreprocessorDirective = true; |
| 466 | bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); |
| 467 | CurPTHLexer->ParsingPreprocessorDirective = false; |
| 468 | |
| 469 | // If this condition is true, enter it! |
| 470 | if (ShouldEnter) { |
| 471 | CondInfo.FoundNonSkip = true; |
| 472 | break; |
| 473 | } |
| 474 | |
| 475 | // Otherwise, skip this block and go to the next one. |
| 476 | continue; |
| 477 | } |
| 478 | } |
| 479 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 480 | /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, |
| 481 | /// return null on failure. isAngled indicates whether the file reference is |
| 482 | /// for system #include's or not (i.e. using <> instead of ""). |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 483 | const FileEntry *Preprocessor::LookupFile( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 484 | StringRef Filename, |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 485 | bool isAngled, |
| 486 | const DirectoryLookup *FromDir, |
| 487 | const DirectoryLookup *&CurDir, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 488 | SmallVectorImpl<char> *SearchPath, |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 489 | SmallVectorImpl<char> *RelativePath, |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 490 | Module **SuggestedModule, |
Douglas Gregor | 1c2e933 | 2011-11-20 17:46:46 +0000 | [diff] [blame] | 491 | bool SkipCache) { |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 492 | // If the header lookup mechanism may be relative to the current file, pass in |
| 493 | // info about where the current file is. |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 494 | const FileEntry *CurFileEnt = 0; |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 495 | if (!FromDir) { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 496 | FileID FID = getCurrentFileLexer()->getFileID(); |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 497 | CurFileEnt = SourceMgr.getFileEntryForID(FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | |
Chris Lattner | be5c64d | 2009-02-04 19:45:07 +0000 | [diff] [blame] | 499 | // If there is no file entry associated with this file, it must be the |
| 500 | // predefines buffer. Any other file is not lexed with a normal lexer, so |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 501 | // it won't be scanned for preprocessor directives. If we have the |
| 502 | // predefines buffer, resolve #include references (which come from the |
| 503 | // -include command line argument) as if they came from the main file, this |
| 504 | // affects file lookup etc. |
| 505 | if (CurFileEnt == 0) { |
Chris Lattner | be5c64d | 2009-02-04 19:45:07 +0000 | [diff] [blame] | 506 | FID = SourceMgr.getMainFileID(); |
| 507 | CurFileEnt = SourceMgr.getFileEntryForID(FID); |
| 508 | } |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 509 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 511 | // Do a standard file entry lookup. |
| 512 | CurDir = CurDirLookup; |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 513 | const FileEntry *FE = HeaderInfo.LookupFile( |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 514 | Filename, isAngled, FromDir, CurDir, CurFileEnt, |
Douglas Gregor | 1c2e933 | 2011-11-20 17:46:46 +0000 | [diff] [blame] | 515 | SearchPath, RelativePath, SuggestedModule, SkipCache); |
Chris Lattner | f45b646 | 2010-01-22 00:14:44 +0000 | [diff] [blame] | 516 | if (FE) return FE; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 518 | // Otherwise, see if this is a subframework header. If so, this is relative |
| 519 | // to one of the headers on the #include stack. Walk the list of the current |
| 520 | // headers on the #include stack and pass them to HeaderInfo. |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 521 | // FIXME: SuggestedModule! |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 522 | if (IsFileLexer()) { |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 523 | if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 524 | if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt, |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 525 | SearchPath, RelativePath))) |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 526 | return FE; |
| 527 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 529 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 530 | IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1]; |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 531 | if (IsFileLexer(ISEntry)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | if ((CurFileEnt = |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 533 | SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID()))) |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 534 | if ((FE = HeaderInfo.LookupSubframeworkHeader( |
| 535 | Filename, CurFileEnt, SearchPath, RelativePath))) |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 536 | return FE; |
| 537 | } |
| 538 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Chris Lattner | 1072509 | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 540 | // Otherwise, we really couldn't find the file. |
| 541 | return 0; |
| 542 | } |
| 543 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 544 | |
| 545 | //===----------------------------------------------------------------------===// |
| 546 | // Preprocessor Directive Handling. |
| 547 | //===----------------------------------------------------------------------===// |
| 548 | |
| 549 | /// HandleDirective - This callback is invoked when the lexer sees a # token |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | /// at the start of a line. This consumes the directive, modifies the |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 551 | /// lexer/preprocessor state, and advances the lexer(s) so that the next token |
| 552 | /// read is the correct one. |
| 553 | void Preprocessor::HandleDirective(Token &Result) { |
| 554 | // FIXME: Traditional: # with whitespace before it not recognized by K&R? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 555 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 556 | // We just parsed a # character at the start of a line, so we're in directive |
| 557 | // mode. Tell the lexer this so any newlines we see will be converted into an |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 558 | // EOD token (which terminates the directive). |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 559 | CurPPLexer->ParsingPreprocessorDirective = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 560 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 561 | ++NumDirectives; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 562 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 563 | // We are about to read a token. For the multiple-include optimization FA to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 564 | // work, we have to remember if we had read any tokens *before* this |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 565 | // pp-directive. |
Chris Lattner | 1d9c54d | 2009-12-14 04:54:40 +0000 | [diff] [blame] | 566 | bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 568 | // Save the '#' token in case we need to return it later. |
| 569 | Token SavedHash = Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 571 | // Read the next token, the directive flavor. This isn't expanded due to |
| 572 | // C99 6.10.3p8. |
| 573 | LexUnexpandedToken(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 575 | // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.: |
| 576 | // #define A(x) #x |
| 577 | // A(abc |
| 578 | // #warning blah |
| 579 | // def) |
| 580 | // If so, the user is relying on non-portable behavior, emit a diagnostic. |
| 581 | if (InMacroArgs) |
| 582 | Diag(Result, diag::ext_embedded_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 583 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 584 | TryAgain: |
| 585 | switch (Result.getKind()) { |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 586 | case tok::eod: |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 587 | return; // null directive. |
| 588 | case tok::comment: |
| 589 | // Handle stuff like "# /*foo*/ define X" in -E -C mode. |
| 590 | LexUnexpandedToken(Result); |
| 591 | goto TryAgain; |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 592 | case tok::code_completion: |
| 593 | if (CodeComplete) |
| 594 | CodeComplete->CodeCompleteDirective( |
| 595 | CurPPLexer->getConditionalStackDepth() > 0); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 596 | setCodeCompletionReached(); |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 597 | return; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 598 | case tok::numeric_constant: // # 7 GNU line marker directive. |
Chris Lattner | 5f607c4 | 2009-03-18 20:41:10 +0000 | [diff] [blame] | 599 | if (getLangOptions().AsmPreprocessor) |
| 600 | break; // # 4 is not a preprocessor directive in .S files. |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 601 | return HandleDigitDirective(Result); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 602 | default: |
| 603 | IdentifierInfo *II = Result.getIdentifierInfo(); |
| 604 | if (II == 0) break; // Not an identifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 605 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 606 | // Ask what the preprocessor keyword ID is. |
| 607 | switch (II->getPPKeywordID()) { |
| 608 | default: break; |
| 609 | // C99 6.10.1 - Conditional Inclusion. |
| 610 | case tok::pp_if: |
| 611 | return HandleIfDirective(Result, ReadAnyTokensBeforeDirective); |
| 612 | case tok::pp_ifdef: |
| 613 | return HandleIfdefDirective(Result, false, true/*not valid for miopt*/); |
| 614 | case tok::pp_ifndef: |
| 615 | return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective); |
| 616 | case tok::pp_elif: |
| 617 | return HandleElifDirective(Result); |
| 618 | case tok::pp_else: |
| 619 | return HandleElseDirective(Result); |
| 620 | case tok::pp_endif: |
| 621 | return HandleEndifDirective(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 622 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 623 | // C99 6.10.2 - Source File Inclusion. |
| 624 | case tok::pp_include: |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 625 | // Handle #include. |
| 626 | return HandleIncludeDirective(SavedHash.getLocation(), Result); |
Chris Lattner | b8e240e | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 627 | case tok::pp___include_macros: |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 628 | // Handle -imacros. |
| 629 | return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 630 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 631 | // C99 6.10.3 - Macro Replacement. |
| 632 | case tok::pp_define: |
| 633 | return HandleDefineDirective(Result); |
| 634 | case tok::pp_undef: |
| 635 | return HandleUndefDirective(Result); |
| 636 | |
| 637 | // C99 6.10.4 - Line Control. |
| 638 | case tok::pp_line: |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 639 | return HandleLineDirective(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 641 | // C99 6.10.5 - Error Directive. |
| 642 | case tok::pp_error: |
| 643 | return HandleUserDiagnosticDirective(Result, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 645 | // C99 6.10.6 - Pragma Directive. |
| 646 | case tok::pp_pragma: |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 647 | return HandlePragmaDirective(PIK_HashPragma); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 648 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 649 | // GNU Extensions. |
| 650 | case tok::pp_import: |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 651 | return HandleImportDirective(SavedHash.getLocation(), Result); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 652 | case tok::pp_include_next: |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 653 | return HandleIncludeNextDirective(SavedHash.getLocation(), Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 655 | case tok::pp_warning: |
| 656 | Diag(Result, diag::ext_pp_warning_directive); |
| 657 | return HandleUserDiagnosticDirective(Result, true); |
| 658 | case tok::pp_ident: |
| 659 | return HandleIdentSCCSDirective(Result); |
| 660 | case tok::pp_sccs: |
| 661 | return HandleIdentSCCSDirective(Result); |
| 662 | case tok::pp_assert: |
| 663 | //isExtension = true; // FIXME: implement #assert |
| 664 | break; |
| 665 | case tok::pp_unassert: |
| 666 | //isExtension = true; // FIXME: implement #unassert |
| 667 | break; |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 668 | |
| 669 | case tok::pp___export_macro__: |
| 670 | return HandleMacroExportDirective(Result); |
Douglas Gregor | aa93a87 | 2011-10-17 15:32:29 +0000 | [diff] [blame] | 671 | case tok::pp___private_macro__: |
| 672 | return HandleMacroPrivateDirective(Result); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 673 | } |
| 674 | break; |
| 675 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 677 | // If this is a .S file, treat unknown # directives as non-preprocessor |
| 678 | // directives. This is important because # may be a comment or introduce |
| 679 | // various pseudo-ops. Just return the # token and push back the following |
| 680 | // token to be lexed next time. |
| 681 | if (getLangOptions().AsmPreprocessor) { |
Daniel Dunbar | 3d399a0 | 2009-07-13 21:48:50 +0000 | [diff] [blame] | 682 | Token *Toks = new Token[2]; |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 683 | // Return the # and the token after it. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 684 | Toks[0] = SavedHash; |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 685 | Toks[1] = Result; |
Chris Lattner | ba3ca52 | 2011-01-06 05:01:51 +0000 | [diff] [blame] | 686 | |
| 687 | // If the second token is a hashhash token, then we need to translate it to |
| 688 | // unknown so the token lexer doesn't try to perform token pasting. |
| 689 | if (Result.is(tok::hashhash)) |
| 690 | Toks[1].setKind(tok::unknown); |
| 691 | |
Chris Lattner | 42aa16c | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 692 | // Enter this token stream so that we re-lex the tokens. Make sure to |
| 693 | // enable macro expansion, in case the token after the # is an identifier |
| 694 | // that is expanded. |
| 695 | EnterTokenStream(Toks, 2, false, true); |
| 696 | return; |
| 697 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 698 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 699 | // If we reached here, the preprocessing token is not valid! |
| 700 | Diag(Result, diag::err_pp_invalid_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 701 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 702 | // Read the rest of the PP line. |
| 703 | DiscardUntilEndOfDirective(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 704 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 705 | // Okay, we're done parsing the directive. |
| 706 | } |
| 707 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 708 | /// GetLineValue - Convert a numeric token into an unsigned value, emitting |
| 709 | /// Diagnostic DiagID if it is invalid, and returning the value in Val. |
| 710 | static bool GetLineValue(Token &DigitTok, unsigned &Val, |
| 711 | unsigned DiagID, Preprocessor &PP) { |
| 712 | if (DigitTok.isNot(tok::numeric_constant)) { |
| 713 | PP.Diag(DigitTok, DiagID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 715 | if (DigitTok.isNot(tok::eod)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 716 | PP.DiscardUntilEndOfDirective(); |
| 717 | return true; |
| 718 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 719 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 720 | llvm::SmallString<64> IntegerBuffer; |
| 721 | IntegerBuffer.resize(DigitTok.getLength()); |
| 722 | const char *DigitTokBegin = &IntegerBuffer[0]; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 723 | bool Invalid = false; |
| 724 | unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid); |
| 725 | if (Invalid) |
| 726 | return true; |
| 727 | |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 728 | // Verify that we have a simple digit-sequence, and compute the value. This |
| 729 | // is always a simple digit string computed in decimal, so we do this manually |
| 730 | // here. |
| 731 | Val = 0; |
| 732 | for (unsigned i = 0; i != ActualLength; ++i) { |
| 733 | if (!isdigit(DigitTokBegin[i])) { |
| 734 | PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i), |
| 735 | diag::err_pp_line_digit_sequence); |
| 736 | PP.DiscardUntilEndOfDirective(); |
| 737 | return true; |
| 738 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 740 | unsigned NextVal = Val*10+(DigitTokBegin[i]-'0'); |
| 741 | if (NextVal < Val) { // overflow. |
| 742 | PP.Diag(DigitTok, DiagID); |
| 743 | PP.DiscardUntilEndOfDirective(); |
| 744 | return true; |
| 745 | } |
| 746 | Val = NextVal; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 747 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 748 | |
| 749 | // Reject 0, this is needed both by #line numbers and flags. |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 750 | if (Val == 0) { |
| 751 | PP.Diag(DigitTok, DiagID); |
| 752 | PP.DiscardUntilEndOfDirective(); |
| 753 | return true; |
| 754 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 756 | if (DigitTokBegin[0] == '0') |
| 757 | PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 758 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 759 | return false; |
| 760 | } |
| 761 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 762 | /// HandleLineDirective - Handle #line directive: C99 6.10.4. The two |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 763 | /// acceptable forms are: |
| 764 | /// # line digit-sequence |
| 765 | /// # line digit-sequence "s-char-sequence" |
| 766 | void Preprocessor::HandleLineDirective(Token &Tok) { |
| 767 | // Read the line # and string argument. Per C99 6.10.4p5, these tokens are |
| 768 | // expanded. |
| 769 | Token DigitTok; |
| 770 | Lex(DigitTok); |
| 771 | |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 772 | // Validate the number and convert it to an unsigned. |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 773 | unsigned LineNo; |
Chris Lattner | dc8c90d | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 774 | if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this)) |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 775 | return; |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 776 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 777 | // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a |
| 778 | // number greater than 2147483647". C90 requires that the line # be <= 32767. |
Eli Friedman | 158ebfb | 2011-10-10 23:35:28 +0000 | [diff] [blame] | 779 | unsigned LineLimit = 32768U; |
| 780 | if (Features.C99 || Features.CPlusPlus0x) |
| 781 | LineLimit = 2147483648U; |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 782 | if (LineNo >= LineLimit) |
| 783 | Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit; |
Richard Smith | 661a996 | 2011-10-15 01:18:56 +0000 | [diff] [blame] | 784 | else if (Features.CPlusPlus0x && LineNo >= 32768U) |
| 785 | Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 786 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 787 | int FilenameID = -1; |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 788 | Token StrTok; |
| 789 | Lex(StrTok); |
| 790 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 791 | // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a |
| 792 | // string followed by eod. |
| 793 | if (StrTok.is(tok::eod)) |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 794 | ; // ok |
| 795 | else if (StrTok.isNot(tok::string_literal)) { |
| 796 | Diag(StrTok, diag::err_pp_line_invalid_filename); |
| 797 | DiscardUntilEndOfDirective(); |
| 798 | return; |
| 799 | } else { |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 800 | // Parse and validate the string, converting it into a unique ID. |
| 801 | StringLiteralParser Literal(&StrTok, 1, *this); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 802 | assert(Literal.isAscii() && "Didn't allow wide strings in"); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 803 | if (Literal.hadError) |
| 804 | return DiscardUntilEndOfDirective(); |
| 805 | if (Literal.Pascal) { |
| 806 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
| 807 | return DiscardUntilEndOfDirective(); |
| 808 | } |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 809 | FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 811 | // Verify that there is nothing after the string, other than EOD. Because |
Chris Lattner | ab82f41 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 812 | // of C99 6.10.4p5, macros that expand to empty tokens are ok. |
| 813 | CheckEndOfDirective("line", true); |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 814 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 815 | |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 816 | SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 817 | |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 818 | if (Callbacks) |
Chris Lattner | 86d0ef7 | 2010-04-14 04:28:50 +0000 | [diff] [blame] | 819 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), |
| 820 | PPCallbacks::RenameFile, |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 821 | SrcMgr::C_User); |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 824 | /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line |
| 825 | /// marker directive. |
| 826 | static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit, |
| 827 | bool &IsSystemHeader, bool &IsExternCHeader, |
| 828 | Preprocessor &PP) { |
| 829 | unsigned FlagVal; |
| 830 | Token FlagTok; |
| 831 | PP.Lex(FlagTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 832 | if (FlagTok.is(tok::eod)) return false; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 833 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) |
| 834 | return true; |
| 835 | |
| 836 | if (FlagVal == 1) { |
| 837 | IsFileEntry = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 839 | PP.Lex(FlagTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 840 | if (FlagTok.is(tok::eod)) return false; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 841 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) |
| 842 | return true; |
| 843 | } else if (FlagVal == 2) { |
| 844 | IsFileExit = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 846 | SourceManager &SM = PP.getSourceManager(); |
| 847 | // If we are leaving the current presumed file, check to make sure the |
| 848 | // presumed include stack isn't empty! |
| 849 | FileID CurFileID = |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 850 | SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first; |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 851 | PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation()); |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 852 | if (PLoc.isInvalid()) |
| 853 | return true; |
| 854 | |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 855 | // If there is no include loc (main file) or if the include loc is in a |
| 856 | // different physical file, then we aren't in a "1" line marker flag region. |
| 857 | SourceLocation IncLoc = PLoc.getIncludeLoc(); |
| 858 | if (IncLoc.isInvalid() || |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 859 | SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) { |
Chris Lattner | 137b6a6 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 860 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop); |
| 861 | PP.DiscardUntilEndOfDirective(); |
| 862 | return true; |
| 863 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 864 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 865 | PP.Lex(FlagTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 866 | if (FlagTok.is(tok::eod)) return false; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 867 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) |
| 868 | return true; |
| 869 | } |
| 870 | |
| 871 | // We must have 3 if there are still flags. |
| 872 | if (FlagVal != 3) { |
| 873 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 874 | PP.DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 875 | return true; |
| 876 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 877 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 878 | IsSystemHeader = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 879 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 880 | PP.Lex(FlagTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 881 | if (FlagTok.is(tok::eod)) return false; |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 882 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 883 | return true; |
| 884 | |
| 885 | // We must have 4 if there is yet another flag. |
| 886 | if (FlagVal != 4) { |
| 887 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 888 | PP.DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 889 | return true; |
| 890 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 891 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 892 | IsExternCHeader = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 893 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 894 | PP.Lex(FlagTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 895 | if (FlagTok.is(tok::eod)) return false; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 896 | |
| 897 | // There are no more valid flags here. |
| 898 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 899 | PP.DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 900 | return true; |
| 901 | } |
| 902 | |
| 903 | /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is |
| 904 | /// one of the following forms: |
| 905 | /// |
| 906 | /// # 42 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 907 | /// # 42 "file" ('1' | '2')? |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 908 | /// # 42 "file" ('1' | '2')? '3' '4'? |
| 909 | /// |
| 910 | void Preprocessor::HandleDigitDirective(Token &DigitTok) { |
| 911 | // Validate the number and convert it to an unsigned. GNU does not have a |
| 912 | // line # limit other than it fit in 32-bits. |
| 913 | unsigned LineNo; |
| 914 | if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer, |
| 915 | *this)) |
| 916 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 918 | Token StrTok; |
| 919 | Lex(StrTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 920 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 921 | bool IsFileEntry = false, IsFileExit = false; |
| 922 | bool IsSystemHeader = false, IsExternCHeader = false; |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 923 | int FilenameID = -1; |
| 924 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 925 | // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a |
| 926 | // string followed by eod. |
| 927 | if (StrTok.is(tok::eod)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 928 | ; // ok |
| 929 | else if (StrTok.isNot(tok::string_literal)) { |
| 930 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 931 | return DiscardUntilEndOfDirective(); |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 932 | } else { |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 933 | // Parse and validate the string, converting it into a unique ID. |
| 934 | StringLiteralParser Literal(&StrTok, 1, *this); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 935 | assert(Literal.isAscii() && "Didn't allow wide strings in"); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 936 | if (Literal.hadError) |
| 937 | return DiscardUntilEndOfDirective(); |
| 938 | if (Literal.Pascal) { |
| 939 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); |
| 940 | return DiscardUntilEndOfDirective(); |
| 941 | } |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 942 | FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 943 | |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 944 | // If a filename was present, read any flags that are present. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 945 | if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 946 | IsSystemHeader, IsExternCHeader, *this)) |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 947 | return; |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 948 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 949 | |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 950 | // Create a line note with this information. |
| 951 | SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | IsFileEntry, IsFileExit, |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 953 | IsSystemHeader, IsExternCHeader); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 954 | |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 955 | // If the preprocessor has callbacks installed, notify them of the #line |
| 956 | // change. This is used so that the line marker comes out in -E mode for |
| 957 | // example. |
| 958 | if (Callbacks) { |
| 959 | PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile; |
| 960 | if (IsFileEntry) |
| 961 | Reason = PPCallbacks::EnterFile; |
| 962 | else if (IsFileExit) |
| 963 | Reason = PPCallbacks::ExitFile; |
| 964 | SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User; |
| 965 | if (IsExternCHeader) |
| 966 | FileKind = SrcMgr::C_ExternCSystem; |
| 967 | else if (IsSystemHeader) |
| 968 | FileKind = SrcMgr::C_System; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | |
Chris Lattner | 86d0ef7 | 2010-04-14 04:28:50 +0000 | [diff] [blame] | 970 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind); |
Chris Lattner | 1662938 | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 971 | } |
Chris Lattner | 478a18e | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | |
Chris Lattner | 099dd05 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 975 | /// HandleUserDiagnosticDirective - Handle a #warning or #error directive. |
| 976 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | void Preprocessor::HandleUserDiagnosticDirective(Token &Tok, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 978 | bool isWarning) { |
Chris Lattner | 099dd05 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 979 | // PTH doesn't emit #warning or #error directives. |
| 980 | if (CurPTHLexer) |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 981 | return CurPTHLexer->DiscardToEndOfLine(); |
| 982 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 983 | // Read the rest of the line raw. We do this because we don't want macros |
| 984 | // to be expanded and we don't require that the tokens be valid preprocessing |
| 985 | // tokens. For example, this is allowed: "#warning ` 'foo". GCC does |
| 986 | // collapse multiple consequtive white space between tokens, but this isn't |
| 987 | // specified by the standard. |
Chris Lattner | 359cc44 | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 988 | std::string Message = CurLexer->ReadToEndOfLine(); |
| 989 | if (isWarning) |
| 990 | Diag(Tok, diag::pp_hash_warning) << Message; |
| 991 | else |
| 992 | Diag(Tok, diag::err_pp_hash_error) << Message; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive. |
| 996 | /// |
| 997 | void Preprocessor::HandleIdentSCCSDirective(Token &Tok) { |
| 998 | // Yes, this directive is an extension. |
| 999 | Diag(Tok, diag::ext_pp_ident_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1000 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1001 | // Read the string argument. |
| 1002 | Token StrTok; |
| 1003 | Lex(StrTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1005 | // If the token kind isn't a string, it's a malformed directive. |
| 1006 | if (StrTok.isNot(tok::string_literal) && |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1007 | StrTok.isNot(tok::wide_string_literal)) { |
| 1008 | Diag(StrTok, diag::err_pp_malformed_ident); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1009 | if (StrTok.isNot(tok::eod)) |
Chris Lattner | 099dd05 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 1010 | DiscardUntilEndOfDirective(); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1011 | return; |
| 1012 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1013 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1014 | // Verify that there is nothing after the string, other than EOD. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1015 | CheckEndOfDirective("ident"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1016 | |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 1017 | if (Callbacks) { |
| 1018 | bool Invalid = false; |
| 1019 | std::string Str = getSpelling(StrTok, &Invalid); |
| 1020 | if (!Invalid) |
| 1021 | Callbacks->Ident(Tok.getLocation(), Str); |
| 1022 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 1025 | /// \brief Handle a #__export_macro__ directive. |
| 1026 | void Preprocessor::HandleMacroExportDirective(Token &Tok) { |
| 1027 | Token MacroNameTok; |
| 1028 | ReadMacroName(MacroNameTok, 2); |
| 1029 | |
| 1030 | // Error reading macro name? If so, diagnostic already issued. |
| 1031 | if (MacroNameTok.is(tok::eod)) |
| 1032 | return; |
| 1033 | |
| 1034 | // Check to see if this is the last token on the #__export_macro__ line. |
| 1035 | CheckEndOfDirective("__export_macro__"); |
| 1036 | |
| 1037 | // Okay, we finally have a valid identifier to undef. |
| 1038 | MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo()); |
| 1039 | |
| 1040 | // If the macro is not defined, this is an error. |
| 1041 | if (MI == 0) { |
Douglas Gregor | aa93a87 | 2011-10-17 15:32:29 +0000 | [diff] [blame] | 1042 | Diag(MacroNameTok, diag::err_pp_visibility_non_macro) |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 1043 | << MacroNameTok.getIdentifierInfo(); |
| 1044 | return; |
| 1045 | } |
| 1046 | |
| 1047 | // Note that this macro has now been exported. |
Douglas Gregor | aa93a87 | 2011-10-17 15:32:29 +0000 | [diff] [blame] | 1048 | MI->setVisibility(/*IsPublic=*/true, MacroNameTok.getLocation()); |
| 1049 | |
| 1050 | // If this macro definition came from a PCH file, mark it |
| 1051 | // as having changed since serialization. |
| 1052 | if (MI->isFromAST()) |
| 1053 | MI->setChangedAfterLoad(); |
| 1054 | } |
| 1055 | |
| 1056 | /// \brief Handle a #__private_macro__ directive. |
| 1057 | void Preprocessor::HandleMacroPrivateDirective(Token &Tok) { |
| 1058 | Token MacroNameTok; |
| 1059 | ReadMacroName(MacroNameTok, 2); |
| 1060 | |
| 1061 | // Error reading macro name? If so, diagnostic already issued. |
| 1062 | if (MacroNameTok.is(tok::eod)) |
| 1063 | return; |
| 1064 | |
| 1065 | // Check to see if this is the last token on the #__private_macro__ line. |
| 1066 | CheckEndOfDirective("__private_macro__"); |
| 1067 | |
| 1068 | // Okay, we finally have a valid identifier to undef. |
| 1069 | MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo()); |
| 1070 | |
| 1071 | // If the macro is not defined, this is an error. |
| 1072 | if (MI == 0) { |
| 1073 | Diag(MacroNameTok, diag::err_pp_visibility_non_macro) |
| 1074 | << MacroNameTok.getIdentifierInfo(); |
| 1075 | return; |
| 1076 | } |
| 1077 | |
| 1078 | // Note that this macro has now been marked private. |
| 1079 | MI->setVisibility(/*IsPublic=*/false, MacroNameTok.getLocation()); |
Douglas Gregor | 7143aab | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 1080 | |
| 1081 | // If this macro definition came from a PCH file, mark it |
| 1082 | // as having changed since serialization. |
| 1083 | if (MI->isFromAST()) |
| 1084 | MI->setChangedAfterLoad(); |
| 1085 | } |
| 1086 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1087 | //===----------------------------------------------------------------------===// |
| 1088 | // Preprocessor Include Directive Handling. |
| 1089 | //===----------------------------------------------------------------------===// |
| 1090 | |
| 1091 | /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully |
| 1092 | /// checked and spelled filename, e.g. as an operand of #include. This returns |
| 1093 | /// true if the input filename was in <>'s or false if it were in ""'s. The |
| 1094 | /// caller is expected to provide a buffer that is large enough to hold the |
| 1095 | /// spelling of the filename, but is also expected to handle the case when |
| 1096 | /// this method decides to use a different buffer. |
| 1097 | bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1098 | StringRef &Buffer) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1099 | // Get the text form of the filename. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1100 | assert(!Buffer.empty() && "Can't have tokens with empty spellings!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1102 | // Make sure the filename is <x> or "x". |
| 1103 | bool isAngled; |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1104 | if (Buffer[0] == '<') { |
| 1105 | if (Buffer.back() != '>') { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1106 | Diag(Loc, diag::err_pp_expects_filename); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1107 | Buffer = StringRef(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1108 | return true; |
| 1109 | } |
| 1110 | isAngled = true; |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1111 | } else if (Buffer[0] == '"') { |
| 1112 | if (Buffer.back() != '"') { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1113 | Diag(Loc, diag::err_pp_expects_filename); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1114 | Buffer = StringRef(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1115 | return true; |
| 1116 | } |
| 1117 | isAngled = false; |
| 1118 | } else { |
| 1119 | Diag(Loc, diag::err_pp_expects_filename); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1120 | Buffer = StringRef(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1121 | return true; |
| 1122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1124 | // Diagnose #include "" as invalid. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1125 | if (Buffer.size() <= 2) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1126 | Diag(Loc, diag::err_pp_empty_filename); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1127 | Buffer = StringRef(); |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1128 | return true; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1129 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1130 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1131 | // Skip the brackets. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1132 | Buffer = Buffer.substr(1, Buffer.size()-2); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1133 | return isAngled; |
| 1134 | } |
| 1135 | |
| 1136 | /// ConcatenateIncludeName - Handle cases where the #include name is expanded |
| 1137 | /// from a macro as multiple tokens, which need to be glued together. This |
| 1138 | /// occurs for code like: |
| 1139 | /// #define FOO <a/b.h> |
| 1140 | /// #include FOO |
| 1141 | /// because in this case, "<a/b.h>" is returned as 7 tokens, not one. |
| 1142 | /// |
| 1143 | /// This code concatenates and consumes tokens up to the '>' token. It returns |
| 1144 | /// false if the > was found, otherwise it returns true if it finds and consumes |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1145 | /// the EOD marker. |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1146 | bool Preprocessor::ConcatenateIncludeName( |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1147 | llvm::SmallString<128> &FilenameBuffer, |
| 1148 | SourceLocation &End) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1149 | Token CurTok; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1150 | |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1151 | Lex(CurTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1152 | while (CurTok.isNot(tok::eod)) { |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1153 | End = CurTok.getLocation(); |
| 1154 | |
Douglas Gregor | 25bb03b | 2010-12-09 23:35:36 +0000 | [diff] [blame] | 1155 | // FIXME: Provide code completion for #includes. |
| 1156 | if (CurTok.is(tok::code_completion)) { |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1157 | setCodeCompletionReached(); |
Douglas Gregor | 25bb03b | 2010-12-09 23:35:36 +0000 | [diff] [blame] | 1158 | Lex(CurTok); |
| 1159 | continue; |
| 1160 | } |
| 1161 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1162 | // Append the spelling of this token to the buffer. If there was a space |
| 1163 | // before it, add it now. |
| 1164 | if (CurTok.hasLeadingSpace()) |
| 1165 | FilenameBuffer.push_back(' '); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1166 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1167 | // Get the spelling of the token, directly into FilenameBuffer if possible. |
| 1168 | unsigned PreAppendSize = FilenameBuffer.size(); |
| 1169 | FilenameBuffer.resize(PreAppendSize+CurTok.getLength()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1171 | const char *BufPtr = &FilenameBuffer[PreAppendSize]; |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1172 | unsigned ActualLen = getSpelling(CurTok, BufPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1173 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1174 | // If the token was spelled somewhere else, copy it into FilenameBuffer. |
| 1175 | if (BufPtr != &FilenameBuffer[PreAppendSize]) |
| 1176 | memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1178 | // Resize FilenameBuffer to the correct size. |
| 1179 | if (CurTok.getLength() != ActualLen) |
| 1180 | FilenameBuffer.resize(PreAppendSize+ActualLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1181 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1182 | // If we found the '>' marker, return success. |
| 1183 | if (CurTok.is(tok::greater)) |
| 1184 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1185 | |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1186 | Lex(CurTok); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1189 | // If we hit the eod marker, emit an error and return true so that the caller |
| 1190 | // knows the EOD has been read. |
John Thompson | a28cc09 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1191 | Diag(CurTok.getLocation(), diag::err_pp_expects_filename); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1192 | return true; |
| 1193 | } |
| 1194 | |
| 1195 | /// HandleIncludeDirective - The "#include" tokens have just been read, read the |
| 1196 | /// file to be included from the lexer, then include it! This is a common |
| 1197 | /// routine with functionality shared between #include, #include_next and |
Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 1198 | /// #import. LookupFrom is set when this is a #include_next directive, it |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | /// specifies the file to start searching from. |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1200 | void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc, |
| 1201 | Token &IncludeTok, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1202 | const DirectoryLookup *LookupFrom, |
| 1203 | bool isImport) { |
| 1204 | |
| 1205 | Token FilenameTok; |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1206 | CurPPLexer->LexIncludeFilename(FilenameTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1208 | // Reserve a buffer to get the spelling. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1209 | llvm::SmallString<128> FilenameBuffer; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1210 | StringRef Filename; |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1211 | SourceLocation End; |
Douglas Gregor | e3a8256 | 2011-11-30 18:02:36 +0000 | [diff] [blame] | 1212 | SourceLocation CharEnd; // the end of this directive, in characters |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1213 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1214 | switch (FilenameTok.getKind()) { |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1215 | case tok::eod: |
| 1216 | // If the token kind is EOD, the error has already been diagnosed. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1217 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1219 | case tok::angle_string_literal: |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 1220 | case tok::string_literal: |
| 1221 | Filename = getSpelling(FilenameTok, FilenameBuffer); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1222 | End = FilenameTok.getLocation(); |
Douglas Gregor | e3a8256 | 2011-11-30 18:02:36 +0000 | [diff] [blame] | 1223 | CharEnd = End.getLocWithOffset(Filename.size()); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1224 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1225 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1226 | case tok::less: |
| 1227 | // This could be a <foo/bar.h> file coming from a macro expansion. In this |
| 1228 | // case, glue the tokens together into FilenameBuffer and interpret those. |
| 1229 | FilenameBuffer.push_back('<'); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1230 | if (ConcatenateIncludeName(FilenameBuffer, End)) |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1231 | return; // Found <eod> but no ">"? Diagnostic already emitted. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1232 | Filename = FilenameBuffer.str(); |
Douglas Gregor | e3a8256 | 2011-11-30 18:02:36 +0000 | [diff] [blame] | 1233 | CharEnd = getLocForEndOfToken(End); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1234 | break; |
| 1235 | default: |
| 1236 | Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); |
| 1237 | DiscardUntilEndOfDirective(); |
| 1238 | return; |
| 1239 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1240 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1241 | bool isAngled = |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1242 | GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1243 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 1244 | // error. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1245 | if (Filename.empty()) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1246 | DiscardUntilEndOfDirective(); |
| 1247 | return; |
| 1248 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1250 | // Verify that there is nothing after the filename, other than EOD. Note that |
Chris Lattner | 9cb51ce | 2009-04-17 23:56:52 +0000 | [diff] [blame] | 1251 | // we allow macros that expand to nothing after the filename, because this |
| 1252 | // falls into the category of "#include pp-tokens new-line" specified in |
| 1253 | // C99 6.10.2p4. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 1254 | CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1255 | |
| 1256 | // Check that we don't have infinite #include recursion. |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1257 | if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) { |
| 1258 | Diag(FilenameTok, diag::err_pp_include_too_deep); |
| 1259 | return; |
| 1260 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1262 | // Complain about attempts to #include files in an audit pragma. |
| 1263 | if (PragmaARCCFCodeAuditedLoc.isValid()) { |
| 1264 | Diag(HashLoc, diag::err_pp_include_in_arc_cf_code_audited); |
| 1265 | Diag(PragmaARCCFCodeAuditedLoc, diag::note_pragma_entered_here); |
| 1266 | |
| 1267 | // Immediately leave the pragma. |
| 1268 | PragmaARCCFCodeAuditedLoc = SourceLocation(); |
| 1269 | } |
| 1270 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1271 | // Search include directories. |
| 1272 | const DirectoryLookup *CurDir; |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 1273 | llvm::SmallString<1024> SearchPath; |
| 1274 | llvm::SmallString<1024> RelativePath; |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 1275 | // We get the raw path only if we have 'Callbacks' to which we later pass |
| 1276 | // the path. |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 1277 | Module *SuggestedModule = 0; |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 1278 | const FileEntry *File = LookupFile( |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 1279 | Filename, isAngled, LookupFrom, CurDir, |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 1280 | Callbacks ? &SearchPath : NULL, Callbacks ? &RelativePath : NULL, |
| 1281 | AutoModuleImport? &SuggestedModule : 0); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1282 | |
Douglas Gregor | 8cfbe6a | 2011-11-30 18:12:06 +0000 | [diff] [blame] | 1283 | if (Callbacks) { |
| 1284 | if (!File) { |
| 1285 | // Give the clients a chance to recover. |
| 1286 | llvm::SmallString<128> RecoveryPath; |
| 1287 | if (Callbacks->FileNotFound(Filename, RecoveryPath)) { |
| 1288 | if (const DirectoryEntry *DE = FileMgr.getDirectory(RecoveryPath)) { |
| 1289 | // Add the recovery path to the list of search paths. |
| 1290 | DirectoryLookup DL(DE, SrcMgr::C_User, true, false); |
| 1291 | HeaderInfo.AddSearchPath(DL, isAngled); |
| 1292 | |
| 1293 | // Try the lookup again, skipping the cache. |
| 1294 | File = LookupFile(Filename, isAngled, LookupFrom, CurDir, 0, 0, |
| 1295 | AutoModuleImport ? &SuggestedModule : 0, |
| 1296 | /*SkipCache*/true); |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | // Notify the callback object that we've seen an inclusion directive. |
| 1302 | Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled, File, |
| 1303 | End, SearchPath, RelativePath); |
| 1304 | } |
| 1305 | |
| 1306 | if (File == 0) { |
| 1307 | if (!SuppressIncludeNotFoundError) |
| 1308 | Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; |
| 1309 | return; |
| 1310 | } |
| 1311 | |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 1312 | // If we are supposed to import a module rather than including the header, |
| 1313 | // do so now. |
Douglas Gregor | c69c42e | 2011-11-17 22:44:56 +0000 | [diff] [blame] | 1314 | if (SuggestedModule) { |
Douglas Gregor | 3d3589d | 2011-11-30 00:36:36 +0000 | [diff] [blame] | 1315 | // Compute the module access path corresponding to this module. |
| 1316 | // FIXME: Should we have a second loadModule() overload to avoid this |
| 1317 | // extra lookup step? |
| 1318 | llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path; |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 1319 | for (Module *Mod = SuggestedModule; Mod; Mod = Mod->Parent) |
Douglas Gregor | 3d3589d | 2011-11-30 00:36:36 +0000 | [diff] [blame] | 1320 | Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name), |
| 1321 | FilenameTok.getLocation())); |
| 1322 | std::reverse(Path.begin(), Path.end()); |
| 1323 | |
Douglas Gregor | e3a8256 | 2011-11-30 18:02:36 +0000 | [diff] [blame] | 1324 | // Warn that we're replacing the include/import with a module import. |
| 1325 | llvm::SmallString<128> PathString; |
| 1326 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
| 1327 | if (I) |
| 1328 | PathString += '.'; |
| 1329 | PathString += Path[I].first->getName(); |
| 1330 | } |
| 1331 | int IncludeKind = 0; |
| 1332 | |
| 1333 | switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) { |
| 1334 | case tok::pp_include: |
| 1335 | IncludeKind = 0; |
| 1336 | break; |
| 1337 | |
| 1338 | case tok::pp_import: |
| 1339 | IncludeKind = 1; |
| 1340 | break; |
| 1341 | |
Douglas Gregor | edee969 | 2011-11-30 18:03:26 +0000 | [diff] [blame] | 1342 | case tok::pp_include_next: |
| 1343 | IncludeKind = 2; |
| 1344 | break; |
Douglas Gregor | e3a8256 | 2011-11-30 18:02:36 +0000 | [diff] [blame] | 1345 | |
| 1346 | case tok::pp___include_macros: |
| 1347 | IncludeKind = 3; |
| 1348 | break; |
| 1349 | |
| 1350 | default: |
| 1351 | llvm_unreachable("unknown include directive kind"); |
| 1352 | break; |
| 1353 | } |
| 1354 | |
| 1355 | CharSourceRange ReplaceRange(SourceRange(HashLoc, CharEnd), |
| 1356 | /*IsTokenRange=*/false); |
| 1357 | Diag(HashLoc, diag::warn_auto_module_import) |
| 1358 | << IncludeKind << PathString |
| 1359 | << FixItHint::CreateReplacement(ReplaceRange, |
| 1360 | "__import_module__ " + PathString.str().str() + ";"); |
| 1361 | |
Douglas Gregor | 3d3589d | 2011-11-30 00:36:36 +0000 | [diff] [blame] | 1362 | // Load the module. |
Douglas Gregor | 5e35693 | 2011-12-01 17:11:21 +0000 | [diff] [blame] | 1363 | // If this was an #__include_macros directive, only make macros visible. |
| 1364 | Module::NameVisibilityKind Visibility |
| 1365 | = (IncludeKind == 3)? Module::MacrosVisible : Module::AllVisible; |
Douglas Gregor | 93ebfa6 | 2011-12-02 23:42:12 +0000 | [diff] [blame] | 1366 | TheModuleLoader.loadModule(IncludeTok.getLocation(), Path, Visibility, |
| 1367 | /*IsIncludeDirective=*/true); |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 1368 | return; |
| 1369 | } |
| 1370 | |
Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 1371 | // The #included file will be considered to be a system header if either it is |
| 1372 | // in a system include directory, or if the #includer is a system include |
| 1373 | // header. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1374 | SrcMgr::CharacteristicKind FileCharacter = |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 1375 | std::max(HeaderInfo.getFileDirFlavor(File), |
Chris Lattner | 693faa6 | 2009-01-19 07:59:15 +0000 | [diff] [blame] | 1376 | SourceMgr.getFileCharacteristic(FilenameTok.getLocation())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | |
Chris Lattner | 6fbe3eb | 2010-04-19 20:44:31 +0000 | [diff] [blame] | 1378 | // Ask HeaderInfo if we should enter this #include file. If not, #including |
| 1379 | // this file will have no effect. |
| 1380 | if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) { |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1381 | if (Callbacks) |
Chris Lattner | 6fbe3eb | 2010-04-19 20:44:31 +0000 | [diff] [blame] | 1382 | Callbacks->FileSkipped(*File, FilenameTok, FileCharacter); |
Chris Lattner | 6fbe3eb | 2010-04-19 20:44:31 +0000 | [diff] [blame] | 1383 | return; |
| 1384 | } |
| 1385 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1386 | // Look up the file, create a File ID for it. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 1387 | FileID FID = SourceMgr.createFileID(File, FilenameTok.getLocation(), |
| 1388 | FileCharacter); |
Peter Collingbourne | d57b7ff | 2011-06-30 16:41:03 +0000 | [diff] [blame] | 1389 | assert(!FID.isInvalid() && "Expected valid file ID"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1390 | |
| 1391 | // Finally, if all is good, enter the new file! |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1392 | EnterSourceFile(FID, CurDir, FilenameTok.getLocation()); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | /// HandleIncludeNextDirective - Implements #include_next. |
| 1396 | /// |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1397 | void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc, |
| 1398 | Token &IncludeNextTok) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1399 | Diag(IncludeNextTok, diag::ext_pp_include_next_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1400 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1401 | // #include_next is like #include, except that we start searching after |
| 1402 | // the current found directory. If we can't do this, issue a |
| 1403 | // diagnostic. |
| 1404 | const DirectoryLookup *Lookup = CurDirLookup; |
| 1405 | if (isInPrimaryFile()) { |
| 1406 | Lookup = 0; |
| 1407 | Diag(IncludeNextTok, diag::pp_include_next_in_primary); |
| 1408 | } else if (Lookup == 0) { |
| 1409 | Diag(IncludeNextTok, diag::pp_include_next_absolute_path); |
| 1410 | } else { |
| 1411 | // Start looking up in the next directory. |
| 1412 | ++Lookup; |
| 1413 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1415 | return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | /// HandleImportDirective - Implements #import. |
| 1419 | /// |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1420 | void Preprocessor::HandleImportDirective(SourceLocation HashLoc, |
| 1421 | Token &ImportTok) { |
Chris Lattner | b627c8d | 2009-03-06 04:28:03 +0000 | [diff] [blame] | 1422 | if (!Features.ObjC1) // #import is standard for ObjC. |
| 1423 | Diag(ImportTok, diag::ext_pp_import_directive); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1424 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1425 | return HandleIncludeDirective(HashLoc, ImportTok, 0, true); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Chris Lattner | de07665 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 1428 | /// HandleIncludeMacrosDirective - The -imacros command line option turns into a |
| 1429 | /// pseudo directive in the predefines buffer. This handles it by sucking all |
| 1430 | /// tokens through the preprocessor and discarding them (only keeping the side |
| 1431 | /// effects on the preprocessor). |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1432 | void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc, |
| 1433 | Token &IncludeMacrosTok) { |
Chris Lattner | de07665 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 1434 | // This directive should only occur in the predefines buffer. If not, emit an |
| 1435 | // error and reject it. |
| 1436 | SourceLocation Loc = IncludeMacrosTok.getLocation(); |
| 1437 | if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) { |
| 1438 | Diag(IncludeMacrosTok.getLocation(), |
| 1439 | diag::pp_include_macros_out_of_predefines); |
| 1440 | DiscardUntilEndOfDirective(); |
| 1441 | return; |
| 1442 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | |
Chris Lattner | fd10511 | 2009-04-08 20:53:24 +0000 | [diff] [blame] | 1444 | // Treat this as a normal #include for checking purposes. If this is |
| 1445 | // successful, it will push a new lexer onto the include stack. |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 1446 | HandleIncludeDirective(HashLoc, IncludeMacrosTok, 0, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | |
Chris Lattner | fd10511 | 2009-04-08 20:53:24 +0000 | [diff] [blame] | 1448 | Token TmpTok; |
| 1449 | do { |
| 1450 | Lex(TmpTok); |
| 1451 | assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!"); |
| 1452 | } while (TmpTok.isNot(tok::hashhash)); |
Chris Lattner | de07665 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1455 | //===----------------------------------------------------------------------===// |
| 1456 | // Preprocessor Macro Directive Handling. |
| 1457 | //===----------------------------------------------------------------------===// |
| 1458 | |
| 1459 | /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro |
| 1460 | /// definition has just been read. Lex the rest of the arguments and the |
| 1461 | /// closing ), updating MI with what we learn. Return true if an error occurs |
| 1462 | /// parsing the arg list. |
| 1463 | bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1464 | SmallVector<IdentifierInfo*, 32> Arguments; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1465 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1466 | Token Tok; |
| 1467 | while (1) { |
| 1468 | LexUnexpandedToken(Tok); |
| 1469 | switch (Tok.getKind()) { |
| 1470 | case tok::r_paren: |
| 1471 | // Found the end of the argument list. |
Chris Lattner | cf29e07 | 2009-02-20 22:31:31 +0000 | [diff] [blame] | 1472 | if (Arguments.empty()) // #define FOO() |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1473 | return false; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1474 | // Otherwise we have #define FOO(A,) |
| 1475 | Diag(Tok, diag::err_pp_expected_ident_in_arg_list); |
| 1476 | return true; |
| 1477 | case tok::ellipsis: // #define X(... -> C99 varargs |
Richard Smith | 661a996 | 2011-10-15 01:18:56 +0000 | [diff] [blame] | 1478 | if (!Features.C99) |
| 1479 | Diag(Tok, Features.CPlusPlus0x ? |
| 1480 | diag::warn_cxx98_compat_variadic_macro : |
| 1481 | diag::ext_variadic_macro); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1482 | |
| 1483 | // Lex the token after the identifier. |
| 1484 | LexUnexpandedToken(Tok); |
| 1485 | if (Tok.isNot(tok::r_paren)) { |
| 1486 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1487 | return true; |
| 1488 | } |
| 1489 | // Add the __VA_ARGS__ identifier as an argument. |
| 1490 | Arguments.push_back(Ident__VA_ARGS__); |
| 1491 | MI->setIsC99Varargs(); |
Chris Lattner | 685befe | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1492 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1493 | return false; |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1494 | case tok::eod: // #define X( |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1495 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1496 | return true; |
| 1497 | default: |
| 1498 | // Handle keywords and identifiers here to accept things like |
| 1499 | // #define Foo(for) for. |
| 1500 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1501 | if (II == 0) { |
| 1502 | // #define X(1 |
| 1503 | Diag(Tok, diag::err_pp_invalid_tok_in_arg_list); |
| 1504 | return true; |
| 1505 | } |
| 1506 | |
| 1507 | // If this is already used as an argument, it is used multiple times (e.g. |
| 1508 | // #define X(A,A. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | if (std::find(Arguments.begin(), Arguments.end(), II) != |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1510 | Arguments.end()) { // C99 6.10.3p6 |
Chris Lattner | 6cf3ed7 | 2008-11-19 07:33:58 +0000 | [diff] [blame] | 1511 | Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1512 | return true; |
| 1513 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1514 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1515 | // Add the argument to the macro info. |
| 1516 | Arguments.push_back(II); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1517 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1518 | // Lex the token after the identifier. |
| 1519 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1520 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1521 | switch (Tok.getKind()) { |
| 1522 | default: // #define X(A B |
| 1523 | Diag(Tok, diag::err_pp_expected_comma_in_arg_list); |
| 1524 | return true; |
| 1525 | case tok::r_paren: // #define X(A) |
Chris Lattner | 685befe | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1526 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1527 | return false; |
| 1528 | case tok::comma: // #define X(A, |
| 1529 | break; |
| 1530 | case tok::ellipsis: // #define X(A... -> GCC extension |
| 1531 | // Diagnose extension. |
| 1532 | Diag(Tok, diag::ext_named_variadic_macro); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1533 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1534 | // Lex the token after the identifier. |
| 1535 | LexUnexpandedToken(Tok); |
| 1536 | if (Tok.isNot(tok::r_paren)) { |
| 1537 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); |
| 1538 | return true; |
| 1539 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1541 | MI->setIsGNUVarargs(); |
Chris Lattner | 685befe | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1542 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1543 | return false; |
| 1544 | } |
| 1545 | } |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | /// HandleDefineDirective - Implements #define. This consumes the entire macro |
| 1550 | /// line then lets the caller lex the next real token. |
| 1551 | void Preprocessor::HandleDefineDirective(Token &DefineTok) { |
| 1552 | ++NumDefined; |
| 1553 | |
| 1554 | Token MacroNameTok; |
| 1555 | ReadMacroName(MacroNameTok, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1556 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1557 | // Error reading macro name? If so, diagnostic already issued. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1558 | if (MacroNameTok.is(tok::eod)) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1559 | return; |
| 1560 | |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1561 | Token LastTok = MacroNameTok; |
| 1562 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1563 | // If we are supposed to keep comments in #defines, reenable comment saving |
| 1564 | // mode. |
Ted Kremenek | ac6b06d | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 1565 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1566 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1567 | // Create the new macro. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1568 | MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1569 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1570 | Token Tok; |
| 1571 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1572 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1573 | // If this is a function-like macro definition, parse the argument list, |
| 1574 | // marking each of the identifiers as being used as macro arguments. Also, |
| 1575 | // check other constraints on the first token of the macro body. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1576 | if (Tok.is(tok::eod)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1577 | // If there is no body to this macro, we have no special handling here. |
Chris Lattner | 6272bcf | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1578 | } else if (Tok.hasLeadingSpace()) { |
| 1579 | // This is a normal token with leading space. Clear the leading space |
| 1580 | // marker on the first token to get proper expansion. |
| 1581 | Tok.clearFlag(Token::LeadingSpace); |
| 1582 | } else if (Tok.is(tok::l_paren)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1583 | // This is a function-like macro definition. Read the argument list. |
| 1584 | MI->setIsFunctionLike(); |
| 1585 | if (ReadMacroDefinitionArgList(MI)) { |
| 1586 | // Forget about MI. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1587 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1588 | // Throw away the rest of the line. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1589 | if (CurPPLexer->ParsingPreprocessorDirective) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1590 | DiscardUntilEndOfDirective(); |
| 1591 | return; |
| 1592 | } |
| 1593 | |
Chris Lattner | 8fde597 | 2009-04-19 18:26:34 +0000 | [diff] [blame] | 1594 | // If this is a definition of a variadic C99 function-like macro, not using |
| 1595 | // the GNU named varargs extension, enabled __VA_ARGS__. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1596 | |
Chris Lattner | 8fde597 | 2009-04-19 18:26:34 +0000 | [diff] [blame] | 1597 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 1598 | // This gets unpoisoned where it is allowed. |
| 1599 | assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!"); |
| 1600 | if (MI->isC99Varargs()) |
| 1601 | Ident__VA_ARGS__->setIsPoisoned(false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1602 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1603 | // Read the first token after the arg list for down below. |
| 1604 | LexUnexpandedToken(Tok); |
Eli Friedman | 158ebfb | 2011-10-10 23:35:28 +0000 | [diff] [blame] | 1605 | } else if (Features.C99 || Features.CPlusPlus0x) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1606 | // C99 requires whitespace between the macro definition and the body. Emit |
| 1607 | // a diagnostic for something like "#define X+". |
Chris Lattner | 6272bcf | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1608 | Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1609 | } else { |
Chris Lattner | 6272bcf | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1610 | // C90 6.8 TC1 says: "In the definition of an object-like macro, if the |
| 1611 | // first character of a replacement list is not a character required by |
| 1612 | // subclause 5.2.1, then there shall be white-space separation between the |
| 1613 | // identifier and the replacement list.". 5.2.1 lists this set: |
| 1614 | // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which |
| 1615 | // is irrelevant here. |
| 1616 | bool isInvalid = false; |
| 1617 | if (Tok.is(tok::at)) // @ is not in the list above. |
| 1618 | isInvalid = true; |
| 1619 | else if (Tok.is(tok::unknown)) { |
| 1620 | // If we have an unknown token, it is something strange like "`". Since |
| 1621 | // all of valid characters would have lexed into a single character |
| 1622 | // token of some sort, we know this is not a valid case. |
| 1623 | isInvalid = true; |
| 1624 | } |
| 1625 | if (isInvalid) |
| 1626 | Diag(Tok, diag::ext_missing_whitespace_after_macro_name); |
| 1627 | else |
| 1628 | Diag(Tok, diag::warn_missing_whitespace_after_macro_name); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1629 | } |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1630 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1631 | if (!Tok.is(tok::eod)) |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1632 | LastTok = Tok; |
| 1633 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1634 | // Read the rest of the macro body. |
| 1635 | if (MI->isObjectLike()) { |
| 1636 | // Object-like macros are very simple, just read their body. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1637 | while (Tok.isNot(tok::eod)) { |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1638 | LastTok = Tok; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1639 | MI->AddTokenToBody(Tok); |
| 1640 | // Get the next token of the macro. |
| 1641 | LexUnexpandedToken(Tok); |
| 1642 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1643 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1644 | } else { |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1645 | // Otherwise, read the body of a function-like macro. While we are at it, |
| 1646 | // check C99 6.10.3.2p1: ensure that # operators are followed by macro |
| 1647 | // parameters in function-like macro expansions. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1648 | while (Tok.isNot(tok::eod)) { |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1649 | LastTok = Tok; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1650 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1651 | if (Tok.isNot(tok::hash)) { |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1652 | MI->AddTokenToBody(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1654 | // Get the next token of the macro. |
| 1655 | LexUnexpandedToken(Tok); |
| 1656 | continue; |
| 1657 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1659 | // Get the next token of the macro. |
| 1660 | LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1661 | |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1662 | // Check for a valid macro arg identifier. |
| 1663 | if (Tok.getIdentifierInfo() == 0 || |
| 1664 | MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) { |
| 1665 | |
| 1666 | // If this is assembler-with-cpp mode, we accept random gibberish after |
| 1667 | // the '#' because '#' is often a comment character. However, change |
| 1668 | // the kind of the token to tok::unknown so that the preprocessor isn't |
| 1669 | // confused. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1670 | if (getLangOptions().AsmPreprocessor && Tok.isNot(tok::eod)) { |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1671 | LastTok.setKind(tok::unknown); |
| 1672 | } else { |
| 1673 | Diag(Tok, diag::err_pp_stringize_not_parameter); |
| 1674 | ReleaseMacroInfo(MI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1675 | |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1676 | // Disable __VA_ARGS__ again. |
| 1677 | Ident__VA_ARGS__->setIsPoisoned(true); |
| 1678 | return; |
| 1679 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1680 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1681 | |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1682 | // Things look ok, add the '#' and param name tokens to the macro. |
| 1683 | MI->AddTokenToBody(LastTok); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1684 | MI->AddTokenToBody(Tok); |
Chris Lattner | 3240469 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1685 | LastTok = Tok; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1686 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1687 | // Get the next token of the macro. |
| 1688 | LexUnexpandedToken(Tok); |
| 1689 | } |
| 1690 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1691 | |
| 1692 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1693 | // Disable __VA_ARGS__ again. |
| 1694 | Ident__VA_ARGS__->setIsPoisoned(true); |
| 1695 | |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1696 | // Check that there is no paste (##) operator at the beginning or end of the |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1697 | // replacement list. |
| 1698 | unsigned NumTokens = MI->getNumTokens(); |
| 1699 | if (NumTokens != 0) { |
| 1700 | if (MI->getReplacementToken(0).is(tok::hashhash)) { |
| 1701 | Diag(MI->getReplacementToken(0), diag::err_paste_at_start); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1702 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1703 | return; |
| 1704 | } |
| 1705 | if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) { |
| 1706 | Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1707 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1708 | return; |
| 1709 | } |
| 1710 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1711 | |
Chris Lattner | 2451b52 | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1712 | MI->setDefinitionEndLoc(LastTok.getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1713 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1714 | // Finally, if this identifier already had a macro defined for it, verify that |
| 1715 | // the macro bodies are identical and free the old definition. |
| 1716 | if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) { |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1717 | // It is very common for system headers to have tons of macro redefinitions |
| 1718 | // and for warnings to be disabled in system headers. If this is the case, |
| 1719 | // then don't bother calling MacroInfo::isIdenticalTo. |
Chris Lattner | 7f549df | 2009-03-13 21:17:23 +0000 | [diff] [blame] | 1720 | if (!getDiagnostics().getSuppressSystemWarnings() || |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1721 | !SourceMgr.isInSystemHeader(DefineTok.getLocation())) { |
Argyrios Kyrtzidis | a33e050 | 2011-01-18 19:50:15 +0000 | [diff] [blame] | 1722 | if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused()) |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1723 | Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1724 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1725 | // Macros must be identical. This means all tokens and whitespace |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1726 | // separation must be the same. C99 6.10.3.2. |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1727 | if (!OtherMI->isAllowRedefinitionsWithoutWarning() && |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1728 | !MI->isIdenticalTo(*OtherMI, *this)) { |
Chris Lattner | 41c3ae1 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1729 | Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef) |
| 1730 | << MacroNameTok.getIdentifierInfo(); |
| 1731 | Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition); |
| 1732 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1733 | } |
Argyrios Kyrtzidis | a33e050 | 2011-01-18 19:50:15 +0000 | [diff] [blame] | 1734 | if (OtherMI->isWarnIfUnused()) |
| 1735 | WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc()); |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1736 | ReleaseMacroInfo(OtherMI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1737 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1738 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1739 | setMacroInfo(MacroNameTok.getIdentifierInfo(), MI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1741 | assert(!MI->isUsed()); |
| 1742 | // If we need warning for not using the macro, add its location in the |
| 1743 | // warn-because-unused-macro set. If it gets used it will be removed from set. |
| 1744 | if (isInPrimaryFile() && // don't warn for include'd macros. |
| 1745 | Diags->getDiagnosticLevel(diag::pp_macro_not_used, |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1746 | MI->getDefinitionLoc()) != DiagnosticsEngine::Ignored) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1747 | MI->setIsWarnIfUnused(true); |
| 1748 | WarnUnusedMacroLocs.insert(MI->getDefinitionLoc()); |
| 1749 | } |
| 1750 | |
Chris Lattner | f4a72b0 | 2009-04-12 01:39:54 +0000 | [diff] [blame] | 1751 | // If the callbacks want to know, tell them about the macro definition. |
| 1752 | if (Callbacks) |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 1753 | Callbacks->MacroDefined(MacroNameTok, MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | /// HandleUndefDirective - Implements #undef. |
| 1757 | /// |
| 1758 | void Preprocessor::HandleUndefDirective(Token &UndefTok) { |
| 1759 | ++NumUndefined; |
| 1760 | |
| 1761 | Token MacroNameTok; |
| 1762 | ReadMacroName(MacroNameTok, 2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1763 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1764 | // Error reading macro name? If so, diagnostic already issued. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1765 | if (MacroNameTok.is(tok::eod)) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1766 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1768 | // Check to see if this is the last token on the #undef line. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1769 | CheckEndOfDirective("undef"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1770 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1771 | // Okay, we finally have a valid identifier to undef. |
| 1772 | MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1773 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1774 | // If the macro is not defined, this is a noop undef, just return. |
| 1775 | if (MI == 0) return; |
| 1776 | |
Argyrios Kyrtzidis | 1f8dcfc | 2011-07-11 20:39:47 +0000 | [diff] [blame] | 1777 | if (!MI->isUsed() && MI->isWarnIfUnused()) |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1778 | Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 41c1747 | 2009-04-21 03:42:09 +0000 | [diff] [blame] | 1779 | |
| 1780 | // If the callbacks want to know, tell them about the macro #undef. |
| 1781 | if (Callbacks) |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 1782 | Callbacks->MacroUndefined(MacroNameTok, MI); |
Chris Lattner | 41c1747 | 2009-04-21 03:42:09 +0000 | [diff] [blame] | 1783 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1784 | if (MI->isWarnIfUnused()) |
| 1785 | WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); |
| 1786 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1787 | // Free macro definition. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1788 | ReleaseMacroInfo(MI); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1789 | setMacroInfo(MacroNameTok.getIdentifierInfo(), 0); |
| 1790 | } |
| 1791 | |
| 1792 | |
| 1793 | //===----------------------------------------------------------------------===// |
| 1794 | // Preprocessor Conditional Directive Handling. |
| 1795 | //===----------------------------------------------------------------------===// |
| 1796 | |
| 1797 | /// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is |
| 1798 | /// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true |
| 1799 | /// if any tokens have been returned or pp-directives activated before this |
| 1800 | /// #ifndef has been lexed. |
| 1801 | /// |
| 1802 | void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef, |
| 1803 | bool ReadAnyTokensBeforeDirective) { |
| 1804 | ++NumIf; |
| 1805 | Token DirectiveTok = Result; |
| 1806 | |
| 1807 | Token MacroNameTok; |
| 1808 | ReadMacroName(MacroNameTok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1809 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1810 | // Error reading macro name? If so, diagnostic already issued. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1811 | if (MacroNameTok.is(tok::eod)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1812 | // Skip code until we get to #endif. This helps with recovery by not |
| 1813 | // emitting an error when the #endif is reached. |
| 1814 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), |
| 1815 | /*Foundnonskip*/false, /*FoundElse*/false); |
| 1816 | return; |
| 1817 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1819 | // Check to see if this is the last token on the #if[n]def line. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1820 | CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef"); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1821 | |
Chris Lattner | 13d283d | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 1822 | IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); |
| 1823 | MacroInfo *MI = getMacroInfo(MII); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1824 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1825 | if (CurPPLexer->getConditionalStackDepth() == 0) { |
Chris Lattner | 13d283d | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 1826 | // If the start of a top-level #ifdef and if the macro is not defined, |
| 1827 | // inform MIOpt that this might be the start of a proper include guard. |
| 1828 | // Otherwise it is some other form of unknown conditional which we can't |
| 1829 | // handle. |
| 1830 | if (!ReadAnyTokensBeforeDirective && MI == 0) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1831 | assert(isIfndef && "#ifdef shouldn't reach here"); |
Chris Lattner | 13d283d | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 1832 | CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MII); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1833 | } else |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1834 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1837 | // If there is a macro, process it. |
| 1838 | if (MI) // Mark it used. |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1839 | markMacroAsUsed(MI); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1841 | // Should we include the stuff contained by this directive? |
| 1842 | if (!MI == isIfndef) { |
| 1843 | // Yes, remember that we are inside a conditional, then lex the next token. |
Chris Lattner | 1d9c54d | 2009-12-14 04:54:40 +0000 | [diff] [blame] | 1844 | CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(), |
| 1845 | /*wasskip*/false, /*foundnonskip*/true, |
| 1846 | /*foundelse*/false); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1847 | } else { |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1848 | // No, skip the contents of this block. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1849 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1850 | /*Foundnonskip*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1851 | /*FoundElse*/false); |
| 1852 | } |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1853 | |
| 1854 | if (Callbacks) { |
| 1855 | if (isIfndef) |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 1856 | Callbacks->Ifndef(MacroNameTok); |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1857 | else |
Craig Silverstein | 2aa9267 | 2010-11-19 21:33:15 +0000 | [diff] [blame] | 1858 | Callbacks->Ifdef(MacroNameTok); |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1859 | } |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | /// HandleIfDirective - Implements the #if directive. |
| 1863 | /// |
| 1864 | void Preprocessor::HandleIfDirective(Token &IfToken, |
| 1865 | bool ReadAnyTokensBeforeDirective) { |
| 1866 | ++NumIf; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1867 | |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1868 | // Parse and evaluate the conditional expression. |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1869 | IdentifierInfo *IfNDefMacro = 0; |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1870 | const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation(); |
| 1871 | const bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro); |
| 1872 | const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation(); |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1873 | |
| 1874 | // If this condition is equivalent to #ifndef X, and if this is the first |
| 1875 | // directive seen, handle it for the multiple-include optimization. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1876 | if (CurPPLexer->getConditionalStackDepth() == 0) { |
Chris Lattner | 13d283d | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 1877 | if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue) |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1878 | CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro); |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1879 | else |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1880 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Nuno Lopes | 0049db6 | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1883 | // Should we include the stuff contained by this directive? |
| 1884 | if (ConditionalTrue) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1885 | // Yes, remember that we are inside a conditional, then lex the next token. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1886 | CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1887 | /*foundnonskip*/true, /*foundelse*/false); |
| 1888 | } else { |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1889 | // No, skip the contents of this block. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1890 | SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false, |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1891 | /*FoundElse*/false); |
| 1892 | } |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1893 | |
| 1894 | if (Callbacks) |
| 1895 | Callbacks->If(SourceRange(ConditionalBegin, ConditionalEnd)); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | /// HandleEndifDirective - Implements the #endif directive. |
| 1899 | /// |
| 1900 | void Preprocessor::HandleEndifDirective(Token &EndifToken) { |
| 1901 | ++NumEndif; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1902 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1903 | // Check that this is the whole directive. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1904 | CheckEndOfDirective("endif"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1906 | PPConditionalInfo CondInfo; |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1907 | if (CurPPLexer->popConditionalLevel(CondInfo)) { |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1908 | // No conditionals on the stack: this is an #endif without an #if. |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1909 | Diag(EndifToken, diag::err_pp_endif_without_if); |
| 1910 | return; |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1911 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1912 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1913 | // If this the end of a top-level #endif, inform MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1914 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 1915 | CurPPLexer->MIOpt.ExitTopLevelConditional(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1916 | |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1917 | assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode && |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1918 | "This code should only be reachable in the non-skipping case!"); |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1919 | |
| 1920 | if (Callbacks) |
| 1921 | Callbacks->Endif(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1924 | /// HandleElseDirective - Implements the #else directive. |
| 1925 | /// |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1926 | void Preprocessor::HandleElseDirective(Token &Result) { |
| 1927 | ++NumElse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1928 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1929 | // #else directive in a non-skipping conditional... start skipping. |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1930 | CheckEndOfDirective("else"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1931 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1932 | PPConditionalInfo CI; |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1933 | if (CurPPLexer->popConditionalLevel(CI)) { |
| 1934 | Diag(Result, diag::pp_err_else_without_if); |
| 1935 | return; |
| 1936 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1937 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1938 | // If this is a top-level #else, inform the MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1939 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 1940 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1941 | |
| 1942 | // If this is a #else with a #else before it, report the error. |
| 1943 | if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1944 | |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1945 | // Finally, skip the rest of the contents of this block. |
| 1946 | SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
Argyrios Kyrtzidis | 6b4ff04 | 2011-09-27 17:32:05 +0000 | [diff] [blame] | 1947 | /*FoundElse*/true, Result.getLocation()); |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1948 | |
| 1949 | if (Callbacks) |
| 1950 | Callbacks->Else(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1953 | /// HandleElifDirective - Implements the #elif directive. |
| 1954 | /// |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1955 | void Preprocessor::HandleElifDirective(Token &ElifToken) { |
| 1956 | ++NumElse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1957 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1958 | // #elif directive in a non-skipping conditional... start skipping. |
| 1959 | // We don't care what the condition is, because we will always skip it (since |
| 1960 | // the block immediately before it was included). |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1961 | const SourceLocation ConditionalBegin = CurPPLexer->getSourceLocation(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1962 | DiscardUntilEndOfDirective(); |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1963 | const SourceLocation ConditionalEnd = CurPPLexer->getSourceLocation(); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1964 | |
| 1965 | PPConditionalInfo CI; |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1966 | if (CurPPLexer->popConditionalLevel(CI)) { |
| 1967 | Diag(ElifToken, diag::pp_err_elif_without_if); |
| 1968 | return; |
| 1969 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1970 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1971 | // If this is a top-level #elif, inform the MIOpt. |
Ted Kremenek | 60e45d4 | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1972 | if (CurPPLexer->getConditionalStackDepth() == 0) |
| 1973 | CurPPLexer->MIOpt.EnterTopLevelConditional(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1974 | |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1975 | // If this is a #elif with a #else before it, report the error. |
| 1976 | if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else); |
| 1977 | |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1978 | // Finally, skip the rest of the contents of this block. |
| 1979 | SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, |
Argyrios Kyrtzidis | 6b4ff04 | 2011-09-27 17:32:05 +0000 | [diff] [blame] | 1980 | /*FoundElse*/CI.FoundElse, |
| 1981 | ElifToken.getLocation()); |
Craig Silverstein | 08985b9 | 2010-11-06 01:19:03 +0000 | [diff] [blame] | 1982 | |
| 1983 | if (Callbacks) |
| 1984 | Callbacks->Elif(SourceRange(ConditionalBegin, ConditionalEnd)); |
Chris Lattner | 141e71f | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1985 | } |