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