Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Pragma.cpp - Pragma registration and handling --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the PragmaHandler/PragmaTable interfaces and implements |
| 11 | // pragma related methods of the Preprocessor class. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Lex/Pragma.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/Lex/HeaderSearch.h" |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 17 | #include "clang/Lex/LiteralSupport.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 19 | #include "clang/Lex/LexDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/Basic/FileManager.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
| 25 | // Out-of-line destructor to provide a home for the class. |
| 26 | PragmaHandler::~PragmaHandler() { |
| 27 | } |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | // PragmaNamespace Implementation. |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| 33 | |
| 34 | PragmaNamespace::~PragmaNamespace() { |
| 35 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) |
| 36 | delete Handlers[i]; |
| 37 | } |
| 38 | |
| 39 | /// FindHandler - Check to see if there is already a handler for the |
| 40 | /// specified name. If not, return the handler for the null identifier if it |
| 41 | /// exists, otherwise return null. If IgnoreNull is true (the default) then |
| 42 | /// the null handler isn't returned on failure to match. |
| 43 | PragmaHandler *PragmaNamespace::FindHandler(const IdentifierInfo *Name, |
| 44 | bool IgnoreNull) const { |
| 45 | PragmaHandler *NullHandler = 0; |
| 46 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | if (Handlers[i]->getName() == Name) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | return Handlers[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | if (Handlers[i]->getName() == 0) |
| 51 | NullHandler = Handlers[i]; |
| 52 | } |
| 53 | return IgnoreNull ? 0 : NullHandler; |
| 54 | } |
| 55 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 56 | void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) { |
| 57 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
| 58 | if (Handlers[i] == Handler) { |
| 59 | Handlers[i] = Handlers.back(); |
| 60 | Handlers.pop_back(); |
| 61 | return; |
| 62 | } |
| 63 | } |
| 64 | assert(0 && "Handler not registered in this namespace"); |
| 65 | } |
| 66 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 67 | void PragmaNamespace::HandlePragma(Preprocessor &PP, Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro |
| 69 | // expand it, the user can have a STDC #define, that should not affect this. |
| 70 | PP.LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | // Get the handler for this token. If there is no handler, ignore the pragma. |
| 73 | PragmaHandler *Handler = FindHandler(Tok.getIdentifierInfo(), false); |
Chris Lattner | af7cdf4 | 2009-04-19 21:10:26 +0000 | [diff] [blame] | 74 | if (Handler == 0) { |
| 75 | PP.Diag(Tok, diag::warn_pragma_ignored); |
| 76 | return; |
| 77 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 79 | // Otherwise, pass it down. |
| 80 | Handler->HandlePragma(PP, Tok); |
| 81 | } |
| 82 | |
| 83 | //===----------------------------------------------------------------------===// |
| 84 | // Preprocessor Pragma Directive Handling. |
| 85 | //===----------------------------------------------------------------------===// |
| 86 | |
| 87 | /// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the |
| 88 | /// rest of the pragma, passing it to the registered pragma handlers. |
| 89 | void Preprocessor::HandlePragmaDirective() { |
| 90 | ++NumPragma; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 92 | // Invoke the first level of pragma handlers which reads the namespace id. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 93 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 94 | PragmaHandlers->HandlePragma(*this, Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | // If the pragma handler didn't read the rest of the line, consume it now. |
Chris Lattner | 027cff6 | 2009-06-18 05:55:53 +0000 | [diff] [blame] | 97 | if (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | DiscardUntilEndOfDirective(); |
| 99 | } |
| 100 | |
| 101 | /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then |
| 102 | /// return the first token after the directive. The _Pragma token has just |
| 103 | /// been read into 'Tok'. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 104 | void Preprocessor::Handle_Pragma(Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 105 | // Remember the pragma token location. |
| 106 | SourceLocation PragmaLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | // Read the '('. |
| 109 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 110 | if (Tok.isNot(tok::l_paren)) { |
| 111 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 112 | return; |
| 113 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 114 | |
| 115 | // Read the '"..."'. |
| 116 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 117 | if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) { |
| 118 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 119 | return; |
| 120 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | // Remember the string. |
| 123 | std::string StrVal = getSpelling(Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | |
| 125 | // Read the ')'. |
| 126 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 127 | if (Tok.isNot(tok::r_paren)) { |
| 128 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 129 | return; |
| 130 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 132 | SourceLocation RParenLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 134 | // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1: |
| 135 | // "The string literal is destringized by deleting the L prefix, if present, |
| 136 | // deleting the leading and trailing double-quotes, replacing each escape |
| 137 | // sequence \" by a double-quote, and replacing each escape sequence \\ by a |
| 138 | // single backslash." |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 139 | if (StrVal[0] == 'L') // Remove L prefix. |
| 140 | StrVal.erase(StrVal.begin()); |
| 141 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 142 | "Invalid string token!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 144 | // Remove the front quote, replacing it with a space, so that the pragma |
| 145 | // contents appear to have a space before them. |
| 146 | StrVal[0] = ' '; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 148 | // Replace the terminating quote with a \n. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 149 | StrVal[StrVal.size()-1] = '\n'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 151 | // Remove escaped quotes and escapes. |
| 152 | for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) { |
| 153 | if (StrVal[i] == '\\' && |
| 154 | (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) { |
| 155 | // \\ -> '\' and \" -> '"'. |
| 156 | StrVal.erase(StrVal.begin()+i); |
| 157 | --e; |
| 158 | } |
| 159 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 161 | // Plop the string (including the newline and trailing null) into a buffer |
| 162 | // where we can lex it. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 163 | Token TmpTok; |
| 164 | TmpTok.startToken(); |
| 165 | CreateString(&StrVal[0], StrVal.size(), TmpTok); |
| 166 | SourceLocation TokLoc = TmpTok.getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 167 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | // Make and enter a lexer object so that we lex and expand the tokens just |
| 169 | // like any others. |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 170 | Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc, |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 171 | StrVal.size(), *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | |
| 173 | EnterSourceFileWithLexer(TL, 0); |
| 174 | |
| 175 | // With everything set up, lex this as a #pragma directive. |
| 176 | HandlePragmaDirective(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | // Finally, return whatever came after the pragma directive. |
| 179 | return Lex(Tok); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | |
| 184 | /// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'. |
| 185 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 186 | void Preprocessor::HandlePragmaOnce(Token &OnceTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 187 | if (isInPrimaryFile()) { |
| 188 | Diag(OnceTok, diag::pp_pragma_once_in_main_file); |
| 189 | return; |
| 190 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 192 | // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 193 | // Mark the file as a once-only file now. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 194 | HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 197 | void Preprocessor::HandlePragmaMark() { |
Ted Kremenek | 17ff58a | 2008-11-19 22:21:33 +0000 | [diff] [blame] | 198 | assert(CurPPLexer && "No current lexer?"); |
Chris Lattner | 6896a37 | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 199 | if (CurLexer) |
| 200 | CurLexer->ReadToEndOfLine(); |
| 201 | else |
| 202 | CurPTHLexer->DiscardToEndOfLine(); |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 206 | /// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'. |
| 207 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 208 | void Preprocessor::HandlePragmaPoison(Token &PoisonTok) { |
| 209 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 210 | |
| 211 | while (1) { |
| 212 | // Read the next token to poison. While doing this, pretend that we are |
| 213 | // skipping while reading the identifier to poison. |
| 214 | // This avoids errors on code like: |
| 215 | // #pragma GCC poison X |
| 216 | // #pragma GCC poison X |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 217 | if (CurPPLexer) CurPPLexer->LexingRawMode = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 218 | LexUnexpandedToken(Tok); |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 219 | if (CurPPLexer) CurPPLexer->LexingRawMode = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 221 | // If we reached the end of line, we're done. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 222 | if (Tok.is(tok::eom)) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 223 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 224 | // Can only poison identifiers. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 225 | if (Tok.isNot(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 226 | Diag(Tok, diag::err_pp_invalid_poison); |
| 227 | return; |
| 228 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 230 | // Look up the identifier info for the token. We disabled identifier lookup |
| 231 | // by saying we're skipping contents, so we need to do this manually. |
| 232 | IdentifierInfo *II = LookUpIdentifierInfo(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 234 | // Already poisoned. |
| 235 | if (II->isPoisoned()) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 237 | // If this is a macro identifier, emit a warning. |
Chris Lattner | 0edde55 | 2007-10-07 08:04:56 +0000 | [diff] [blame] | 238 | if (II->hasMacroDefinition()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 239 | Diag(Tok, diag::pp_poisoning_existing_macro); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 241 | // Finally, poison it! |
| 242 | II->setIsPoisoned(); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know |
| 247 | /// that the whole directive has been parsed. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 248 | void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 249 | if (isInPrimaryFile()) { |
| 250 | Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file); |
| 251 | return; |
| 252 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 254 | // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc. |
Ted Kremenek | 35c10c2 | 2008-11-20 01:45:11 +0000 | [diff] [blame] | 255 | PreprocessorLexer *TheLexer = getCurrentFileLexer(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 257 | // Mark the file as a system header. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 258 | HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
| 260 | |
Chris Lattner | 6896a37 | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 261 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation()); |
| 262 | unsigned FilenameLen = strlen(PLoc.getFilename()); |
| 263 | unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(), |
| 264 | FilenameLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 6896a37 | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 266 | // Emit a line marker. This will change any source locations from this point |
| 267 | // forward to realize they are in a system header. |
| 268 | // Create a line note with this information. |
| 269 | SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID, |
| 270 | false, false, true, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 272 | // Notify the client, if desired, that we are in a new source file. |
| 273 | if (Callbacks) |
Ted Kremenek | 35c10c2 | 2008-11-20 01:45:11 +0000 | [diff] [blame] | 274 | Callbacks->FileChanged(SysHeaderTok.getLocation(), |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 275 | PPCallbacks::SystemHeaderPragma, SrcMgr::C_System); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah. |
| 279 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 280 | void Preprocessor::HandlePragmaDependency(Token &DependencyTok) { |
| 281 | Token FilenameTok; |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 282 | CurPPLexer->LexIncludeFilename(FilenameTok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 283 | |
| 284 | // If the token kind is EOM, the error has already been diagnosed. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 285 | if (FilenameTok.is(tok::eom)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 286 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 288 | // Reserve a buffer to get the spelling. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 289 | llvm::SmallString<128> FilenameBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 290 | bool Invalid = false; |
| 291 | llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid); |
| 292 | if (Invalid) |
| 293 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 295 | bool isAngled = |
| 296 | GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 297 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 298 | // error. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 299 | if (Filename.empty()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 300 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 302 | // Search include directories for this file. |
| 303 | const DirectoryLookup *CurDir; |
Chris Lattner | f45b646 | 2010-01-22 00:14:44 +0000 | [diff] [blame] | 304 | const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir); |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 305 | if (File == 0) { |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 306 | Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 307 | return; |
| 308 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 310 | const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 311 | |
| 312 | // If this file is older than the file it depends on, emit a diagnostic. |
| 313 | if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) { |
| 314 | // Lex tokens at the end of the message and include them in the message. |
| 315 | std::string Message; |
| 316 | Lex(DependencyTok); |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 317 | while (DependencyTok.isNot(tok::eom)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 318 | Message += getSpelling(DependencyTok) + " "; |
| 319 | Lex(DependencyTok); |
| 320 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 322 | Message.erase(Message.end()-1); |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 323 | Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 327 | /// HandlePragmaComment - Handle the microsoft #pragma comment extension. The |
| 328 | /// syntax is: |
| 329 | /// #pragma comment(linker, "foo") |
| 330 | /// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user. |
| 331 | /// "foo" is a string, which is fully macro expanded, and permits string |
Gabor Greif | d7ee349 | 2009-03-17 11:39:38 +0000 | [diff] [blame] | 332 | /// concatenation, embedded escape characters etc. See MSDN for more details. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 333 | void Preprocessor::HandlePragmaComment(Token &Tok) { |
| 334 | SourceLocation CommentLoc = Tok.getLocation(); |
| 335 | Lex(Tok); |
| 336 | if (Tok.isNot(tok::l_paren)) { |
| 337 | Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 338 | return; |
| 339 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 341 | // Read the identifier. |
| 342 | Lex(Tok); |
| 343 | if (Tok.isNot(tok::identifier)) { |
| 344 | Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 345 | return; |
| 346 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 348 | // Verify that this is one of the 5 whitelisted options. |
| 349 | // FIXME: warn that 'exestr' is deprecated. |
| 350 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") && |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 352 | !II->isStr("linker") && !II->isStr("user")) { |
| 353 | Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind); |
| 354 | return; |
| 355 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 357 | // Read the optional string if present. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 358 | Lex(Tok); |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 359 | std::string ArgumentString; |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 360 | if (Tok.is(tok::comma)) { |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 361 | Lex(Tok); // eat the comma. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 362 | |
| 363 | // We need at least one string. |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 364 | if (Tok.isNot(tok::string_literal)) { |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 365 | Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | // String concatenation allows multiple strings, which can even come from |
| 370 | // macro expansion. |
| 371 | // "foo " "bar" "Baz" |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 372 | llvm::SmallVector<Token, 4> StrToks; |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 373 | while (Tok.is(tok::string_literal)) { |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 374 | StrToks.push_back(Tok); |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 375 | Lex(Tok); |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | // Concatenate and parse the strings. |
| 379 | StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this); |
| 380 | assert(!Literal.AnyWide && "Didn't allow wide strings in"); |
| 381 | if (Literal.hadError) |
| 382 | return; |
| 383 | if (Literal.Pascal) { |
| 384 | Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed); |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | ArgumentString = std::string(Literal.GetString(), |
| 389 | Literal.GetString()+Literal.GetStringLength()); |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 390 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 392 | // FIXME: If the kind is "compiler" warn if the string is present (it is |
| 393 | // ignored). |
| 394 | // FIXME: 'lib' requires a comment string. |
| 395 | // FIXME: 'linker' requires a comment string, and has a specific list of |
| 396 | // things that are allowable. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 398 | if (Tok.isNot(tok::r_paren)) { |
| 399 | Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 400 | return; |
| 401 | } |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 402 | Lex(Tok); // eat the r_paren. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 403 | |
| 404 | if (Tok.isNot(tok::eom)) { |
| 405 | Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 406 | return; |
| 407 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 409 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
Chris Lattner | 172e336 | 2009-01-16 19:01:46 +0000 | [diff] [blame] | 410 | if (Callbacks) |
| 411 | Callbacks->PragmaComment(CommentLoc, II, ArgumentString); |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | |
| 415 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 416 | |
| 417 | /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. |
| 418 | /// If 'Namespace' is non-null, then it is a token required to exist on the |
| 419 | /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 420 | void Preprocessor::AddPragmaHandler(const char *Namespace, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 421 | PragmaHandler *Handler) { |
| 422 | PragmaNamespace *InsertNS = PragmaHandlers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 424 | // If this is specified to be in a namespace, step down into it. |
| 425 | if (Namespace) { |
| 426 | IdentifierInfo *NSID = getIdentifierInfo(Namespace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 428 | // If there is already a pragma handler with the name of this namespace, |
| 429 | // we either have an error (directive with the same name as a namespace) or |
| 430 | // we already have the namespace to insert into. |
| 431 | if (PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID)) { |
| 432 | InsertNS = Existing->getIfNamespace(); |
| 433 | assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma" |
| 434 | " handler with the same name!"); |
| 435 | } else { |
| 436 | // Otherwise, this namespace doesn't exist yet, create and insert the |
| 437 | // handler for it. |
| 438 | InsertNS = new PragmaNamespace(NSID); |
| 439 | PragmaHandlers->AddPragma(InsertNS); |
| 440 | } |
| 441 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 442 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 443 | // Check to make sure we don't already have a pragma for this identifier. |
| 444 | assert(!InsertNS->FindHandler(Handler->getName()) && |
| 445 | "Pragma handler already exists for this identifier!"); |
| 446 | InsertNS->AddPragma(Handler); |
| 447 | } |
| 448 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 449 | /// RemovePragmaHandler - Remove the specific pragma handler from the |
| 450 | /// preprocessor. If \arg Namespace is non-null, then it should be the |
| 451 | /// namespace that \arg Handler was added to. It is an error to remove |
| 452 | /// a handler that has not been registered. |
| 453 | void Preprocessor::RemovePragmaHandler(const char *Namespace, |
| 454 | PragmaHandler *Handler) { |
| 455 | PragmaNamespace *NS = PragmaHandlers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 457 | // If this is specified to be in a namespace, step down into it. |
| 458 | if (Namespace) { |
| 459 | IdentifierInfo *NSID = getIdentifierInfo(Namespace); |
| 460 | PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID); |
| 461 | assert(Existing && "Namespace containing handler does not exist!"); |
| 462 | |
| 463 | NS = Existing->getIfNamespace(); |
| 464 | assert(NS && "Invalid namespace, registered as a regular pragma handler!"); |
| 465 | } |
| 466 | |
| 467 | NS->RemovePragmaHandler(Handler); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 469 | // If this is a non-default namespace and it is now empty, remove |
| 470 | // it. |
| 471 | if (NS != PragmaHandlers && NS->IsEmpty()) |
| 472 | PragmaHandlers->RemovePragmaHandler(NS); |
| 473 | } |
| 474 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 475 | namespace { |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 476 | /// PragmaOnceHandler - "#pragma once" marks the file as atomically included. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 477 | struct PragmaOnceHandler : public PragmaHandler { |
| 478 | PragmaOnceHandler(const IdentifierInfo *OnceID) : PragmaHandler(OnceID) {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 479 | virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) { |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 480 | PP.CheckEndOfDirective("pragma once"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 481 | PP.HandlePragmaOnce(OnceTok); |
| 482 | } |
| 483 | }; |
| 484 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 485 | /// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the |
| 486 | /// rest of the line is not lexed. |
| 487 | struct PragmaMarkHandler : public PragmaHandler { |
| 488 | PragmaMarkHandler(const IdentifierInfo *MarkID) : PragmaHandler(MarkID) {} |
| 489 | virtual void HandlePragma(Preprocessor &PP, Token &MarkTok) { |
| 490 | PP.HandlePragmaMark(); |
| 491 | } |
| 492 | }; |
| 493 | |
| 494 | /// PragmaPoisonHandler - "#pragma poison x" marks x as not usable. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 495 | struct PragmaPoisonHandler : public PragmaHandler { |
| 496 | PragmaPoisonHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 497 | virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 498 | PP.HandlePragmaPoison(PoisonTok); |
| 499 | } |
| 500 | }; |
| 501 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 502 | /// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file |
| 503 | /// as a system header, which silences warnings in it. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 504 | struct PragmaSystemHeaderHandler : public PragmaHandler { |
| 505 | PragmaSystemHeaderHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 506 | virtual void HandlePragma(Preprocessor &PP, Token &SHToken) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 507 | PP.HandlePragmaSystemHeader(SHToken); |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 508 | PP.CheckEndOfDirective("pragma"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 509 | } |
| 510 | }; |
| 511 | struct PragmaDependencyHandler : public PragmaHandler { |
| 512 | PragmaDependencyHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 513 | virtual void HandlePragma(Preprocessor &PP, Token &DepToken) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 514 | PP.HandlePragmaDependency(DepToken); |
| 515 | } |
| 516 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 518 | /// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"' |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 519 | /// Since clang's diagnostic supports extended functionality beyond GCC's |
| 520 | /// the constructor takes a clangMode flag to tell it whether or not to allow |
| 521 | /// clang's extended functionality, or whether to reject it. |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 522 | struct PragmaDiagnosticHandler : public PragmaHandler { |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 523 | private: |
| 524 | const bool ClangMode; |
| 525 | public: |
| 526 | PragmaDiagnosticHandler(const IdentifierInfo *ID, |
| 527 | const bool clangMode) : PragmaHandler(ID), |
| 528 | ClangMode(clangMode) {} |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 529 | virtual void HandlePragma(Preprocessor &PP, Token &DiagToken) { |
| 530 | Token Tok; |
| 531 | PP.LexUnexpandedToken(Tok); |
| 532 | if (Tok.isNot(tok::identifier)) { |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 533 | unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid |
| 534 | : diag::warn_pragma_diagnostic_gcc_invalid; |
| 535 | PP.Diag(Tok, Diag); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 536 | return; |
| 537 | } |
| 538 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 540 | diag::Mapping Map; |
| 541 | if (II->isStr("warning")) |
| 542 | Map = diag::MAP_WARNING; |
| 543 | else if (II->isStr("error")) |
| 544 | Map = diag::MAP_ERROR; |
| 545 | else if (II->isStr("ignored")) |
| 546 | Map = diag::MAP_IGNORE; |
| 547 | else if (II->isStr("fatal")) |
| 548 | Map = diag::MAP_FATAL; |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 549 | else if (ClangMode) { |
| 550 | if (II->isStr("pop")) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 551 | if (!PP.getDiagnostics().popMappings()) |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 552 | PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_cannot_ppp); |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | if (II->isStr("push")) { |
| 557 | PP.getDiagnostics().pushMappings(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | return; |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | PP.Diag(Tok, diag::warn_pragma_diagnostic_clang_invalid); |
| 562 | return; |
| 563 | } else { |
| 564 | PP.Diag(Tok, diag::warn_pragma_diagnostic_gcc_invalid); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 565 | return; |
| 566 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 568 | PP.LexUnexpandedToken(Tok); |
| 569 | |
| 570 | // We need at least one string. |
| 571 | if (Tok.isNot(tok::string_literal)) { |
| 572 | PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); |
| 573 | return; |
| 574 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 575 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 576 | // String concatenation allows multiple strings, which can even come from |
| 577 | // macro expansion. |
| 578 | // "foo " "bar" "Baz" |
| 579 | llvm::SmallVector<Token, 4> StrToks; |
| 580 | while (Tok.is(tok::string_literal)) { |
| 581 | StrToks.push_back(Tok); |
| 582 | PP.LexUnexpandedToken(Tok); |
| 583 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 585 | if (Tok.isNot(tok::eom)) { |
| 586 | PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); |
| 587 | return; |
| 588 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 589 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 590 | // Concatenate and parse the strings. |
| 591 | StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP); |
| 592 | assert(!Literal.AnyWide && "Didn't allow wide strings in"); |
| 593 | if (Literal.hadError) |
| 594 | return; |
| 595 | if (Literal.Pascal) { |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 596 | unsigned Diag = ClangMode ? diag::warn_pragma_diagnostic_clang_invalid |
| 597 | : diag::warn_pragma_diagnostic_gcc_invalid; |
| 598 | PP.Diag(Tok, Diag); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 599 | return; |
| 600 | } |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 601 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 602 | std::string WarningName(Literal.GetString(), |
| 603 | Literal.GetString()+Literal.GetStringLength()); |
| 604 | |
| 605 | if (WarningName.size() < 3 || WarningName[0] != '-' || |
| 606 | WarningName[1] != 'W') { |
| 607 | PP.Diag(StrToks[0].getLocation(), |
| 608 | diag::warn_pragma_diagnostic_invalid_option); |
| 609 | return; |
| 610 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 611 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 612 | if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2, |
| 613 | Map)) |
| 614 | PP.Diag(StrToks[0].getLocation(), |
| 615 | diag::warn_pragma_diagnostic_unknown_warning) << WarningName; |
| 616 | } |
| 617 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 618 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 619 | /// PragmaCommentHandler - "#pragma comment ...". |
| 620 | struct PragmaCommentHandler : public PragmaHandler { |
| 621 | PragmaCommentHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {} |
| 622 | virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) { |
| 623 | PP.HandlePragmaComment(CommentTok); |
| 624 | } |
| 625 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 626 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 627 | // Pragma STDC implementations. |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 628 | |
| 629 | enum STDCSetting { |
| 630 | STDC_ON, STDC_OFF, STDC_DEFAULT, STDC_INVALID |
| 631 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 632 | |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 633 | static STDCSetting LexOnOffSwitch(Preprocessor &PP) { |
| 634 | Token Tok; |
| 635 | PP.LexUnexpandedToken(Tok); |
| 636 | |
| 637 | if (Tok.isNot(tok::identifier)) { |
| 638 | PP.Diag(Tok, diag::ext_stdc_pragma_syntax); |
| 639 | return STDC_INVALID; |
| 640 | } |
| 641 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 642 | STDCSetting Result; |
| 643 | if (II->isStr("ON")) |
| 644 | Result = STDC_ON; |
| 645 | else if (II->isStr("OFF")) |
| 646 | Result = STDC_OFF; |
| 647 | else if (II->isStr("DEFAULT")) |
| 648 | Result = STDC_DEFAULT; |
| 649 | else { |
| 650 | PP.Diag(Tok, diag::ext_stdc_pragma_syntax); |
| 651 | return STDC_INVALID; |
| 652 | } |
| 653 | |
| 654 | // Verify that this is followed by EOM. |
| 655 | PP.LexUnexpandedToken(Tok); |
| 656 | if (Tok.isNot(tok::eom)) |
| 657 | PP.Diag(Tok, diag::ext_stdc_pragma_syntax_eom); |
| 658 | return Result; |
| 659 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 661 | /// PragmaSTDC_FP_CONTRACTHandler - "#pragma STDC FP_CONTRACT ...". |
| 662 | struct PragmaSTDC_FP_CONTRACTHandler : public PragmaHandler { |
| 663 | PragmaSTDC_FP_CONTRACTHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {} |
Chris Lattner | f545be5 | 2009-04-19 21:25:37 +0000 | [diff] [blame] | 664 | virtual void HandlePragma(Preprocessor &PP, Token &Tok) { |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 665 | // We just ignore the setting of FP_CONTRACT. Since we don't do contractions |
| 666 | // at all, our default is OFF and setting it to ON is an optimization hint |
| 667 | // we can safely ignore. When we support -ffma or something, we would need |
| 668 | // to diagnose that we are ignoring FMA. |
| 669 | LexOnOffSwitch(PP); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 670 | } |
| 671 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 672 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 673 | /// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...". |
| 674 | struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler { |
| 675 | PragmaSTDC_FENV_ACCESSHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {} |
Chris Lattner | f545be5 | 2009-04-19 21:25:37 +0000 | [diff] [blame] | 676 | virtual void HandlePragma(Preprocessor &PP, Token &Tok) { |
Chris Lattner | 4d8aac3 | 2009-04-19 21:55:32 +0000 | [diff] [blame] | 677 | if (LexOnOffSwitch(PP) == STDC_ON) |
| 678 | PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 679 | } |
| 680 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 681 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 682 | /// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...". |
| 683 | struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler { |
| 684 | PragmaSTDC_CX_LIMITED_RANGEHandler(const IdentifierInfo *ID) |
| 685 | : PragmaHandler(ID) {} |
Chris Lattner | f545be5 | 2009-04-19 21:25:37 +0000 | [diff] [blame] | 686 | virtual void HandlePragma(Preprocessor &PP, Token &Tok) { |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 687 | LexOnOffSwitch(PP); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 688 | } |
| 689 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 690 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 691 | /// PragmaSTDC_UnknownHandler - "#pragma STDC ...". |
| 692 | struct PragmaSTDC_UnknownHandler : public PragmaHandler { |
| 693 | PragmaSTDC_UnknownHandler() : PragmaHandler(0) {} |
Chris Lattner | f545be5 | 2009-04-19 21:25:37 +0000 | [diff] [blame] | 694 | virtual void HandlePragma(Preprocessor &PP, Token &UnknownTok) { |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 695 | // C99 6.10.6p2, unknown forms are not allowed. |
Chris Lattner | f545be5 | 2009-04-19 21:25:37 +0000 | [diff] [blame] | 696 | PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 697 | } |
| 698 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 699 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 700 | } // end anonymous namespace |
| 701 | |
| 702 | |
| 703 | /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas: |
| 704 | /// #pragma GCC poison/system_header/dependency and #pragma once. |
| 705 | void Preprocessor::RegisterBuiltinPragmas() { |
| 706 | AddPragmaHandler(0, new PragmaOnceHandler(getIdentifierInfo("once"))); |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 707 | AddPragmaHandler(0, new PragmaMarkHandler(getIdentifierInfo("mark"))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 708 | |
Chris Lattner | e8fa06e | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 709 | // #pragma GCC ... |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 710 | AddPragmaHandler("GCC", new PragmaPoisonHandler(getIdentifierInfo("poison"))); |
| 711 | AddPragmaHandler("GCC", new PragmaSystemHeaderHandler( |
| 712 | getIdentifierInfo("system_header"))); |
| 713 | AddPragmaHandler("GCC", new PragmaDependencyHandler( |
| 714 | getIdentifierInfo("dependency"))); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 715 | AddPragmaHandler("GCC", new PragmaDiagnosticHandler( |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 716 | getIdentifierInfo("diagnostic"), |
| 717 | false)); |
Chris Lattner | e8fa06e | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 718 | // #pragma clang ... |
| 719 | AddPragmaHandler("clang", new PragmaPoisonHandler( |
| 720 | getIdentifierInfo("poison"))); |
| 721 | AddPragmaHandler("clang", new PragmaSystemHeaderHandler( |
| 722 | getIdentifierInfo("system_header"))); |
| 723 | AddPragmaHandler("clang", new PragmaDependencyHandler( |
| 724 | getIdentifierInfo("dependency"))); |
| 725 | AddPragmaHandler("clang", new PragmaDiagnosticHandler( |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 726 | getIdentifierInfo("diagnostic"), |
| 727 | true)); |
Chris Lattner | e8fa06e | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 728 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 729 | AddPragmaHandler("STDC", new PragmaSTDC_FP_CONTRACTHandler( |
| 730 | getIdentifierInfo("FP_CONTRACT"))); |
| 731 | AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler( |
| 732 | getIdentifierInfo("FENV_ACCESS"))); |
| 733 | AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler( |
| 734 | getIdentifierInfo("CX_LIMITED_RANGE"))); |
| 735 | AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 736 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 737 | // MS extensions. |
| 738 | if (Features.Microsoft) |
| 739 | AddPragmaHandler(0, new PragmaCommentHandler(getIdentifierInfo("comment"))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 740 | } |