| Chris Lattner | 8962015 | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 1 | //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===// | 
| Chris Lattner | f64b352 | 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 | 100c65e | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 15 | #include "clang/Lex/LiteralSupport.h" | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 16 | #include "clang/Lex/HeaderSearch.h" | 
|  | 17 | #include "clang/Lex/MacroInfo.h" | 
| Chris Lattner | 60f3622 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 18 | #include "clang/Lex/LexDiagnostic.h" | 
| Chris Lattner | 710bb87 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 19 | #include "clang/Basic/FileManager.h" | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" | 
| Chris Lattner | 100c65e | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/APInt.h" | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 22 | using namespace clang; | 
|  | 23 |  | 
|  | 24 | //===----------------------------------------------------------------------===// | 
|  | 25 | // Utility Methods for Preprocessor Directive Handling. | 
|  | 26 | //===----------------------------------------------------------------------===// | 
|  | 27 |  | 
| Chris Lattner | 666f7a4 | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 28 | MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) { | 
| Ted Kremenek | 6c7ea11 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 29 | MacroInfo *MI; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 |  | 
| Ted Kremenek | 6c7ea11 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 31 | if (!MICache.empty()) { | 
|  | 32 | MI = MICache.back(); | 
|  | 33 | MICache.pop_back(); | 
| Chris Lattner | 666f7a4 | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 34 | } else | 
|  | 35 | MI = (MacroInfo*) BP.Allocate<MacroInfo>(); | 
| Ted Kremenek | 6c7ea11 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 36 | new (MI) MacroInfo(L); | 
|  | 37 | return MI; | 
|  | 38 | } | 
|  | 39 |  | 
| Chris Lattner | 666f7a4 | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 40 | /// ReleaseMacroInfo - Release the specified MacroInfo.  This memory will | 
|  | 41 | ///  be reused for allocating new MacroInfo objects. | 
|  | 42 | void Preprocessor::ReleaseMacroInfo(MacroInfo* MI) { | 
|  | 43 | MICache.push_back(MI); | 
| Chris Lattner | 70946da | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 44 | MI->FreeArgumentList(BP); | 
| Chris Lattner | 666f7a4 | 2009-02-20 22:19:20 +0000 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
|  | 47 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 48 | /// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the | 
|  | 49 | /// current line until the tok::eom token is found. | 
|  | 50 | void Preprocessor::DiscardUntilEndOfDirective() { | 
|  | 51 | Token Tmp; | 
|  | 52 | do { | 
|  | 53 | LexUnexpandedToken(Tmp); | 
|  | 54 | } while (Tmp.isNot(tok::eom)); | 
|  | 55 | } | 
|  | 56 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 57 | /// ReadMacroName - Lex and validate a macro name, which occurs after a | 
|  | 58 | /// #define or #undef.  This sets the token kind to eom and discards the rest | 
|  | 59 | /// of the macro line if the macro name is invalid.  isDefineUndef is 1 if | 
|  | 60 | /// this is due to a a #define, 2 if #undef directive, 0 if it is something | 
|  | 61 | /// else (e.g. #ifdef). | 
|  | 62 | void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) { | 
|  | 63 | // Read the token, don't allow macro expansion on it. | 
|  | 64 | LexUnexpandedToken(MacroNameTok); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 66 | // Missing macro name? | 
| Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 67 | if (MacroNameTok.is(tok::eom)) { | 
|  | 68 | Diag(MacroNameTok, diag::err_pp_missing_macro_name); | 
|  | 69 | return; | 
|  | 70 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 72 | IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); | 
|  | 73 | if (II == 0) { | 
| Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 74 | bool Invalid = false; | 
|  | 75 | std::string Spelling = getSpelling(MacroNameTok, &Invalid); | 
|  | 76 | if (Invalid) | 
|  | 77 | return; | 
|  | 78 |  | 
| Chris Lattner | 77c76ae | 2008-12-13 20:12:40 +0000 | [diff] [blame] | 79 | const IdentifierInfo &Info = Identifiers.get(Spelling); | 
|  | 80 | if (Info.isCPlusPlusOperatorKeyword()) | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 81 | // C++ 2.5p2: Alternative tokens behave the same as its primary token | 
|  | 82 | // except for their spellings. | 
| Chris Lattner | 97b8e84 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 83 | Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name) << Spelling; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 84 | else | 
|  | 85 | Diag(MacroNameTok, diag::err_pp_macro_not_identifier); | 
|  | 86 | // Fall through on error. | 
|  | 87 | } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) { | 
|  | 88 | // Error if defining "defined": C99 6.10.8.4. | 
|  | 89 | Diag(MacroNameTok, diag::err_defined_macro_name); | 
|  | 90 | } else if (isDefineUndef && II->hasMacroDefinition() && | 
|  | 91 | getMacroInfo(II)->isBuiltinMacro()) { | 
|  | 92 | // Error if defining "__LINE__" and other builtins: C99 6.10.8.4. | 
|  | 93 | if (isDefineUndef == 1) | 
|  | 94 | Diag(MacroNameTok, diag::pp_redef_builtin_macro); | 
|  | 95 | else | 
|  | 96 | Diag(MacroNameTok, diag::pp_undef_builtin_macro); | 
|  | 97 | } else { | 
|  | 98 | // Okay, we got a good identifier node.  Return it. | 
|  | 99 | return; | 
|  | 100 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 102 | // Invalid macro name, read and discard the rest of the line.  Then set the | 
|  | 103 | // token kind to tok::eom. | 
|  | 104 | MacroNameTok.setKind(tok::eom); | 
|  | 105 | return DiscardUntilEndOfDirective(); | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | /// CheckEndOfDirective - Ensure that the next token is a tok::eom token.  If | 
| Chris Lattner | 0003c27 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 109 | /// not, emit a diagnostic and consume up until the eom.  If EnableMacros is | 
|  | 110 | /// true, then we consider macros that expand to zero tokens as being ok. | 
|  | 111 | void Preprocessor::CheckEndOfDirective(const char *DirType, bool EnableMacros) { | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 112 | Token Tmp; | 
| Chris Lattner | 0003c27 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 113 | // Lex unexpanded tokens for most directives: macros might expand to zero | 
|  | 114 | // tokens, causing us to miss diagnosing invalid lines.  Some directives (like | 
|  | 115 | // #line) allow empty macros. | 
|  | 116 | if (EnableMacros) | 
|  | 117 | Lex(Tmp); | 
|  | 118 | else | 
|  | 119 | LexUnexpandedToken(Tmp); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 121 | // There should be no tokens after the directive, but we allow them as an | 
|  | 122 | // extension. | 
|  | 123 | while (Tmp.is(tok::comment))  // Skip comments in -C mode. | 
|  | 124 | LexUnexpandedToken(Tmp); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 126 | if (Tmp.isNot(tok::eom)) { | 
| Chris Lattner | 825676a | 2009-04-14 05:15:20 +0000 | [diff] [blame] | 127 | // Add a fixit in GNU/C99/C++ mode.  Don't offer a fixit for strict-C89, | 
|  | 128 | // because it is more trouble than it is worth to insert /**/ and check that | 
|  | 129 | // there is no /**/ in the range also. | 
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 130 | FixItHint Hint; | 
| Chris Lattner | 825676a | 2009-04-14 05:15:20 +0000 | [diff] [blame] | 131 | if (Features.GNUMode || Features.C99 || Features.CPlusPlus) | 
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 132 | Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//"); | 
|  | 133 | Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 134 | DiscardUntilEndOfDirective(); | 
|  | 135 | } | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 |  | 
|  | 139 |  | 
|  | 140 | /// SkipExcludedConditionalBlock - We just read a #if or related directive and | 
|  | 141 | /// decided that the subsequent tokens are in the #if'd out portion of the | 
|  | 142 | /// file.  Lex the rest of the file, until we see an #endif.  If | 
|  | 143 | /// FoundNonSkipPortion is true, then we have already emitted code for part of | 
|  | 144 | /// this #if directive, so #else/#elif blocks should never be entered. If ElseOk | 
|  | 145 | /// is true, then #else directives are ok, if not, then we have already seen one | 
|  | 146 | /// so a #else directive is a duplicate.  When this returns, the caller can lex | 
|  | 147 | /// the first valid token. | 
|  | 148 | void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc, | 
|  | 149 | bool FoundNonSkipPortion, | 
|  | 150 | bool FoundElse) { | 
|  | 151 | ++NumSkipped; | 
| Ted Kremenek | 6b73291 | 2008-11-18 01:04:47 +0000 | [diff] [blame] | 152 | assert(CurTokenLexer == 0 && CurPPLexer && "Lexing a macro, not a file?"); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 153 |  | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 154 | CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false, | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 155 | FoundNonSkipPortion, FoundElse); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 |  | 
| Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 157 | if (CurPTHLexer) { | 
|  | 158 | PTHSkipExcludedConditionalBlock(); | 
|  | 159 | return; | 
|  | 160 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 162 | // Enter raw mode to disable identifier lookup (and thus macro expansion), | 
|  | 163 | // disabling warnings, etc. | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 164 | CurPPLexer->LexingRawMode = true; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 165 | Token Tok; | 
|  | 166 | while (1) { | 
| Chris Lattner | f406b24 | 2010-01-18 22:33:01 +0000 | [diff] [blame] | 167 | CurLexer->Lex(Tok); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 169 | // If this is the end of the buffer, we have an error. | 
|  | 170 | if (Tok.is(tok::eof)) { | 
|  | 171 | // Emit errors for each unterminated conditional on the stack, including | 
|  | 172 | // the current one. | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 173 | while (!CurPPLexer->ConditionalStack.empty()) { | 
| Douglas Gregor | 02690ba | 2010-08-12 17:04:55 +0000 | [diff] [blame] | 174 | if (!isCodeCompletionFile(Tok.getLocation())) | 
|  | 175 | Diag(CurPPLexer->ConditionalStack.back().IfLoc, | 
|  | 176 | diag::err_pp_unterminated_conditional); | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 177 | CurPPLexer->ConditionalStack.pop_back(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | } | 
|  | 179 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 180 | // Just return and let the caller lex after this #include. | 
|  | 181 | break; | 
|  | 182 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 184 | // If this token is not a preprocessor directive, just skip it. | 
|  | 185 | if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) | 
|  | 186 | continue; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 188 | // We just parsed a # character at the start of a line, so we're in | 
|  | 189 | // directive mode.  Tell the lexer this so any newlines we see will be | 
|  | 190 | // converted into an EOM token (this terminates the macro). | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 191 | CurPPLexer->ParsingPreprocessorDirective = true; | 
| Ted Kremenek | 59e003e | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 192 | if (CurLexer) CurLexer->SetCommentRetentionState(false); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 193 |  | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 195 | // Read the next token, the directive flavor. | 
|  | 196 | LexUnexpandedToken(Tok); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 198 | // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or | 
|  | 199 | // something bogus), skip it. | 
|  | 200 | if (Tok.isNot(tok::identifier)) { | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 201 | CurPPLexer->ParsingPreprocessorDirective = false; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 202 | // Restore comment saving mode. | 
| Ted Kremenek | 59e003e | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 203 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 204 | continue; | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | // If the first letter isn't i or e, it isn't intesting to us.  We know that | 
|  | 208 | // this is safe in the face of spelling differences, because there is no way | 
|  | 209 | // to spell an i/e in a strange way that is another letter.  Skipping this | 
|  | 210 | // allows us to avoid looking up the identifier info for #define/#undef and | 
|  | 211 | // other common directives. | 
| Douglas Gregor | 42fe858 | 2010-03-16 20:46:42 +0000 | [diff] [blame] | 212 | bool Invalid = false; | 
|  | 213 | const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation(), | 
|  | 214 | &Invalid); | 
|  | 215 | if (Invalid) | 
|  | 216 | return; | 
|  | 217 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 218 | char FirstChar = RawCharData[0]; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | if (FirstChar >= 'a' && FirstChar <= 'z' && | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 220 | FirstChar != 'i' && FirstChar != 'e') { | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 221 | CurPPLexer->ParsingPreprocessorDirective = false; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 222 | // Restore comment saving mode. | 
| Ted Kremenek | 59e003e | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 223 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 224 | continue; | 
|  | 225 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 227 | // Get the identifier name without trigraphs or embedded newlines.  Note | 
|  | 228 | // that we can't use Tok.getIdentifierInfo() because its lookup is disabled | 
|  | 229 | // when skipping. | 
| Benjamin Kramer | 14488464 | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 230 | char DirectiveBuf[20]; | 
|  | 231 | llvm::StringRef Directive; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 232 | if (!Tok.needsCleaning() && Tok.getLength() < 20) { | 
| Benjamin Kramer | 14488464 | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 233 | Directive = llvm::StringRef(RawCharData, Tok.getLength()); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 234 | } else { | 
|  | 235 | std::string DirectiveStr = getSpelling(Tok); | 
| Benjamin Kramer | 14488464 | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 236 | unsigned IdLen = DirectiveStr.size(); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 237 | if (IdLen >= 20) { | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 238 | CurPPLexer->ParsingPreprocessorDirective = false; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 239 | // Restore comment saving mode. | 
| Ted Kremenek | 59e003e | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 240 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 241 | continue; | 
|  | 242 | } | 
| Benjamin Kramer | 14488464 | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 243 | memcpy(DirectiveBuf, &DirectiveStr[0], IdLen); | 
|  | 244 | Directive = llvm::StringRef(DirectiveBuf, IdLen); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 245 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 |  | 
| Benjamin Kramer | 14488464 | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 247 | if (Directive.startswith("if")) { | 
|  | 248 | llvm::StringRef Sub = Directive.substr(2); | 
|  | 249 | if (Sub.empty() ||   // "if" | 
|  | 250 | Sub == "def" ||   // "ifdef" | 
|  | 251 | Sub == "ndef") {  // "ifndef" | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 252 | // We know the entire #if/#ifdef/#ifndef block will be skipped, don't | 
|  | 253 | // bother parsing the condition. | 
|  | 254 | DiscardUntilEndOfDirective(); | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 255 | CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true, | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 256 | /*foundnonskip*/false, | 
|  | 257 | /*fnddelse*/false); | 
|  | 258 | } | 
| Benjamin Kramer | 14488464 | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 259 | } else if (Directive[0] == 'e') { | 
|  | 260 | llvm::StringRef Sub = Directive.substr(1); | 
|  | 261 | if (Sub == "ndif") {  // "endif" | 
| Chris Lattner | ce2ab6f | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 262 | CheckEndOfDirective("endif"); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 263 | PPConditionalInfo CondInfo; | 
|  | 264 | CondInfo.WasSkipping = true; // Silence bogus warning. | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 265 | bool InCond = CurPPLexer->popConditionalLevel(CondInfo); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 266 | InCond = InCond;  // Silence warning in no-asserts mode. | 
|  | 267 | assert(!InCond && "Can't be skipping if not in a conditional!"); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 269 | // If we popped the outermost skipping block, we're done skipping! | 
|  | 270 | if (!CondInfo.WasSkipping) | 
|  | 271 | break; | 
| Benjamin Kramer | 14488464 | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 272 | } else if (Sub == "lse") { // "else". | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 273 | // #else directive in a skipping conditional.  If not in some other | 
|  | 274 | // skipping conditional, and if #else hasn't already been seen, enter it | 
|  | 275 | // as a non-skipping conditional. | 
| Chris Lattner | bc63de1 | 2009-04-18 01:34:22 +0000 | [diff] [blame] | 276 | DiscardUntilEndOfDirective();  // C99 6.10p4. | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 277 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 279 | // If this is a #else with a #else before it, report the error. | 
|  | 280 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 282 | // Note that we've seen a #else in this conditional. | 
|  | 283 | CondInfo.FoundElse = true; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 285 | // If the conditional is at the top level, and the #if block wasn't | 
|  | 286 | // entered, enter the #else block now. | 
|  | 287 | if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) { | 
|  | 288 | CondInfo.FoundNonSkip = true; | 
|  | 289 | break; | 
|  | 290 | } | 
| Benjamin Kramer | 14488464 | 2009-12-31 13:32:38 +0000 | [diff] [blame] | 291 | } else if (Sub == "lif") {  // "elif". | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 292 | PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel(); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 293 |  | 
|  | 294 | bool ShouldEnter; | 
|  | 295 | // If this is in a skipping block or if we're already handled this #if | 
|  | 296 | // block, don't bother parsing the condition. | 
|  | 297 | if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) { | 
|  | 298 | DiscardUntilEndOfDirective(); | 
|  | 299 | ShouldEnter = false; | 
|  | 300 | } else { | 
|  | 301 | // Restore the value of LexingRawMode so that identifiers are | 
|  | 302 | // looked up, etc, inside the #elif expression. | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 303 | assert(CurPPLexer->LexingRawMode && "We have to be skipping here!"); | 
|  | 304 | CurPPLexer->LexingRawMode = false; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 305 | IdentifierInfo *IfNDefMacro = 0; | 
|  | 306 | ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 307 | CurPPLexer->LexingRawMode = true; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 308 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 310 | // If this is a #elif with a #else before it, report the error. | 
|  | 311 | if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 313 | // If this condition is true, enter it! | 
|  | 314 | if (ShouldEnter) { | 
|  | 315 | CondInfo.FoundNonSkip = true; | 
|  | 316 | break; | 
|  | 317 | } | 
|  | 318 | } | 
|  | 319 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 |  | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 321 | CurPPLexer->ParsingPreprocessorDirective = false; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 322 | // Restore comment saving mode. | 
| Ted Kremenek | 59e003e | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 323 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepComments); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 324 | } | 
|  | 325 |  | 
|  | 326 | // Finally, if we are out of the conditional (saw an #endif or ran off the end | 
|  | 327 | // of the file, just stop skipping and return to lexing whatever came after | 
|  | 328 | // the #if block. | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 329 | CurPPLexer->LexingRawMode = false; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 330 | } | 
|  | 331 |  | 
| Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 332 | void Preprocessor::PTHSkipExcludedConditionalBlock() { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 |  | 
|  | 334 | while (1) { | 
| Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 335 | assert(CurPTHLexer); | 
|  | 336 | assert(CurPTHLexer->LexingRawMode == false); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 |  | 
| Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 338 | // Skip to the next '#else', '#elif', or #endif. | 
|  | 339 | if (CurPTHLexer->SkipBlock()) { | 
|  | 340 | // We have reached an #endif.  Both the '#' and 'endif' tokens | 
|  | 341 | // have been consumed by the PTHLexer.  Just pop off the condition level. | 
|  | 342 | PPConditionalInfo CondInfo; | 
|  | 343 | bool InCond = CurPTHLexer->popConditionalLevel(CondInfo); | 
|  | 344 | InCond = InCond;  // Silence warning in no-asserts mode. | 
|  | 345 | assert(!InCond && "Can't be skipping if not in a conditional!"); | 
|  | 346 | break; | 
|  | 347 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 |  | 
| Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 349 | // We have reached a '#else' or '#elif'.  Lex the next token to get | 
|  | 350 | // the directive flavor. | 
|  | 351 | Token Tok; | 
|  | 352 | LexUnexpandedToken(Tok); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 |  | 
| Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 354 | // We can actually look up the IdentifierInfo here since we aren't in | 
|  | 355 | // raw mode. | 
|  | 356 | tok::PPKeywordKind K = Tok.getIdentifierInfo()->getPPKeywordID(); | 
|  | 357 |  | 
|  | 358 | if (K == tok::pp_else) { | 
|  | 359 | // #else: Enter the else condition.  We aren't in a nested condition | 
|  | 360 | //  since we skip those. We're always in the one matching the last | 
|  | 361 | //  blocked we skipped. | 
|  | 362 | PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); | 
|  | 363 | // Note that we've seen a #else in this conditional. | 
|  | 364 | CondInfo.FoundElse = true; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 365 |  | 
| Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 366 | // If the #if block wasn't entered then enter the #else block now. | 
|  | 367 | if (!CondInfo.FoundNonSkip) { | 
|  | 368 | CondInfo.FoundNonSkip = true; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 |  | 
| Daniel Dunbar | 2cba6be | 2009-04-13 17:57:49 +0000 | [diff] [blame] | 370 | // Scan until the eom token. | 
| Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 371 | CurPTHLexer->ParsingPreprocessorDirective = true; | 
| Daniel Dunbar | 2cba6be | 2009-04-13 17:57:49 +0000 | [diff] [blame] | 372 | DiscardUntilEndOfDirective(); | 
| Ted Kremenek | 1b18ad2 | 2008-12-23 01:30:52 +0000 | [diff] [blame] | 373 | CurPTHLexer->ParsingPreprocessorDirective = false; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 |  | 
| Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 375 | break; | 
|  | 376 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 |  | 
| Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 378 | // Otherwise skip this block. | 
|  | 379 | continue; | 
|  | 380 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 |  | 
| Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 382 | assert(K == tok::pp_elif); | 
|  | 383 | PPConditionalInfo &CondInfo = CurPTHLexer->peekConditionalLevel(); | 
|  | 384 |  | 
|  | 385 | // If this is a #elif with a #else before it, report the error. | 
|  | 386 | if (CondInfo.FoundElse) | 
|  | 387 | Diag(Tok, diag::pp_err_elif_after_else); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 |  | 
| Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 389 | // If this is in a skipping block or if we're already handled this #if | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | // block, don't bother parsing the condition.  We just skip this block. | 
| Ted Kremenek | 56572ab | 2008-12-12 18:34:08 +0000 | [diff] [blame] | 391 | if (CondInfo.FoundNonSkip) | 
|  | 392 | continue; | 
|  | 393 |  | 
|  | 394 | // Evaluate the condition of the #elif. | 
|  | 395 | IdentifierInfo *IfNDefMacro = 0; | 
|  | 396 | CurPTHLexer->ParsingPreprocessorDirective = true; | 
|  | 397 | bool ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro); | 
|  | 398 | CurPTHLexer->ParsingPreprocessorDirective = false; | 
|  | 399 |  | 
|  | 400 | // If this condition is true, enter it! | 
|  | 401 | if (ShouldEnter) { | 
|  | 402 | CondInfo.FoundNonSkip = true; | 
|  | 403 | break; | 
|  | 404 | } | 
|  | 405 |  | 
|  | 406 | // Otherwise, skip this block and go to the next one. | 
|  | 407 | continue; | 
|  | 408 | } | 
|  | 409 | } | 
|  | 410 |  | 
| Chris Lattner | f7ad82d | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 411 | /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, | 
|  | 412 | /// return null on failure.  isAngled indicates whether the file reference is | 
|  | 413 | /// for system #include's or not (i.e. using <> instead of ""). | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 414 | const FileEntry *Preprocessor::LookupFile(llvm::StringRef Filename, | 
| Chris Lattner | f7ad82d | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 415 | bool isAngled, | 
|  | 416 | const DirectoryLookup *FromDir, | 
|  | 417 | const DirectoryLookup *&CurDir) { | 
|  | 418 | // If the header lookup mechanism may be relative to the current file, pass in | 
|  | 419 | // info about where the current file is. | 
| Douglas Gregor | 618e64a | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 420 | const FileEntry *CurFileEnt = 0; | 
| Chris Lattner | f7ad82d | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 421 | if (!FromDir) { | 
| Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 422 | FileID FID = getCurrentFileLexer()->getFileID(); | 
| Douglas Gregor | 618e64a | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 423 | CurFileEnt = SourceMgr.getFileEntryForID(FID); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 |  | 
| Chris Lattner | 022923a | 2009-02-04 19:45:07 +0000 | [diff] [blame] | 425 | // If there is no file entry associated with this file, it must be the | 
|  | 426 | // predefines buffer.  Any other file is not lexed with a normal lexer, so | 
| Douglas Gregor | 618e64a | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 427 | // it won't be scanned for preprocessor directives.   If we have the | 
|  | 428 | // predefines buffer, resolve #include references (which come from the | 
|  | 429 | // -include command line argument) as if they came from the main file, this | 
|  | 430 | // affects file lookup etc. | 
|  | 431 | if (CurFileEnt == 0) { | 
| Chris Lattner | 022923a | 2009-02-04 19:45:07 +0000 | [diff] [blame] | 432 | FID = SourceMgr.getMainFileID(); | 
|  | 433 | CurFileEnt = SourceMgr.getFileEntryForID(FID); | 
|  | 434 | } | 
| Chris Lattner | f7ad82d | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 435 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 |  | 
| Chris Lattner | f7ad82d | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 437 | // Do a standard file entry lookup. | 
|  | 438 | CurDir = CurDirLookup; | 
|  | 439 | const FileEntry *FE = | 
| Douglas Gregor | 618e64a | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 440 | HeaderInfo.LookupFile(Filename, isAngled, FromDir, CurDir, CurFileEnt); | 
| Chris Lattner | fde8535 | 2010-01-22 00:14:44 +0000 | [diff] [blame] | 441 | if (FE) return FE; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 442 |  | 
| Chris Lattner | f7ad82d | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 443 | // Otherwise, see if this is a subframework header.  If so, this is relative | 
|  | 444 | // to one of the headers on the #include stack.  Walk the list of the current | 
|  | 445 | // headers on the #include stack and pass them to HeaderInfo. | 
| Ted Kremenek | 6bc5f3e | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 446 | if (IsFileLexer()) { | 
| Ted Kremenek | 4524521 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 447 | if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 448 | if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt))) | 
| Chris Lattner | f7ad82d | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 449 | return FE; | 
|  | 450 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 |  | 
| Chris Lattner | f7ad82d | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 452 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { | 
|  | 453 | IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1]; | 
| Ted Kremenek | 6bc5f3e | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 454 | if (IsFileLexer(ISEntry)) { | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | if ((CurFileEnt = | 
| Ted Kremenek | 4524521 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 456 | SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID()))) | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 457 | if ((FE = HeaderInfo.LookupSubframeworkHeader(Filename, CurFileEnt))) | 
| Chris Lattner | f7ad82d | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 458 | return FE; | 
|  | 459 | } | 
|  | 460 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 |  | 
| Chris Lattner | f7ad82d | 2008-03-09 04:17:44 +0000 | [diff] [blame] | 462 | // Otherwise, we really couldn't find the file. | 
|  | 463 | return 0; | 
|  | 464 | } | 
|  | 465 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 466 |  | 
|  | 467 | //===----------------------------------------------------------------------===// | 
|  | 468 | // Preprocessor Directive Handling. | 
|  | 469 | //===----------------------------------------------------------------------===// | 
|  | 470 |  | 
|  | 471 | /// HandleDirective - This callback is invoked when the lexer sees a # token | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | /// at the start of a line.  This consumes the directive, modifies the | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 473 | /// lexer/preprocessor state, and advances the lexer(s) so that the next token | 
|  | 474 | /// read is the correct one. | 
|  | 475 | void Preprocessor::HandleDirective(Token &Result) { | 
|  | 476 | // FIXME: Traditional: # with whitespace before it not recognized by K&R? | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 478 | // We just parsed a # character at the start of a line, so we're in directive | 
|  | 479 | // mode.  Tell the lexer this so any newlines we see will be converted into an | 
|  | 480 | // EOM token (which terminates the directive). | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 481 | CurPPLexer->ParsingPreprocessorDirective = true; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 483 | ++NumDirectives; | 
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 484 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 485 | // We are about to read a token.  For the multiple-include optimization FA to | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | // work, we have to remember if we had read any tokens *before* this | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 487 | // pp-directive. | 
| Chris Lattner | 8cf1f93 | 2009-12-14 04:54:40 +0000 | [diff] [blame] | 488 | bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 |  | 
| Chris Lattner | 2d17ab7 | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 490 | // Save the '#' token in case we need to return it later. | 
|  | 491 | Token SavedHash = Result; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 493 | // Read the next token, the directive flavor.  This isn't expanded due to | 
|  | 494 | // C99 6.10.3p8. | 
|  | 495 | LexUnexpandedToken(Result); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 497 | // C99 6.10.3p11: Is this preprocessor directive in macro invocation?  e.g.: | 
|  | 498 | //   #define A(x) #x | 
|  | 499 | //   A(abc | 
|  | 500 | //     #warning blah | 
|  | 501 | //   def) | 
|  | 502 | // If so, the user is relying on non-portable behavior, emit a diagnostic. | 
|  | 503 | if (InMacroArgs) | 
|  | 504 | Diag(Result, diag::ext_embedded_directive); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 506 | TryAgain: | 
|  | 507 | switch (Result.getKind()) { | 
|  | 508 | case tok::eom: | 
|  | 509 | return;   // null directive. | 
|  | 510 | case tok::comment: | 
|  | 511 | // Handle stuff like "# /*foo*/ define X" in -E -C mode. | 
|  | 512 | LexUnexpandedToken(Result); | 
|  | 513 | goto TryAgain; | 
|  | 514 |  | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 515 | case tok::numeric_constant:  // # 7  GNU line marker directive. | 
| Chris Lattner | 5eb8ae2 | 2009-03-18 20:41:10 +0000 | [diff] [blame] | 516 | if (getLangOptions().AsmPreprocessor) | 
|  | 517 | break;  // # 4 is not a preprocessor directive in .S files. | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 518 | return HandleDigitDirective(Result); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 519 | default: | 
|  | 520 | IdentifierInfo *II = Result.getIdentifierInfo(); | 
|  | 521 | if (II == 0) break;  // Not an identifier. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 523 | // Ask what the preprocessor keyword ID is. | 
|  | 524 | switch (II->getPPKeywordID()) { | 
|  | 525 | default: break; | 
|  | 526 | // C99 6.10.1 - Conditional Inclusion. | 
|  | 527 | case tok::pp_if: | 
|  | 528 | return HandleIfDirective(Result, ReadAnyTokensBeforeDirective); | 
|  | 529 | case tok::pp_ifdef: | 
|  | 530 | return HandleIfdefDirective(Result, false, true/*not valid for miopt*/); | 
|  | 531 | case tok::pp_ifndef: | 
|  | 532 | return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective); | 
|  | 533 | case tok::pp_elif: | 
|  | 534 | return HandleElifDirective(Result); | 
|  | 535 | case tok::pp_else: | 
|  | 536 | return HandleElseDirective(Result); | 
|  | 537 | case tok::pp_endif: | 
|  | 538 | return HandleEndifDirective(Result); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 540 | // C99 6.10.2 - Source File Inclusion. | 
|  | 541 | case tok::pp_include: | 
| Chris Lattner | 14a7f39 | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 542 | return HandleIncludeDirective(Result);       // Handle #include. | 
|  | 543 | case tok::pp___include_macros: | 
| Chris Lattner | 58a1eb0 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 544 | return HandleIncludeMacrosDirective(Result); // Handle -imacros. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 546 | // C99 6.10.3 - Macro Replacement. | 
|  | 547 | case tok::pp_define: | 
|  | 548 | return HandleDefineDirective(Result); | 
|  | 549 | case tok::pp_undef: | 
|  | 550 | return HandleUndefDirective(Result); | 
|  | 551 |  | 
|  | 552 | // C99 6.10.4 - Line Control. | 
|  | 553 | case tok::pp_line: | 
| Chris Lattner | 100c65e | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 554 | return HandleLineDirective(Result); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 555 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 556 | // C99 6.10.5 - Error Directive. | 
|  | 557 | case tok::pp_error: | 
|  | 558 | return HandleUserDiagnosticDirective(Result, false); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 559 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 560 | // C99 6.10.6 - Pragma Directive. | 
|  | 561 | case tok::pp_pragma: | 
|  | 562 | return HandlePragmaDirective(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 564 | // GNU Extensions. | 
|  | 565 | case tok::pp_import: | 
|  | 566 | return HandleImportDirective(Result); | 
|  | 567 | case tok::pp_include_next: | 
|  | 568 | return HandleIncludeNextDirective(Result); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 570 | case tok::pp_warning: | 
|  | 571 | Diag(Result, diag::ext_pp_warning_directive); | 
|  | 572 | return HandleUserDiagnosticDirective(Result, true); | 
|  | 573 | case tok::pp_ident: | 
|  | 574 | return HandleIdentSCCSDirective(Result); | 
|  | 575 | case tok::pp_sccs: | 
|  | 576 | return HandleIdentSCCSDirective(Result); | 
|  | 577 | case tok::pp_assert: | 
|  | 578 | //isExtension = true;  // FIXME: implement #assert | 
|  | 579 | break; | 
|  | 580 | case tok::pp_unassert: | 
|  | 581 | //isExtension = true;  // FIXME: implement #unassert | 
|  | 582 | break; | 
|  | 583 | } | 
|  | 584 | break; | 
|  | 585 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 |  | 
| Chris Lattner | 2d17ab7 | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 587 | // If this is a .S file, treat unknown # directives as non-preprocessor | 
|  | 588 | // directives.  This is important because # may be a comment or introduce | 
|  | 589 | // various pseudo-ops.  Just return the # token and push back the following | 
|  | 590 | // token to be lexed next time. | 
|  | 591 | if (getLangOptions().AsmPreprocessor) { | 
| Daniel Dunbar | 48b4d1e | 2009-07-13 21:48:50 +0000 | [diff] [blame] | 592 | Token *Toks = new Token[2]; | 
| Chris Lattner | 2d17ab7 | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 593 | // Return the # and the token after it. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | Toks[0] = SavedHash; | 
| Chris Lattner | 2d17ab7 | 2009-03-18 21:00:25 +0000 | [diff] [blame] | 595 | Toks[1] = Result; | 
|  | 596 | // Enter this token stream so that we re-lex the tokens.  Make sure to | 
|  | 597 | // enable macro expansion, in case the token after the # is an identifier | 
|  | 598 | // that is expanded. | 
|  | 599 | EnterTokenStream(Toks, 2, false, true); | 
|  | 600 | return; | 
|  | 601 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 602 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 603 | // If we reached here, the preprocessing token is not valid! | 
|  | 604 | Diag(Result, diag::err_pp_invalid_directive); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 605 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 606 | // Read the rest of the PP line. | 
|  | 607 | DiscardUntilEndOfDirective(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 609 | // Okay, we're done parsing the directive. | 
|  | 610 | } | 
|  | 611 |  | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 612 | /// GetLineValue - Convert a numeric token into an unsigned value, emitting | 
|  | 613 | /// Diagnostic DiagID if it is invalid, and returning the value in Val. | 
|  | 614 | static bool GetLineValue(Token &DigitTok, unsigned &Val, | 
|  | 615 | unsigned DiagID, Preprocessor &PP) { | 
|  | 616 | if (DigitTok.isNot(tok::numeric_constant)) { | 
|  | 617 | PP.Diag(DigitTok, DiagID); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 618 |  | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 619 | if (DigitTok.isNot(tok::eom)) | 
|  | 620 | PP.DiscardUntilEndOfDirective(); | 
|  | 621 | return true; | 
|  | 622 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 623 |  | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 624 | llvm::SmallString<64> IntegerBuffer; | 
|  | 625 | IntegerBuffer.resize(DigitTok.getLength()); | 
|  | 626 | const char *DigitTokBegin = &IntegerBuffer[0]; | 
| Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 627 | bool Invalid = false; | 
|  | 628 | unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid); | 
|  | 629 | if (Invalid) | 
|  | 630 | return true; | 
|  | 631 |  | 
| Chris Lattner | d66f172 | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 632 | // Verify that we have a simple digit-sequence, and compute the value.  This | 
|  | 633 | // is always a simple digit string computed in decimal, so we do this manually | 
|  | 634 | // here. | 
|  | 635 | Val = 0; | 
|  | 636 | for (unsigned i = 0; i != ActualLength; ++i) { | 
|  | 637 | if (!isdigit(DigitTokBegin[i])) { | 
|  | 638 | PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i), | 
|  | 639 | diag::err_pp_line_digit_sequence); | 
|  | 640 | PP.DiscardUntilEndOfDirective(); | 
|  | 641 | return true; | 
|  | 642 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 643 |  | 
| Chris Lattner | d66f172 | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 644 | unsigned NextVal = Val*10+(DigitTokBegin[i]-'0'); | 
|  | 645 | if (NextVal < Val) { // overflow. | 
|  | 646 | PP.Diag(DigitTok, DiagID); | 
|  | 647 | PP.DiscardUntilEndOfDirective(); | 
|  | 648 | return true; | 
|  | 649 | } | 
|  | 650 | Val = NextVal; | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 651 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 652 |  | 
|  | 653 | // Reject 0, this is needed both by #line numbers and flags. | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 654 | if (Val == 0) { | 
|  | 655 | PP.Diag(DigitTok, DiagID); | 
|  | 656 | PP.DiscardUntilEndOfDirective(); | 
|  | 657 | return true; | 
|  | 658 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 659 |  | 
| Chris Lattner | d66f172 | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 660 | if (DigitTokBegin[0] == '0') | 
|  | 661 | PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 |  | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 663 | return false; | 
|  | 664 | } | 
|  | 665 |  | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 666 | /// HandleLineDirective - Handle #line directive: C99 6.10.4.  The two | 
| Chris Lattner | 100c65e | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 667 | /// acceptable forms are: | 
|  | 668 | ///   # line digit-sequence | 
|  | 669 | ///   # line digit-sequence "s-char-sequence" | 
|  | 670 | void Preprocessor::HandleLineDirective(Token &Tok) { | 
|  | 671 | // Read the line # and string argument.  Per C99 6.10.4p5, these tokens are | 
|  | 672 | // expanded. | 
|  | 673 | Token DigitTok; | 
|  | 674 | Lex(DigitTok); | 
|  | 675 |  | 
| Chris Lattner | 100c65e | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 676 | // Validate the number and convert it to an unsigned. | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 677 | unsigned LineNo; | 
| Chris Lattner | d66f172 | 2009-04-18 18:35:15 +0000 | [diff] [blame] | 678 | if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this)) | 
| Chris Lattner | 100c65e | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 679 | return; | 
| Chris Lattner | 100c65e | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 680 |  | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 681 | // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a | 
|  | 682 | // number greater than 2147483647".  C90 requires that the line # be <= 32767. | 
| Chris Lattner | 100c65e | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 683 | unsigned LineLimit = Features.C99 ? 2147483648U : 32768U; | 
|  | 684 | if (LineNo >= LineLimit) | 
|  | 685 | Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 686 |  | 
| Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 687 | int FilenameID = -1; | 
| Chris Lattner | 100c65e | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 688 | Token StrTok; | 
|  | 689 | Lex(StrTok); | 
|  | 690 |  | 
|  | 691 | // If the StrTok is "eom", then it wasn't present.  Otherwise, it must be a | 
|  | 692 | // string followed by eom. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 693 | if (StrTok.is(tok::eom)) | 
| Chris Lattner | 100c65e | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 694 | ; // ok | 
|  | 695 | else if (StrTok.isNot(tok::string_literal)) { | 
|  | 696 | Diag(StrTok, diag::err_pp_line_invalid_filename); | 
|  | 697 | DiscardUntilEndOfDirective(); | 
|  | 698 | return; | 
|  | 699 | } else { | 
| Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 700 | // Parse and validate the string, converting it into a unique ID. | 
|  | 701 | StringLiteralParser Literal(&StrTok, 1, *this); | 
|  | 702 | assert(!Literal.AnyWide && "Didn't allow wide strings in"); | 
|  | 703 | if (Literal.hadError) | 
|  | 704 | return DiscardUntilEndOfDirective(); | 
|  | 705 | if (Literal.Pascal) { | 
|  | 706 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); | 
|  | 707 | return DiscardUntilEndOfDirective(); | 
|  | 708 | } | 
|  | 709 | FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString(), | 
|  | 710 | Literal.GetStringLength()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 711 |  | 
| Chris Lattner | 0003c27 | 2009-04-17 23:30:53 +0000 | [diff] [blame] | 712 | // Verify that there is nothing after the string, other than EOM.  Because | 
|  | 713 | // of C99 6.10.4p5, macros that expand to empty tokens are ok. | 
|  | 714 | CheckEndOfDirective("line", true); | 
| Chris Lattner | 100c65e | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 715 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 716 |  | 
| Chris Lattner | 1eaa70a | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 717 | SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 718 |  | 
| Chris Lattner | 839150e | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 719 | if (Callbacks) | 
| Chris Lattner | c745cec | 2010-04-14 04:28:50 +0000 | [diff] [blame] | 720 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), | 
|  | 721 | PPCallbacks::RenameFile, | 
| Chris Lattner | 839150e | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 722 | SrcMgr::C_User); | 
| Chris Lattner | 100c65e | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 723 | } | 
|  | 724 |  | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 725 | /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line | 
|  | 726 | /// marker directive. | 
|  | 727 | static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit, | 
|  | 728 | bool &IsSystemHeader, bool &IsExternCHeader, | 
|  | 729 | Preprocessor &PP) { | 
|  | 730 | unsigned FlagVal; | 
|  | 731 | Token FlagTok; | 
|  | 732 | PP.Lex(FlagTok); | 
|  | 733 | if (FlagTok.is(tok::eom)) return false; | 
|  | 734 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) | 
|  | 735 | return true; | 
|  | 736 |  | 
|  | 737 | if (FlagVal == 1) { | 
|  | 738 | IsFileEntry = true; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 |  | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 740 | PP.Lex(FlagTok); | 
|  | 741 | if (FlagTok.is(tok::eom)) return false; | 
|  | 742 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) | 
|  | 743 | return true; | 
|  | 744 | } else if (FlagVal == 2) { | 
|  | 745 | IsFileExit = true; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 |  | 
| Chris Lattner | 1c96778 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 747 | SourceManager &SM = PP.getSourceManager(); | 
|  | 748 | // If we are leaving the current presumed file, check to make sure the | 
|  | 749 | // presumed include stack isn't empty! | 
|  | 750 | FileID CurFileID = | 
|  | 751 | SM.getDecomposedInstantiationLoc(FlagTok.getLocation()).first; | 
|  | 752 | PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 |  | 
| Chris Lattner | 1c96778 | 2009-02-04 06:25:26 +0000 | [diff] [blame] | 754 | // If there is no include loc (main file) or if the include loc is in a | 
|  | 755 | // different physical file, then we aren't in a "1" line marker flag region. | 
|  | 756 | SourceLocation IncLoc = PLoc.getIncludeLoc(); | 
|  | 757 | if (IncLoc.isInvalid() || | 
|  | 758 | SM.getDecomposedInstantiationLoc(IncLoc).first != CurFileID) { | 
|  | 759 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop); | 
|  | 760 | PP.DiscardUntilEndOfDirective(); | 
|  | 761 | return true; | 
|  | 762 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 763 |  | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 764 | PP.Lex(FlagTok); | 
|  | 765 | if (FlagTok.is(tok::eom)) return false; | 
|  | 766 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP)) | 
|  | 767 | return true; | 
|  | 768 | } | 
|  | 769 |  | 
|  | 770 | // We must have 3 if there are still flags. | 
|  | 771 | if (FlagVal != 3) { | 
|  | 772 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); | 
| Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 773 | PP.DiscardUntilEndOfDirective(); | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 774 | return true; | 
|  | 775 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 776 |  | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 777 | IsSystemHeader = true; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 778 |  | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 779 | PP.Lex(FlagTok); | 
|  | 780 | if (FlagTok.is(tok::eom)) return false; | 
| Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 781 | if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP)) | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 782 | return true; | 
|  | 783 |  | 
|  | 784 | // We must have 4 if there is yet another flag. | 
|  | 785 | if (FlagVal != 4) { | 
|  | 786 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); | 
| Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 787 | PP.DiscardUntilEndOfDirective(); | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 788 | return true; | 
|  | 789 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 790 |  | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 791 | IsExternCHeader = true; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 |  | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 793 | PP.Lex(FlagTok); | 
|  | 794 | if (FlagTok.is(tok::eom)) return false; | 
|  | 795 |  | 
|  | 796 | // There are no more valid flags here. | 
|  | 797 | PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag); | 
| Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 798 | PP.DiscardUntilEndOfDirective(); | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 799 | return true; | 
|  | 800 | } | 
|  | 801 |  | 
|  | 802 | /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is | 
|  | 803 | /// one of the following forms: | 
|  | 804 | /// | 
|  | 805 | ///     # 42 | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 806 | ///     # 42 "file" ('1' | '2')? | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 807 | ///     # 42 "file" ('1' | '2')? '3' '4'? | 
|  | 808 | /// | 
|  | 809 | void Preprocessor::HandleDigitDirective(Token &DigitTok) { | 
|  | 810 | // Validate the number and convert it to an unsigned.  GNU does not have a | 
|  | 811 | // line # limit other than it fit in 32-bits. | 
|  | 812 | unsigned LineNo; | 
|  | 813 | if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer, | 
|  | 814 | *this)) | 
|  | 815 | return; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 |  | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 817 | Token StrTok; | 
|  | 818 | Lex(StrTok); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 819 |  | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 820 | bool IsFileEntry = false, IsFileExit = false; | 
|  | 821 | bool IsSystemHeader = false, IsExternCHeader = false; | 
| Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 822 | int FilenameID = -1; | 
|  | 823 |  | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 824 | // If the StrTok is "eom", then it wasn't present.  Otherwise, it must be a | 
|  | 825 | // string followed by eom. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 826 | if (StrTok.is(tok::eom)) | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 827 | ; // ok | 
|  | 828 | else if (StrTok.isNot(tok::string_literal)) { | 
|  | 829 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); | 
| Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 830 | return DiscardUntilEndOfDirective(); | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 831 | } else { | 
| Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 832 | // Parse and validate the string, converting it into a unique ID. | 
|  | 833 | StringLiteralParser Literal(&StrTok, 1, *this); | 
|  | 834 | assert(!Literal.AnyWide && "Didn't allow wide strings in"); | 
|  | 835 | if (Literal.hadError) | 
|  | 836 | return DiscardUntilEndOfDirective(); | 
|  | 837 | if (Literal.Pascal) { | 
|  | 838 | Diag(StrTok, diag::err_pp_linemarker_invalid_filename); | 
|  | 839 | return DiscardUntilEndOfDirective(); | 
|  | 840 | } | 
|  | 841 | FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString(), | 
|  | 842 | Literal.GetStringLength()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 |  | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 844 | // If a filename was present, read any flags that are present. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, | 
| Chris Lattner | b5fba6f | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 846 | IsSystemHeader, IsExternCHeader, *this)) | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 847 | return; | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 848 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 |  | 
| Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 850 | // Create a line note with this information. | 
|  | 851 | SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | IsFileEntry, IsFileExit, | 
| Chris Lattner | 0a1a8d8 | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 853 | IsSystemHeader, IsExternCHeader); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 854 |  | 
| Chris Lattner | 839150e | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 855 | // If the preprocessor has callbacks installed, notify them of the #line | 
|  | 856 | // change.  This is used so that the line marker comes out in -E mode for | 
|  | 857 | // example. | 
|  | 858 | if (Callbacks) { | 
|  | 859 | PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile; | 
|  | 860 | if (IsFileEntry) | 
|  | 861 | Reason = PPCallbacks::EnterFile; | 
|  | 862 | else if (IsFileExit) | 
|  | 863 | Reason = PPCallbacks::ExitFile; | 
|  | 864 | SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User; | 
|  | 865 | if (IsExternCHeader) | 
|  | 866 | FileKind = SrcMgr::C_ExternCSystem; | 
|  | 867 | else if (IsSystemHeader) | 
|  | 868 | FileKind = SrcMgr::C_System; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 869 |  | 
| Chris Lattner | c745cec | 2010-04-14 04:28:50 +0000 | [diff] [blame] | 870 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind); | 
| Chris Lattner | 839150e | 2009-03-27 17:13:49 +0000 | [diff] [blame] | 871 | } | 
| Chris Lattner | 76e6896 | 2009-01-26 06:19:46 +0000 | [diff] [blame] | 872 | } | 
|  | 873 |  | 
|  | 874 |  | 
| Chris Lattner | 38d7fd2 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 875 | /// HandleUserDiagnosticDirective - Handle a #warning or #error directive. | 
|  | 876 | /// | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 877 | void Preprocessor::HandleUserDiagnosticDirective(Token &Tok, | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 878 | bool isWarning) { | 
| Chris Lattner | 38d7fd2 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 879 | // PTH doesn't emit #warning or #error directives. | 
|  | 880 | if (CurPTHLexer) | 
| Chris Lattner | 100c65e | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 881 | return CurPTHLexer->DiscardToEndOfLine(); | 
|  | 882 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 883 | // Read the rest of the line raw.  We do this because we don't want macros | 
|  | 884 | // to be expanded and we don't require that the tokens be valid preprocessing | 
|  | 885 | // tokens.  For example, this is allowed: "#warning `   'foo".  GCC does | 
|  | 886 | // collapse multiple consequtive white space between tokens, but this isn't | 
|  | 887 | // specified by the standard. | 
| Chris Lattner | 100c65e | 2009-01-26 05:29:08 +0000 | [diff] [blame] | 888 | std::string Message = CurLexer->ReadToEndOfLine(); | 
|  | 889 | if (isWarning) | 
|  | 890 | Diag(Tok, diag::pp_hash_warning) << Message; | 
|  | 891 | else | 
|  | 892 | Diag(Tok, diag::err_pp_hash_error) << Message; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 893 | } | 
|  | 894 |  | 
|  | 895 | /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive. | 
|  | 896 | /// | 
|  | 897 | void Preprocessor::HandleIdentSCCSDirective(Token &Tok) { | 
|  | 898 | // Yes, this directive is an extension. | 
|  | 899 | Diag(Tok, diag::ext_pp_ident_directive); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 901 | // Read the string argument. | 
|  | 902 | Token StrTok; | 
|  | 903 | Lex(StrTok); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 904 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 905 | // If the token kind isn't a string, it's a malformed directive. | 
|  | 906 | if (StrTok.isNot(tok::string_literal) && | 
| Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 907 | StrTok.isNot(tok::wide_string_literal)) { | 
|  | 908 | Diag(StrTok, diag::err_pp_malformed_ident); | 
| Chris Lattner | 38d7fd2 | 2009-01-26 05:30:54 +0000 | [diff] [blame] | 909 | if (StrTok.isNot(tok::eom)) | 
|  | 910 | DiscardUntilEndOfDirective(); | 
| Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 911 | return; | 
|  | 912 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 914 | // Verify that there is nothing after the string, other than EOM. | 
| Chris Lattner | ce2ab6f | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 915 | CheckEndOfDirective("ident"); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 916 |  | 
| Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 917 | if (Callbacks) { | 
|  | 918 | bool Invalid = false; | 
|  | 919 | std::string Str = getSpelling(StrTok, &Invalid); | 
|  | 920 | if (!Invalid) | 
|  | 921 | Callbacks->Ident(Tok.getLocation(), Str); | 
|  | 922 | } | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 923 | } | 
|  | 924 |  | 
|  | 925 | //===----------------------------------------------------------------------===// | 
|  | 926 | // Preprocessor Include Directive Handling. | 
|  | 927 | //===----------------------------------------------------------------------===// | 
|  | 928 |  | 
|  | 929 | /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully | 
|  | 930 | /// checked and spelled filename, e.g. as an operand of #include. This returns | 
|  | 931 | /// true if the input filename was in <>'s or false if it were in ""'s.  The | 
|  | 932 | /// caller is expected to provide a buffer that is large enough to hold the | 
|  | 933 | /// spelling of the filename, but is also expected to handle the case when | 
|  | 934 | /// this method decides to use a different buffer. | 
|  | 935 | bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc, | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 936 | llvm::StringRef &Buffer) { | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 937 | // Get the text form of the filename. | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 938 | assert(!Buffer.empty() && "Can't have tokens with empty spellings!"); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 939 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 940 | // Make sure the filename is <x> or "x". | 
|  | 941 | bool isAngled; | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 942 | if (Buffer[0] == '<') { | 
|  | 943 | if (Buffer.back() != '>') { | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 944 | Diag(Loc, diag::err_pp_expects_filename); | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 945 | Buffer = llvm::StringRef(); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 946 | return true; | 
|  | 947 | } | 
|  | 948 | isAngled = true; | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 949 | } else if (Buffer[0] == '"') { | 
|  | 950 | if (Buffer.back() != '"') { | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 951 | Diag(Loc, diag::err_pp_expects_filename); | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 952 | Buffer = llvm::StringRef(); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 953 | return true; | 
|  | 954 | } | 
|  | 955 | isAngled = false; | 
|  | 956 | } else { | 
|  | 957 | Diag(Loc, diag::err_pp_expects_filename); | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 958 | Buffer = llvm::StringRef(); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 959 | return true; | 
|  | 960 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 962 | // Diagnose #include "" as invalid. | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 963 | if (Buffer.size() <= 2) { | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 964 | Diag(Loc, diag::err_pp_empty_filename); | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 965 | Buffer = llvm::StringRef(); | 
|  | 966 | return true; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 967 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 968 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 969 | // Skip the brackets. | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 970 | Buffer = Buffer.substr(1, Buffer.size()-2); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 971 | return isAngled; | 
|  | 972 | } | 
|  | 973 |  | 
|  | 974 | /// ConcatenateIncludeName - Handle cases where the #include name is expanded | 
|  | 975 | /// from a macro as multiple tokens, which need to be glued together.  This | 
|  | 976 | /// occurs for code like: | 
|  | 977 | ///    #define FOO <a/b.h> | 
|  | 978 | ///    #include FOO | 
|  | 979 | /// because in this case, "<a/b.h>" is returned as 7 tokens, not one. | 
|  | 980 | /// | 
|  | 981 | /// This code concatenates and consumes tokens up to the '>' token.  It returns | 
|  | 982 | /// false if the > was found, otherwise it returns true if it finds and consumes | 
|  | 983 | /// the EOM marker. | 
| John Thompson | b535352 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 984 | bool Preprocessor::ConcatenateIncludeName( | 
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 985 | llvm::SmallString<128> &FilenameBuffer) { | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 986 | Token CurTok; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 987 |  | 
| John Thompson | b535352 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 988 | Lex(CurTok); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 989 | while (CurTok.isNot(tok::eom)) { | 
|  | 990 | // Append the spelling of this token to the buffer. If there was a space | 
|  | 991 | // before it, add it now. | 
|  | 992 | if (CurTok.hasLeadingSpace()) | 
|  | 993 | FilenameBuffer.push_back(' '); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 994 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 995 | // Get the spelling of the token, directly into FilenameBuffer if possible. | 
|  | 996 | unsigned PreAppendSize = FilenameBuffer.size(); | 
|  | 997 | FilenameBuffer.resize(PreAppendSize+CurTok.getLength()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 998 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 999 | const char *BufPtr = &FilenameBuffer[PreAppendSize]; | 
| John Thompson | b535352 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1000 | unsigned ActualLen = getSpelling(CurTok, BufPtr); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1002 | // If the token was spelled somewhere else, copy it into FilenameBuffer. | 
|  | 1003 | if (BufPtr != &FilenameBuffer[PreAppendSize]) | 
|  | 1004 | memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1006 | // Resize FilenameBuffer to the correct size. | 
|  | 1007 | if (CurTok.getLength() != ActualLen) | 
|  | 1008 | FilenameBuffer.resize(PreAppendSize+ActualLen); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1009 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1010 | // If we found the '>' marker, return success. | 
|  | 1011 | if (CurTok.is(tok::greater)) | 
|  | 1012 | return false; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1013 |  | 
| John Thompson | b535352 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1014 | Lex(CurTok); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1015 | } | 
|  | 1016 |  | 
|  | 1017 | // If we hit the eom marker, emit an error and return true so that the caller | 
|  | 1018 | // knows the EOM has been read. | 
| John Thompson | b535352 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1019 | Diag(CurTok.getLocation(), diag::err_pp_expects_filename); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1020 | return true; | 
|  | 1021 | } | 
|  | 1022 |  | 
|  | 1023 | /// HandleIncludeDirective - The "#include" tokens have just been read, read the | 
|  | 1024 | /// file to be included from the lexer, then include it!  This is a common | 
|  | 1025 | /// routine with functionality shared between #include, #include_next and | 
| Chris Lattner | c88a23e | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 1026 | /// #import.  LookupFrom is set when this is a #include_next directive, it | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1027 | /// specifies the file to start searching from. | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1028 | void Preprocessor::HandleIncludeDirective(Token &IncludeTok, | 
|  | 1029 | const DirectoryLookup *LookupFrom, | 
|  | 1030 | bool isImport) { | 
|  | 1031 |  | 
|  | 1032 | Token FilenameTok; | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1033 | CurPPLexer->LexIncludeFilename(FilenameTok); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1034 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1035 | // Reserve a buffer to get the spelling. | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1036 | llvm::SmallString<128> FilenameBuffer; | 
|  | 1037 | llvm::StringRef Filename; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1038 |  | 
|  | 1039 | switch (FilenameTok.getKind()) { | 
|  | 1040 | case tok::eom: | 
|  | 1041 | // If the token kind is EOM, the error has already been diagnosed. | 
|  | 1042 | return; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1043 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1044 | case tok::angle_string_literal: | 
| Benjamin Kramer | 0a1abd4 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 1045 | case tok::string_literal: | 
|  | 1046 | Filename = getSpelling(FilenameTok, FilenameBuffer); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1047 | break; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1048 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1049 | case tok::less: | 
|  | 1050 | // This could be a <foo/bar.h> file coming from a macro expansion.  In this | 
|  | 1051 | // case, glue the tokens together into FilenameBuffer and interpret those. | 
|  | 1052 | FilenameBuffer.push_back('<'); | 
| John Thompson | b535352 | 2009-10-30 13:49:06 +0000 | [diff] [blame] | 1053 | if (ConcatenateIncludeName(FilenameBuffer)) | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1054 | return;   // Found <eom> but no ">"?  Diagnostic already emitted. | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1055 | Filename = FilenameBuffer.str(); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1056 | break; | 
|  | 1057 | default: | 
|  | 1058 | Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); | 
|  | 1059 | DiscardUntilEndOfDirective(); | 
|  | 1060 | return; | 
|  | 1061 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1062 |  | 
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1063 | bool isAngled = | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1064 | GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1065 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an | 
|  | 1066 | // error. | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1067 | if (Filename.empty()) { | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1068 | DiscardUntilEndOfDirective(); | 
|  | 1069 | return; | 
|  | 1070 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 |  | 
| Chris Lattner | b40289b | 2009-04-17 23:56:52 +0000 | [diff] [blame] | 1072 | // Verify that there is nothing after the filename, other than EOM.  Note that | 
|  | 1073 | // we allow macros that expand to nothing after the filename, because this | 
|  | 1074 | // falls into the category of "#include pp-tokens new-line" specified in | 
|  | 1075 | // C99 6.10.2p4. | 
| Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 1076 | CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1077 |  | 
|  | 1078 | // Check that we don't have infinite #include recursion. | 
| Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1079 | if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) { | 
|  | 1080 | Diag(FilenameTok, diag::err_pp_include_too_deep); | 
|  | 1081 | return; | 
|  | 1082 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1084 | // Search include directories. | 
|  | 1085 | const DirectoryLookup *CurDir; | 
| Chris Lattner | fde8535 | 2010-01-22 00:14:44 +0000 | [diff] [blame] | 1086 | const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir); | 
| Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1087 | if (File == 0) { | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1088 | Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; | 
| Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1089 | return; | 
|  | 1090 | } | 
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1091 |  | 
| Chris Lattner | c88a23e | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 1092 | // The #included file will be considered to be a system header if either it is | 
|  | 1093 | // in a system include directory, or if the #includer is a system include | 
|  | 1094 | // header. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | SrcMgr::CharacteristicKind FileCharacter = | 
| Chris Lattner | b03dc76 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 1096 | std::max(HeaderInfo.getFileDirFlavor(File), | 
| Chris Lattner | c033416 | 2009-01-19 07:59:15 +0000 | [diff] [blame] | 1097 | SourceMgr.getFileCharacteristic(FilenameTok.getLocation())); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 |  | 
| Chris Lattner | 72286d6 | 2010-04-19 20:44:31 +0000 | [diff] [blame] | 1099 | // Ask HeaderInfo if we should enter this #include file.  If not, #including | 
|  | 1100 | // this file will have no effect. | 
|  | 1101 | if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) { | 
| Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1102 | if (Callbacks) | 
| Chris Lattner | 72286d6 | 2010-04-19 20:44:31 +0000 | [diff] [blame] | 1103 | Callbacks->FileSkipped(*File, FilenameTok, FileCharacter); | 
| Chris Lattner | 72286d6 | 2010-04-19 20:44:31 +0000 | [diff] [blame] | 1104 | return; | 
|  | 1105 | } | 
|  | 1106 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1107 | // Look up the file, create a File ID for it. | 
| Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 1108 | FileID FID = SourceMgr.createFileID(File, FilenameTok.getLocation(), | 
|  | 1109 | FileCharacter); | 
|  | 1110 | if (FID.isInvalid()) { | 
| Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 1111 | Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; | 
| Chris Lattner | 97b8e84 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 1112 | return; | 
|  | 1113 | } | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1114 |  | 
|  | 1115 | // Finally, if all is good, enter the new file! | 
| Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 1116 | EnterSourceFile(FID, CurDir, FilenameTok.getLocation()); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1117 | } | 
|  | 1118 |  | 
|  | 1119 | /// HandleIncludeNextDirective - Implements #include_next. | 
|  | 1120 | /// | 
|  | 1121 | void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) { | 
|  | 1122 | Diag(IncludeNextTok, diag::ext_pp_include_next_directive); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1124 | // #include_next is like #include, except that we start searching after | 
|  | 1125 | // the current found directory.  If we can't do this, issue a | 
|  | 1126 | // diagnostic. | 
|  | 1127 | const DirectoryLookup *Lookup = CurDirLookup; | 
|  | 1128 | if (isInPrimaryFile()) { | 
|  | 1129 | Lookup = 0; | 
|  | 1130 | Diag(IncludeNextTok, diag::pp_include_next_in_primary); | 
|  | 1131 | } else if (Lookup == 0) { | 
|  | 1132 | Diag(IncludeNextTok, diag::pp_include_next_absolute_path); | 
|  | 1133 | } else { | 
|  | 1134 | // Start looking up in the next directory. | 
|  | 1135 | ++Lookup; | 
|  | 1136 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1138 | return HandleIncludeDirective(IncludeNextTok, Lookup); | 
|  | 1139 | } | 
|  | 1140 |  | 
|  | 1141 | /// HandleImportDirective - Implements #import. | 
|  | 1142 | /// | 
|  | 1143 | void Preprocessor::HandleImportDirective(Token &ImportTok) { | 
| Chris Lattner | d4a9673 | 2009-03-06 04:28:03 +0000 | [diff] [blame] | 1144 | if (!Features.ObjC1)  // #import is standard for ObjC. | 
|  | 1145 | Diag(ImportTok, diag::ext_pp_import_directive); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1146 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1147 | return HandleIncludeDirective(ImportTok, 0, true); | 
|  | 1148 | } | 
|  | 1149 |  | 
| Chris Lattner | 58a1eb0 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 1150 | /// HandleIncludeMacrosDirective - The -imacros command line option turns into a | 
|  | 1151 | /// pseudo directive in the predefines buffer.  This handles it by sucking all | 
|  | 1152 | /// tokens through the preprocessor and discarding them (only keeping the side | 
|  | 1153 | /// effects on the preprocessor). | 
|  | 1154 | void Preprocessor::HandleIncludeMacrosDirective(Token &IncludeMacrosTok) { | 
|  | 1155 | // This directive should only occur in the predefines buffer.  If not, emit an | 
|  | 1156 | // error and reject it. | 
|  | 1157 | SourceLocation Loc = IncludeMacrosTok.getLocation(); | 
|  | 1158 | if (strcmp(SourceMgr.getBufferName(Loc), "<built-in>") != 0) { | 
|  | 1159 | Diag(IncludeMacrosTok.getLocation(), | 
|  | 1160 | diag::pp_include_macros_out_of_predefines); | 
|  | 1161 | DiscardUntilEndOfDirective(); | 
|  | 1162 | return; | 
|  | 1163 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1164 |  | 
| Chris Lattner | e01d82b | 2009-04-08 20:53:24 +0000 | [diff] [blame] | 1165 | // Treat this as a normal #include for checking purposes.  If this is | 
|  | 1166 | // successful, it will push a new lexer onto the include stack. | 
|  | 1167 | HandleIncludeDirective(IncludeMacrosTok, 0, false); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 |  | 
| Chris Lattner | e01d82b | 2009-04-08 20:53:24 +0000 | [diff] [blame] | 1169 | Token TmpTok; | 
|  | 1170 | do { | 
|  | 1171 | Lex(TmpTok); | 
|  | 1172 | assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!"); | 
|  | 1173 | } while (TmpTok.isNot(tok::hashhash)); | 
| Chris Lattner | 58a1eb0 | 2009-04-08 18:46:40 +0000 | [diff] [blame] | 1174 | } | 
|  | 1175 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1176 | //===----------------------------------------------------------------------===// | 
|  | 1177 | // Preprocessor Macro Directive Handling. | 
|  | 1178 | //===----------------------------------------------------------------------===// | 
|  | 1179 |  | 
|  | 1180 | /// ReadMacroDefinitionArgList - The ( starting an argument list of a macro | 
|  | 1181 | /// definition has just been read.  Lex the rest of the arguments and the | 
|  | 1182 | /// closing ), updating MI with what we learn.  Return true if an error occurs | 
|  | 1183 | /// parsing the arg list. | 
|  | 1184 | bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) { | 
|  | 1185 | llvm::SmallVector<IdentifierInfo*, 32> Arguments; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1186 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1187 | Token Tok; | 
|  | 1188 | while (1) { | 
|  | 1189 | LexUnexpandedToken(Tok); | 
|  | 1190 | switch (Tok.getKind()) { | 
|  | 1191 | case tok::r_paren: | 
|  | 1192 | // Found the end of the argument list. | 
| Chris Lattner | f87c510 | 2009-02-20 22:31:31 +0000 | [diff] [blame] | 1193 | if (Arguments.empty())  // #define FOO() | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1194 | return false; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1195 | // Otherwise we have #define FOO(A,) | 
|  | 1196 | Diag(Tok, diag::err_pp_expected_ident_in_arg_list); | 
|  | 1197 | return true; | 
|  | 1198 | case tok::ellipsis:  // #define X(... -> C99 varargs | 
|  | 1199 | // Warn if use of C99 feature in non-C99 mode. | 
|  | 1200 | if (!Features.C99) Diag(Tok, diag::ext_variadic_macro); | 
|  | 1201 |  | 
|  | 1202 | // Lex the token after the identifier. | 
|  | 1203 | LexUnexpandedToken(Tok); | 
|  | 1204 | if (Tok.isNot(tok::r_paren)) { | 
|  | 1205 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); | 
|  | 1206 | return true; | 
|  | 1207 | } | 
|  | 1208 | // Add the __VA_ARGS__ identifier as an argument. | 
|  | 1209 | Arguments.push_back(Ident__VA_ARGS__); | 
|  | 1210 | MI->setIsC99Varargs(); | 
| Chris Lattner | 70946da | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1211 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1212 | return false; | 
|  | 1213 | case tok::eom:  // #define X( | 
|  | 1214 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); | 
|  | 1215 | return true; | 
|  | 1216 | default: | 
|  | 1217 | // Handle keywords and identifiers here to accept things like | 
|  | 1218 | // #define Foo(for) for. | 
|  | 1219 | IdentifierInfo *II = Tok.getIdentifierInfo(); | 
|  | 1220 | if (II == 0) { | 
|  | 1221 | // #define X(1 | 
|  | 1222 | Diag(Tok, diag::err_pp_invalid_tok_in_arg_list); | 
|  | 1223 | return true; | 
|  | 1224 | } | 
|  | 1225 |  | 
|  | 1226 | // If this is already used as an argument, it is used multiple times (e.g. | 
|  | 1227 | // #define X(A,A. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | if (std::find(Arguments.begin(), Arguments.end(), II) != | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1229 | Arguments.end()) {  // C99 6.10.3p6 | 
| Chris Lattner | c5cdade | 2008-11-19 07:33:58 +0000 | [diff] [blame] | 1230 | Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1231 | return true; | 
|  | 1232 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1233 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1234 | // Add the argument to the macro info. | 
|  | 1235 | Arguments.push_back(II); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1236 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1237 | // Lex the token after the identifier. | 
|  | 1238 | LexUnexpandedToken(Tok); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1239 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1240 | switch (Tok.getKind()) { | 
|  | 1241 | default:          // #define X(A B | 
|  | 1242 | Diag(Tok, diag::err_pp_expected_comma_in_arg_list); | 
|  | 1243 | return true; | 
|  | 1244 | case tok::r_paren: // #define X(A) | 
| Chris Lattner | 70946da | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1245 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1246 | return false; | 
|  | 1247 | case tok::comma:  // #define X(A, | 
|  | 1248 | break; | 
|  | 1249 | case tok::ellipsis:  // #define X(A... -> GCC extension | 
|  | 1250 | // Diagnose extension. | 
|  | 1251 | Diag(Tok, diag::ext_named_variadic_macro); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1252 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1253 | // Lex the token after the identifier. | 
|  | 1254 | LexUnexpandedToken(Tok); | 
|  | 1255 | if (Tok.isNot(tok::r_paren)) { | 
|  | 1256 | Diag(Tok, diag::err_pp_missing_rparen_in_macro_def); | 
|  | 1257 | return true; | 
|  | 1258 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1260 | MI->setIsGNUVarargs(); | 
| Chris Lattner | 70946da | 2009-02-20 22:46:43 +0000 | [diff] [blame] | 1261 | MI->setArgumentList(&Arguments[0], Arguments.size(), BP); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1262 | return false; | 
|  | 1263 | } | 
|  | 1264 | } | 
|  | 1265 | } | 
|  | 1266 | } | 
|  | 1267 |  | 
|  | 1268 | /// HandleDefineDirective - Implements #define.  This consumes the entire macro | 
|  | 1269 | /// line then lets the caller lex the next real token. | 
|  | 1270 | void Preprocessor::HandleDefineDirective(Token &DefineTok) { | 
|  | 1271 | ++NumDefined; | 
|  | 1272 |  | 
|  | 1273 | Token MacroNameTok; | 
|  | 1274 | ReadMacroName(MacroNameTok, 1); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1275 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1276 | // Error reading macro name?  If so, diagnostic already issued. | 
|  | 1277 | if (MacroNameTok.is(tok::eom)) | 
|  | 1278 | return; | 
|  | 1279 |  | 
| Chris Lattner | d6e97af | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1280 | Token LastTok = MacroNameTok; | 
|  | 1281 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1282 | // If we are supposed to keep comments in #defines, reenable comment saving | 
|  | 1283 | // mode. | 
| Ted Kremenek | 59e003e | 2008-11-18 00:43:07 +0000 | [diff] [blame] | 1284 | if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1285 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1286 | // Create the new macro. | 
| Ted Kremenek | 6c7ea11 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1287 | MacroInfo *MI = AllocateMacroInfo(MacroNameTok.getLocation()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1289 | Token Tok; | 
|  | 1290 | LexUnexpandedToken(Tok); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1291 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1292 | // If this is a function-like macro definition, parse the argument list, | 
|  | 1293 | // marking each of the identifiers as being used as macro arguments.  Also, | 
|  | 1294 | // check other constraints on the first token of the macro body. | 
|  | 1295 | if (Tok.is(tok::eom)) { | 
|  | 1296 | // If there is no body to this macro, we have no special handling here. | 
| Chris Lattner | 2425bcb | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1297 | } else if (Tok.hasLeadingSpace()) { | 
|  | 1298 | // This is a normal token with leading space.  Clear the leading space | 
|  | 1299 | // marker on the first token to get proper expansion. | 
|  | 1300 | Tok.clearFlag(Token::LeadingSpace); | 
|  | 1301 | } else if (Tok.is(tok::l_paren)) { | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1302 | // This is a function-like macro definition.  Read the argument list. | 
|  | 1303 | MI->setIsFunctionLike(); | 
|  | 1304 | if (ReadMacroDefinitionArgList(MI)) { | 
|  | 1305 | // Forget about MI. | 
| Ted Kremenek | 6c7ea11 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1306 | ReleaseMacroInfo(MI); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1307 | // Throw away the rest of the line. | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1308 | if (CurPPLexer->ParsingPreprocessorDirective) | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1309 | DiscardUntilEndOfDirective(); | 
|  | 1310 | return; | 
|  | 1311 | } | 
|  | 1312 |  | 
| Chris Lattner | 249c38b | 2009-04-19 18:26:34 +0000 | [diff] [blame] | 1313 | // If this is a definition of a variadic C99 function-like macro, not using | 
|  | 1314 | // the GNU named varargs extension, enabled __VA_ARGS__. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 |  | 
| Chris Lattner | 249c38b | 2009-04-19 18:26:34 +0000 | [diff] [blame] | 1316 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. | 
|  | 1317 | // This gets unpoisoned where it is allowed. | 
|  | 1318 | assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!"); | 
|  | 1319 | if (MI->isC99Varargs()) | 
|  | 1320 | Ident__VA_ARGS__->setIsPoisoned(false); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1321 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1322 | // Read the first token after the arg list for down below. | 
|  | 1323 | LexUnexpandedToken(Tok); | 
| Chris Lattner | 2425bcb | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1324 | } else if (Features.C99) { | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1325 | // C99 requires whitespace between the macro definition and the body.  Emit | 
|  | 1326 | // a diagnostic for something like "#define X+". | 
| Chris Lattner | 2425bcb | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1327 | Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1328 | } else { | 
| Chris Lattner | 2425bcb | 2009-04-18 02:23:25 +0000 | [diff] [blame] | 1329 | // C90 6.8 TC1 says: "In the definition of an object-like macro, if the | 
|  | 1330 | // first character of a replacement list is not a character required by | 
|  | 1331 | // subclause 5.2.1, then there shall be white-space separation between the | 
|  | 1332 | // identifier and the replacement list.".  5.2.1 lists this set: | 
|  | 1333 | //   "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which | 
|  | 1334 | // is irrelevant here. | 
|  | 1335 | bool isInvalid = false; | 
|  | 1336 | if (Tok.is(tok::at)) // @ is not in the list above. | 
|  | 1337 | isInvalid = true; | 
|  | 1338 | else if (Tok.is(tok::unknown)) { | 
|  | 1339 | // If we have an unknown token, it is something strange like "`".  Since | 
|  | 1340 | // all of valid characters would have lexed into a single character | 
|  | 1341 | // token of some sort, we know this is not a valid case. | 
|  | 1342 | isInvalid = true; | 
|  | 1343 | } | 
|  | 1344 | if (isInvalid) | 
|  | 1345 | Diag(Tok, diag::ext_missing_whitespace_after_macro_name); | 
|  | 1346 | else | 
|  | 1347 | Diag(Tok, diag::warn_missing_whitespace_after_macro_name); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1348 | } | 
| Chris Lattner | d6e97af | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1349 |  | 
|  | 1350 | if (!Tok.is(tok::eom)) | 
|  | 1351 | LastTok = Tok; | 
|  | 1352 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1353 | // Read the rest of the macro body. | 
|  | 1354 | if (MI->isObjectLike()) { | 
|  | 1355 | // Object-like macros are very simple, just read their body. | 
|  | 1356 | while (Tok.isNot(tok::eom)) { | 
| Chris Lattner | d6e97af | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1357 | LastTok = Tok; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1358 | MI->AddTokenToBody(Tok); | 
|  | 1359 | // Get the next token of the macro. | 
|  | 1360 | LexUnexpandedToken(Tok); | 
|  | 1361 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1362 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1363 | } else { | 
| Chris Lattner | 83bd828 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1364 | // Otherwise, read the body of a function-like macro.  While we are at it, | 
|  | 1365 | // check C99 6.10.3.2p1: ensure that # operators are followed by macro | 
|  | 1366 | // parameters in function-like macro expansions. | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1367 | while (Tok.isNot(tok::eom)) { | 
| Chris Lattner | d6e97af | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1368 | LastTok = Tok; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1369 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1370 | if (Tok.isNot(tok::hash)) { | 
| Chris Lattner | 83bd828 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1371 | MI->AddTokenToBody(Tok); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1373 | // Get the next token of the macro. | 
|  | 1374 | LexUnexpandedToken(Tok); | 
|  | 1375 | continue; | 
|  | 1376 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1378 | // Get the next token of the macro. | 
|  | 1379 | LexUnexpandedToken(Tok); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1380 |  | 
| Chris Lattner | 83bd828 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1381 | // Check for a valid macro arg identifier. | 
|  | 1382 | if (Tok.getIdentifierInfo() == 0 || | 
|  | 1383 | MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) { | 
|  | 1384 |  | 
|  | 1385 | // If this is assembler-with-cpp mode, we accept random gibberish after | 
|  | 1386 | // the '#' because '#' is often a comment character.  However, change | 
|  | 1387 | // the kind of the token to tok::unknown so that the preprocessor isn't | 
|  | 1388 | // confused. | 
|  | 1389 | if (getLangOptions().AsmPreprocessor && Tok.isNot(tok::eom)) { | 
|  | 1390 | LastTok.setKind(tok::unknown); | 
|  | 1391 | } else { | 
|  | 1392 | Diag(Tok, diag::err_pp_stringize_not_parameter); | 
|  | 1393 | ReleaseMacroInfo(MI); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1394 |  | 
| Chris Lattner | 83bd828 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1395 | // Disable __VA_ARGS__ again. | 
|  | 1396 | Ident__VA_ARGS__->setIsPoisoned(true); | 
|  | 1397 | return; | 
|  | 1398 | } | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1399 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1400 |  | 
| Chris Lattner | 83bd828 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1401 | // Things look ok, add the '#' and param name tokens to the macro. | 
|  | 1402 | MI->AddTokenToBody(LastTok); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1403 | MI->AddTokenToBody(Tok); | 
| Chris Lattner | 83bd828 | 2009-05-25 17:16:10 +0000 | [diff] [blame] | 1404 | LastTok = Tok; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1405 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1406 | // Get the next token of the macro. | 
|  | 1407 | LexUnexpandedToken(Tok); | 
|  | 1408 | } | 
|  | 1409 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 |  | 
|  | 1411 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1412 | // Disable __VA_ARGS__ again. | 
|  | 1413 | Ident__VA_ARGS__->setIsPoisoned(true); | 
|  | 1414 |  | 
|  | 1415 | // Check that there is no paste (##) operator at the begining or end of the | 
|  | 1416 | // replacement list. | 
|  | 1417 | unsigned NumTokens = MI->getNumTokens(); | 
|  | 1418 | if (NumTokens != 0) { | 
|  | 1419 | if (MI->getReplacementToken(0).is(tok::hashhash)) { | 
|  | 1420 | Diag(MI->getReplacementToken(0), diag::err_paste_at_start); | 
| Ted Kremenek | 6c7ea11 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1421 | ReleaseMacroInfo(MI); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1422 | return; | 
|  | 1423 | } | 
|  | 1424 | if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) { | 
|  | 1425 | Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end); | 
| Ted Kremenek | 6c7ea11 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1426 | ReleaseMacroInfo(MI); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1427 | return; | 
|  | 1428 | } | 
|  | 1429 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1430 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1431 | // If this is the primary source file, remember that this macro hasn't been | 
|  | 1432 | // used yet. | 
|  | 1433 | if (isInPrimaryFile()) | 
|  | 1434 | MI->setIsUsed(false); | 
| Chris Lattner | d6e97af | 2009-04-21 04:46:33 +0000 | [diff] [blame] | 1435 |  | 
|  | 1436 | MI->setDefinitionEndLoc(LastTok.getLocation()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1437 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1438 | // Finally, if this identifier already had a macro defined for it, verify that | 
|  | 1439 | // the macro bodies are identical and free the old definition. | 
|  | 1440 | if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) { | 
| Chris Lattner | 5244f34 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1441 | // It is very common for system headers to have tons of macro redefinitions | 
|  | 1442 | // and for warnings to be disabled in system headers.  If this is the case, | 
|  | 1443 | // then don't bother calling MacroInfo::isIdenticalTo. | 
| Chris Lattner | 80c21df | 2009-03-13 21:17:23 +0000 | [diff] [blame] | 1444 | if (!getDiagnostics().getSuppressSystemWarnings() || | 
| Chris Lattner | 5244f34 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1445 | !SourceMgr.isInSystemHeader(DefineTok.getLocation())) { | 
|  | 1446 | if (!OtherMI->isUsed()) | 
|  | 1447 | Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1448 |  | 
| Chris Lattner | 5244f34 | 2009-01-16 19:50:11 +0000 | [diff] [blame] | 1449 | // Macros must be identical.  This means all tokes and whitespace | 
|  | 1450 | // separation must be the same.  C99 6.10.3.2. | 
|  | 1451 | if (!MI->isIdenticalTo(*OtherMI, *this)) { | 
|  | 1452 | Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef) | 
|  | 1453 | << MacroNameTok.getIdentifierInfo(); | 
|  | 1454 | Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition); | 
|  | 1455 | } | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1456 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 |  | 
| Ted Kremenek | 6c7ea11 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1458 | ReleaseMacroInfo(OtherMI); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1459 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1460 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1461 | setMacroInfo(MacroNameTok.getIdentifierInfo(), MI); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 |  | 
| Chris Lattner | 928e909 | 2009-04-12 01:39:54 +0000 | [diff] [blame] | 1463 | // If the callbacks want to know, tell them about the macro definition. | 
|  | 1464 | if (Callbacks) | 
|  | 1465 | Callbacks->MacroDefined(MacroNameTok.getIdentifierInfo(), MI); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1466 | } | 
|  | 1467 |  | 
|  | 1468 | /// HandleUndefDirective - Implements #undef. | 
|  | 1469 | /// | 
|  | 1470 | void Preprocessor::HandleUndefDirective(Token &UndefTok) { | 
|  | 1471 | ++NumUndefined; | 
|  | 1472 |  | 
|  | 1473 | Token MacroNameTok; | 
|  | 1474 | ReadMacroName(MacroNameTok, 2); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1475 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1476 | // Error reading macro name?  If so, diagnostic already issued. | 
|  | 1477 | if (MacroNameTok.is(tok::eom)) | 
|  | 1478 | return; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1479 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1480 | // Check to see if this is the last token on the #undef line. | 
| Chris Lattner | ce2ab6f | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1481 | CheckEndOfDirective("undef"); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1483 | // Okay, we finally have a valid identifier to undef. | 
|  | 1484 | MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo()); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1485 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1486 | // If the macro is not defined, this is a noop undef, just return. | 
|  | 1487 | if (MI == 0) return; | 
|  | 1488 |  | 
|  | 1489 | if (!MI->isUsed()) | 
|  | 1490 | Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used); | 
| Chris Lattner | cd6d4b1 | 2009-04-21 03:42:09 +0000 | [diff] [blame] | 1491 |  | 
|  | 1492 | // If the callbacks want to know, tell them about the macro #undef. | 
|  | 1493 | if (Callbacks) | 
| Benjamin Kramer | d05f31d | 2010-08-07 22:27:00 +0000 | [diff] [blame] | 1494 | Callbacks->MacroUndefined(MacroNameTok.getLocation(), | 
|  | 1495 | MacroNameTok.getIdentifierInfo(), MI); | 
| Chris Lattner | cd6d4b1 | 2009-04-21 03:42:09 +0000 | [diff] [blame] | 1496 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1497 | // Free macro definition. | 
| Ted Kremenek | 6c7ea11 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 1498 | ReleaseMacroInfo(MI); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1499 | setMacroInfo(MacroNameTok.getIdentifierInfo(), 0); | 
|  | 1500 | } | 
|  | 1501 |  | 
|  | 1502 |  | 
|  | 1503 | //===----------------------------------------------------------------------===// | 
|  | 1504 | // Preprocessor Conditional Directive Handling. | 
|  | 1505 | //===----------------------------------------------------------------------===// | 
|  | 1506 |  | 
|  | 1507 | /// HandleIfdefDirective - Implements the #ifdef/#ifndef directive.  isIfndef is | 
|  | 1508 | /// true when this is a #ifndef directive.  ReadAnyTokensBeforeDirective is true | 
|  | 1509 | /// if any tokens have been returned or pp-directives activated before this | 
|  | 1510 | /// #ifndef has been lexed. | 
|  | 1511 | /// | 
|  | 1512 | void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef, | 
|  | 1513 | bool ReadAnyTokensBeforeDirective) { | 
|  | 1514 | ++NumIf; | 
|  | 1515 | Token DirectiveTok = Result; | 
|  | 1516 |  | 
|  | 1517 | Token MacroNameTok; | 
|  | 1518 | ReadMacroName(MacroNameTok); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1520 | // Error reading macro name?  If so, diagnostic already issued. | 
|  | 1521 | if (MacroNameTok.is(tok::eom)) { | 
|  | 1522 | // Skip code until we get to #endif.  This helps with recovery by not | 
|  | 1523 | // emitting an error when the #endif is reached. | 
|  | 1524 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), | 
|  | 1525 | /*Foundnonskip*/false, /*FoundElse*/false); | 
|  | 1526 | return; | 
|  | 1527 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1528 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1529 | // Check to see if this is the last token on the #if[n]def line. | 
| Chris Lattner | ce2ab6f | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1530 | CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef"); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1531 |  | 
| Chris Lattner | aa1cccbb | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 1532 | IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); | 
|  | 1533 | MacroInfo *MI = getMacroInfo(MII); | 
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1534 |  | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1535 | if (CurPPLexer->getConditionalStackDepth() == 0) { | 
| Chris Lattner | aa1cccbb | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 1536 | // If the start of a top-level #ifdef and if the macro is not defined, | 
|  | 1537 | // inform MIOpt that this might be the start of a proper include guard. | 
|  | 1538 | // Otherwise it is some other form of unknown conditional which we can't | 
|  | 1539 | // handle. | 
|  | 1540 | if (!ReadAnyTokensBeforeDirective && MI == 0) { | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1541 | assert(isIfndef && "#ifdef shouldn't reach here"); | 
| Chris Lattner | aa1cccbb | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 1542 | CurPPLexer->MIOpt.EnterTopLevelIFNDEF(MII); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1543 | } else | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1544 | CurPPLexer->MIOpt.EnterTopLevelConditional(); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1545 | } | 
|  | 1546 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1547 | // If there is a macro, process it. | 
|  | 1548 | if (MI)  // Mark it used. | 
|  | 1549 | MI->setIsUsed(true); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1550 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1551 | // Should we include the stuff contained by this directive? | 
|  | 1552 | if (!MI == isIfndef) { | 
|  | 1553 | // Yes, remember that we are inside a conditional, then lex the next token. | 
| Chris Lattner | 8cf1f93 | 2009-12-14 04:54:40 +0000 | [diff] [blame] | 1554 | CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(), | 
|  | 1555 | /*wasskip*/false, /*foundnonskip*/true, | 
|  | 1556 | /*foundelse*/false); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1557 | } else { | 
|  | 1558 | // No, skip the contents of this block and return the first token after it. | 
|  | 1559 | SkipExcludedConditionalBlock(DirectiveTok.getLocation(), | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1560 | /*Foundnonskip*/false, | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1561 | /*FoundElse*/false); | 
|  | 1562 | } | 
|  | 1563 | } | 
|  | 1564 |  | 
|  | 1565 | /// HandleIfDirective - Implements the #if directive. | 
|  | 1566 | /// | 
|  | 1567 | void Preprocessor::HandleIfDirective(Token &IfToken, | 
|  | 1568 | bool ReadAnyTokensBeforeDirective) { | 
|  | 1569 | ++NumIf; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1571 | // Parse and evaluation the conditional expression. | 
|  | 1572 | IdentifierInfo *IfNDefMacro = 0; | 
|  | 1573 | bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1574 |  | 
| Nuno Lopes | 363212b | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1575 |  | 
|  | 1576 | // If this condition is equivalent to #ifndef X, and if this is the first | 
|  | 1577 | // directive seen, handle it for the multiple-include optimization. | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1578 | if (CurPPLexer->getConditionalStackDepth() == 0) { | 
| Chris Lattner | aa1cccbb | 2010-02-12 08:03:27 +0000 | [diff] [blame] | 1579 | if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue) | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1580 | CurPPLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro); | 
| Nuno Lopes | 363212b | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1581 | else | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1582 | CurPPLexer->MIOpt.EnterTopLevelConditional(); | 
| Nuno Lopes | 363212b | 2008-06-01 18:31:24 +0000 | [diff] [blame] | 1583 | } | 
|  | 1584 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1585 | // Should we include the stuff contained by this directive? | 
|  | 1586 | if (ConditionalTrue) { | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1587 | // Yes, remember that we are inside a conditional, then lex the next token. | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1588 | CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false, | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1589 | /*foundnonskip*/true, /*foundelse*/false); | 
|  | 1590 | } else { | 
|  | 1591 | // No, skip the contents of this block and return the first token after it. | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1592 | SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false, | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1593 | /*FoundElse*/false); | 
|  | 1594 | } | 
|  | 1595 | } | 
|  | 1596 |  | 
|  | 1597 | /// HandleEndifDirective - Implements the #endif directive. | 
|  | 1598 | /// | 
|  | 1599 | void Preprocessor::HandleEndifDirective(Token &EndifToken) { | 
|  | 1600 | ++NumEndif; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1601 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1602 | // Check that this is the whole directive. | 
| Chris Lattner | ce2ab6f | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1603 | CheckEndOfDirective("endif"); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1604 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1605 | PPConditionalInfo CondInfo; | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1606 | if (CurPPLexer->popConditionalLevel(CondInfo)) { | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1607 | // No conditionals on the stack: this is an #endif without an #if. | 
| Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1608 | Diag(EndifToken, diag::err_pp_endif_without_if); | 
|  | 1609 | return; | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1610 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1611 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1612 | // If this the end of a top-level #endif, inform MIOpt. | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1613 | if (CurPPLexer->getConditionalStackDepth() == 0) | 
|  | 1614 | CurPPLexer->MIOpt.ExitTopLevelConditional(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1615 |  | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1616 | assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode && | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1617 | "This code should only be reachable in the non-skipping case!"); | 
|  | 1618 | } | 
|  | 1619 |  | 
|  | 1620 |  | 
|  | 1621 | void Preprocessor::HandleElseDirective(Token &Result) { | 
|  | 1622 | ++NumElse; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1624 | // #else directive in a non-skipping conditional... start skipping. | 
| Chris Lattner | ce2ab6f | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 1625 | CheckEndOfDirective("else"); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1627 | PPConditionalInfo CI; | 
| Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1628 | if (CurPPLexer->popConditionalLevel(CI)) { | 
|  | 1629 | Diag(Result, diag::pp_err_else_without_if); | 
|  | 1630 | return; | 
|  | 1631 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1632 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1633 | // If this is a top-level #else, inform the MIOpt. | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1634 | if (CurPPLexer->getConditionalStackDepth() == 0) | 
|  | 1635 | CurPPLexer->MIOpt.EnterTopLevelConditional(); | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1636 |  | 
|  | 1637 | // If this is a #else with a #else before it, report the error. | 
|  | 1638 | if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1640 | // Finally, skip the rest of the contents of this block and return the first | 
|  | 1641 | // token after it. | 
|  | 1642 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, | 
|  | 1643 | /*FoundElse*/true); | 
|  | 1644 | } | 
|  | 1645 |  | 
|  | 1646 | void Preprocessor::HandleElifDirective(Token &ElifToken) { | 
|  | 1647 | ++NumElse; | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1648 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1649 | // #elif directive in a non-skipping conditional... start skipping. | 
|  | 1650 | // We don't care what the condition is, because we will always skip it (since | 
|  | 1651 | // the block immediately before it was included). | 
|  | 1652 | DiscardUntilEndOfDirective(); | 
|  | 1653 |  | 
|  | 1654 | PPConditionalInfo CI; | 
| Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 1655 | if (CurPPLexer->popConditionalLevel(CI)) { | 
|  | 1656 | Diag(ElifToken, diag::pp_err_elif_without_if); | 
|  | 1657 | return; | 
|  | 1658 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1660 | // If this is a top-level #elif, inform the MIOpt. | 
| Ted Kremenek | 30cd88c | 2008-11-18 00:34:22 +0000 | [diff] [blame] | 1661 | if (CurPPLexer->getConditionalStackDepth() == 0) | 
|  | 1662 | CurPPLexer->MIOpt.EnterTopLevelConditional(); | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1663 |  | 
| Chris Lattner | f64b352 | 2008-03-09 01:54:53 +0000 | [diff] [blame] | 1664 | // If this is a #elif with a #else before it, report the error. | 
|  | 1665 | if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else); | 
|  | 1666 |  | 
|  | 1667 | // Finally, skip the rest of the contents of this block and return the first | 
|  | 1668 | // token after it. | 
|  | 1669 | return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true, | 
|  | 1670 | /*FoundElse*/CI.FoundElse); | 
|  | 1671 | } |