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 | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 19 | #include "clang/Lex/MacroInfo.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 20 | #include "clang/Lex/LexDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "clang/Basic/FileManager.h" |
| 22 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | ff759a6 | 2010-08-18 23:09:23 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CrashRecoveryContext.h" |
Daniel Dunbar | 5505413 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
| 28 | // Out-of-line destructor to provide a home for the class. |
| 29 | PragmaHandler::~PragmaHandler() { |
| 30 | } |
| 31 | |
| 32 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | c72cc50 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 33 | // EmptyPragmaHandler Implementation. |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 36 | EmptyPragmaHandler::EmptyPragmaHandler() {} |
Daniel Dunbar | c72cc50 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 37 | |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 38 | void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, |
| 39 | PragmaIntroducerKind Introducer, |
| 40 | Token &FirstToken) {} |
Daniel Dunbar | c72cc50 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 41 | |
| 42 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | // PragmaNamespace Implementation. |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | |
| 46 | |
| 47 | PragmaNamespace::~PragmaNamespace() { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 48 | for (llvm::StringMap<PragmaHandler*>::iterator |
| 49 | I = Handlers.begin(), E = Handlers.end(); I != E; ++I) |
| 50 | delete I->second; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /// FindHandler - Check to see if there is already a handler for the |
| 54 | /// specified name. If not, return the handler for the null identifier if it |
| 55 | /// exists, otherwise return null. If IgnoreNull is true (the default) then |
| 56 | /// the null handler isn't returned on failure to match. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 57 | PragmaHandler *PragmaNamespace::FindHandler(StringRef Name, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 58 | bool IgnoreNull) const { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 59 | if (PragmaHandler *Handler = Handlers.lookup(Name)) |
| 60 | return Handler; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 61 | return IgnoreNull ? 0 : Handlers.lookup(StringRef()); |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 62 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 64 | void PragmaNamespace::AddPragma(PragmaHandler *Handler) { |
| 65 | assert(!Handlers.lookup(Handler->getName()) && |
| 66 | "A handler with this name is already registered in this namespace"); |
| 67 | llvm::StringMapEntry<PragmaHandler *> &Entry = |
| 68 | Handlers.GetOrCreateValue(Handler->getName()); |
| 69 | Entry.setValue(Handler); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 72 | void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 73 | assert(Handlers.lookup(Handler->getName()) && |
| 74 | "Handler not registered in this namespace"); |
| 75 | Handlers.erase(Handler->getName()); |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 78 | void PragmaNamespace::HandlePragma(Preprocessor &PP, |
| 79 | PragmaIntroducerKind Introducer, |
| 80 | Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro |
| 82 | // expand it, the user can have a STDC #define, that should not affect this. |
| 83 | PP.LexUnexpandedToken(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 85 | // Get the handler for this token. If there is no handler, ignore the pragma. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 86 | PragmaHandler *Handler |
| 87 | = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName() |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 88 | : StringRef(), |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 89 | /*IgnoreNull=*/false); |
Chris Lattner | af7cdf4 | 2009-04-19 21:10:26 +0000 | [diff] [blame] | 90 | if (Handler == 0) { |
| 91 | PP.Diag(Tok, diag::warn_pragma_ignored); |
| 92 | return; |
| 93 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 95 | // Otherwise, pass it down. |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 96 | Handler->HandlePragma(PP, Introducer, Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | //===----------------------------------------------------------------------===// |
| 100 | // Preprocessor Pragma Directive Handling. |
| 101 | //===----------------------------------------------------------------------===// |
| 102 | |
| 103 | /// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the |
| 104 | /// rest of the pragma, passing it to the registered pragma handlers. |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 105 | void Preprocessor::HandlePragmaDirective(unsigned Introducer) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | ++NumPragma; |
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 | // Invoke the first level of pragma handlers which reads the namespace id. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 109 | Token Tok; |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 110 | PragmaHandlers->HandlePragma(*this, PragmaIntroducerKind(Introducer), Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 112 | // If the pragma handler didn't read the rest of the line, consume it now. |
Peter Collingbourne | b2eb53d | 2011-02-22 13:49:00 +0000 | [diff] [blame] | 113 | if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective()) |
| 114 | || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 115 | DiscardUntilEndOfDirective(); |
| 116 | } |
| 117 | |
| 118 | /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then |
| 119 | /// return the first token after the directive. The _Pragma token has just |
| 120 | /// been read into 'Tok'. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 121 | void Preprocessor::Handle_Pragma(Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | // Remember the pragma token location. |
| 123 | SourceLocation PragmaLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | // Read the '('. |
| 126 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 127 | if (Tok.isNot(tok::l_paren)) { |
| 128 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 129 | return; |
| 130 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | |
| 132 | // Read the '"..."'. |
| 133 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 134 | if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) { |
| 135 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 136 | // Skip this token, and the ')', if present. |
| 137 | if (Tok.isNot(tok::r_paren)) |
| 138 | Lex(Tok); |
| 139 | if (Tok.is(tok::r_paren)) |
| 140 | Lex(Tok); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | if (Tok.hasUDSuffix()) { |
| 145 | Diag(Tok, diag::err_invalid_string_udl); |
| 146 | // Skip this token, and the ')', if present. |
| 147 | Lex(Tok); |
| 148 | if (Tok.is(tok::r_paren)) |
| 149 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 150 | return; |
| 151 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 153 | // Remember the string. |
| 154 | std::string StrVal = getSpelling(Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 155 | |
| 156 | // Read the ')'. |
| 157 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 158 | if (Tok.isNot(tok::r_paren)) { |
| 159 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 160 | return; |
| 161 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 163 | SourceLocation RParenLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 165 | // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1: |
| 166 | // "The string literal is destringized by deleting the L prefix, if present, |
| 167 | // deleting the leading and trailing double-quotes, replacing each escape |
| 168 | // sequence \" by a double-quote, and replacing each escape sequence \\ by a |
| 169 | // single backslash." |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | if (StrVal[0] == 'L') // Remove L prefix. |
| 171 | StrVal.erase(StrVal.begin()); |
| 172 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 173 | "Invalid string token!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | // Remove the front quote, replacing it with a space, so that the pragma |
| 176 | // contents appear to have a space before them. |
| 177 | StrVal[0] = ' '; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 179 | // Replace the terminating quote with a \n. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 180 | StrVal[StrVal.size()-1] = '\n'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 182 | // Remove escaped quotes and escapes. |
| 183 | for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) { |
| 184 | if (StrVal[i] == '\\' && |
| 185 | (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) { |
| 186 | // \\ -> '\' and \" -> '"'. |
| 187 | StrVal.erase(StrVal.begin()+i); |
| 188 | --e; |
| 189 | } |
| 190 | } |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 191 | |
Peter Collingbourne | a5ef584 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 192 | // Plop the string (including the newline and trailing null) into a buffer |
| 193 | // where we can lex it. |
| 194 | Token TmpTok; |
| 195 | TmpTok.startToken(); |
| 196 | CreateString(&StrVal[0], StrVal.size(), TmpTok); |
| 197 | SourceLocation TokLoc = TmpTok.getLocation(); |
| 198 | |
| 199 | // Make and enter a lexer object so that we lex and expand the tokens just |
| 200 | // like any others. |
| 201 | Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc, |
| 202 | StrVal.size(), *this); |
| 203 | |
| 204 | EnterSourceFileWithLexer(TL, 0); |
| 205 | |
| 206 | // With everything set up, lex this as a #pragma directive. |
| 207 | HandlePragmaDirective(PIK__Pragma); |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 208 | |
| 209 | // Finally, return whatever came after the pragma directive. |
| 210 | return Lex(Tok); |
| 211 | } |
| 212 | |
| 213 | /// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text |
| 214 | /// is not enclosed within a string literal. |
| 215 | void Preprocessor::HandleMicrosoft__pragma(Token &Tok) { |
| 216 | // Remember the pragma token location. |
| 217 | SourceLocation PragmaLoc = Tok.getLocation(); |
| 218 | |
| 219 | // Read the '('. |
| 220 | Lex(Tok); |
| 221 | if (Tok.isNot(tok::l_paren)) { |
| 222 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 223 | return; |
| 224 | } |
| 225 | |
Peter Collingbourne | a5ef584 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 226 | // Get the tokens enclosed within the __pragma(), as well as the final ')'. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 227 | SmallVector<Token, 32> PragmaToks; |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 228 | int NumParens = 0; |
| 229 | Lex(Tok); |
| 230 | while (Tok.isNot(tok::eof)) { |
Peter Collingbourne | a5ef584 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 231 | PragmaToks.push_back(Tok); |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 232 | if (Tok.is(tok::l_paren)) |
| 233 | NumParens++; |
| 234 | else if (Tok.is(tok::r_paren) && NumParens-- == 0) |
| 235 | break; |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 236 | Lex(Tok); |
| 237 | } |
| 238 | |
John McCall | 3da92a9 | 2010-08-29 01:09:54 +0000 | [diff] [blame] | 239 | if (Tok.is(tok::eof)) { |
| 240 | Diag(PragmaLoc, diag::err_unterminated___pragma); |
| 241 | return; |
| 242 | } |
| 243 | |
Peter Collingbourne | a5ef584 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 244 | PragmaToks.front().setFlag(Token::LeadingSpace); |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 245 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 246 | // Replace the ')' with an EOD to mark the end of the pragma. |
| 247 | PragmaToks.back().setKind(tok::eod); |
Peter Collingbourne | a5ef584 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 248 | |
| 249 | Token *TokArray = new Token[PragmaToks.size()]; |
| 250 | std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray); |
| 251 | |
| 252 | // Push the tokens onto the stack. |
| 253 | EnterTokenStream(TokArray, PragmaToks.size(), true, true); |
| 254 | |
| 255 | // With everything set up, lex this as a #pragma directive. |
| 256 | HandlePragmaDirective(PIK___pragma); |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 257 | |
| 258 | // Finally, return whatever came after the pragma directive. |
| 259 | return Lex(Tok); |
| 260 | } |
| 261 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 262 | /// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'. |
| 263 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 264 | void Preprocessor::HandlePragmaOnce(Token &OnceTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 265 | if (isInPrimaryFile()) { |
| 266 | Diag(OnceTok, diag::pp_pragma_once_in_main_file); |
| 267 | return; |
| 268 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 269 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 270 | // 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] | 271 | // Mark the file as a once-only file now. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 272 | HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 275 | void Preprocessor::HandlePragmaMark() { |
Ted Kremenek | 17ff58a | 2008-11-19 22:21:33 +0000 | [diff] [blame] | 276 | assert(CurPPLexer && "No current lexer?"); |
Chris Lattner | 6896a37 | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 277 | if (CurLexer) |
| 278 | CurLexer->ReadToEndOfLine(); |
| 279 | else |
| 280 | CurPTHLexer->DiscardToEndOfLine(); |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 284 | /// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'. |
| 285 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 286 | void Preprocessor::HandlePragmaPoison(Token &PoisonTok) { |
| 287 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 288 | |
| 289 | while (1) { |
| 290 | // Read the next token to poison. While doing this, pretend that we are |
| 291 | // skipping while reading the identifier to poison. |
| 292 | // This avoids errors on code like: |
| 293 | // #pragma GCC poison X |
| 294 | // #pragma GCC poison X |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 295 | if (CurPPLexer) CurPPLexer->LexingRawMode = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 296 | LexUnexpandedToken(Tok); |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 297 | if (CurPPLexer) CurPPLexer->LexingRawMode = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 299 | // If we reached the end of line, we're done. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 300 | if (Tok.is(tok::eod)) 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 | // Can only poison identifiers. |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 303 | if (Tok.isNot(tok::raw_identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 304 | Diag(Tok, diag::err_pp_invalid_poison); |
| 305 | return; |
| 306 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 308 | // Look up the identifier info for the token. We disabled identifier lookup |
| 309 | // by saying we're skipping contents, so we need to do this manually. |
| 310 | IdentifierInfo *II = LookUpIdentifierInfo(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 312 | // Already poisoned. |
| 313 | if (II->isPoisoned()) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 315 | // If this is a macro identifier, emit a warning. |
Chris Lattner | 0edde55 | 2007-10-07 08:04:56 +0000 | [diff] [blame] | 316 | if (II->hasMacroDefinition()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 317 | Diag(Tok, diag::pp_poisoning_existing_macro); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 319 | // Finally, poison it! |
| 320 | II->setIsPoisoned(); |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 321 | if (II->isFromAST()) |
| 322 | II->setChangedSinceDeserialization(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | /// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know |
| 327 | /// that the whole directive has been parsed. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 328 | void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 329 | if (isInPrimaryFile()) { |
| 330 | Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file); |
| 331 | return; |
| 332 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 334 | // 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] | 335 | PreprocessorLexer *TheLexer = getCurrentFileLexer(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 337 | // Mark the file as a system header. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 338 | HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 339 | |
| 340 | |
Chris Lattner | 6896a37 | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 341 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation()); |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 342 | if (PLoc.isInvalid()) |
| 343 | return; |
| 344 | |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 345 | unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
Chris Lattner | 784c257 | 2011-05-22 22:10:16 +0000 | [diff] [blame] | 347 | // Notify the client, if desired, that we are in a new source file. |
| 348 | if (Callbacks) |
| 349 | Callbacks->FileChanged(SysHeaderTok.getLocation(), |
| 350 | PPCallbacks::SystemHeaderPragma, SrcMgr::C_System); |
| 351 | |
Chris Lattner | 6896a37 | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 352 | // Emit a line marker. This will change any source locations from this point |
| 353 | // forward to realize they are in a system header. |
| 354 | // Create a line note with this information. |
| 355 | SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID, |
| 356 | false, false, true, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah. |
| 360 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 361 | void Preprocessor::HandlePragmaDependency(Token &DependencyTok) { |
| 362 | Token FilenameTok; |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 363 | CurPPLexer->LexIncludeFilename(FilenameTok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 364 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 365 | // If the token kind is EOD, the error has already been diagnosed. |
| 366 | if (FilenameTok.is(tok::eod)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 367 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 369 | // Reserve a buffer to get the spelling. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 370 | SmallString<128> FilenameBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 371 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 372 | StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid); |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 373 | if (Invalid) |
| 374 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 376 | bool isAngled = |
| 377 | GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 378 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 379 | // error. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 380 | if (Filename.empty()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 381 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 383 | // Search include directories for this file. |
| 384 | const DirectoryLookup *CurDir; |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 385 | const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir, NULL, NULL, |
| 386 | NULL); |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 387 | if (File == 0) { |
Eli Friedman | f84139a | 2011-08-30 23:07:51 +0000 | [diff] [blame] | 388 | if (!SuppressIncludeNotFoundError) |
| 389 | Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 390 | return; |
| 391 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 393 | const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 394 | |
| 395 | // If this file is older than the file it depends on, emit a diagnostic. |
| 396 | if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) { |
| 397 | // Lex tokens at the end of the message and include them in the message. |
| 398 | std::string Message; |
| 399 | Lex(DependencyTok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 400 | while (DependencyTok.isNot(tok::eod)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 401 | Message += getSpelling(DependencyTok) + " "; |
| 402 | Lex(DependencyTok); |
| 403 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 404 | |
Chris Lattner | 96de259 | 2010-09-05 23:16:09 +0000 | [diff] [blame] | 405 | // Remove the trailing ' ' if present. |
| 406 | if (!Message.empty()) |
| 407 | Message.erase(Message.end()-1); |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 408 | Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 412 | /// HandlePragmaComment - Handle the microsoft #pragma comment extension. The |
| 413 | /// syntax is: |
| 414 | /// #pragma comment(linker, "foo") |
| 415 | /// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user. |
| 416 | /// "foo" is a string, which is fully macro expanded, and permits string |
Gabor Greif | d7ee349 | 2009-03-17 11:39:38 +0000 | [diff] [blame] | 417 | /// concatenation, embedded escape characters etc. See MSDN for more details. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 418 | void Preprocessor::HandlePragmaComment(Token &Tok) { |
| 419 | SourceLocation CommentLoc = Tok.getLocation(); |
| 420 | Lex(Tok); |
| 421 | if (Tok.isNot(tok::l_paren)) { |
| 422 | Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 423 | return; |
| 424 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 426 | // Read the identifier. |
| 427 | Lex(Tok); |
| 428 | if (Tok.isNot(tok::identifier)) { |
| 429 | Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 430 | return; |
| 431 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 433 | // Verify that this is one of the 5 whitelisted options. |
| 434 | // FIXME: warn that 'exestr' is deprecated. |
| 435 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") && |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 437 | !II->isStr("linker") && !II->isStr("user")) { |
| 438 | Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind); |
| 439 | return; |
| 440 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 442 | // Read the optional string if present. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 443 | Lex(Tok); |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 444 | std::string ArgumentString; |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 445 | if (Tok.is(tok::comma)) { |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 446 | Lex(Tok); // eat the comma. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 447 | |
| 448 | // We need at least one string. |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 449 | if (Tok.isNot(tok::string_literal)) { |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 450 | Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | // String concatenation allows multiple strings, which can even come from |
| 455 | // macro expansion. |
| 456 | // "foo " "bar" "Baz" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 457 | SmallVector<Token, 4> StrToks; |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 458 | while (Tok.is(tok::string_literal)) { |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 459 | if (Tok.hasUDSuffix()) |
| 460 | Diag(Tok, diag::err_invalid_string_udl); |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 461 | StrToks.push_back(Tok); |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 462 | Lex(Tok); |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | // Concatenate and parse the strings. |
| 466 | StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 467 | assert(Literal.isAscii() && "Didn't allow wide strings in"); |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 468 | if (Literal.hadError) |
| 469 | return; |
| 470 | if (Literal.Pascal) { |
| 471 | Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed); |
| 472 | return; |
| 473 | } |
| 474 | |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 475 | ArgumentString = Literal.GetString(); |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 476 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 478 | // FIXME: If the kind is "compiler" warn if the string is present (it is |
| 479 | // ignored). |
| 480 | // FIXME: 'lib' requires a comment string. |
| 481 | // FIXME: 'linker' requires a comment string, and has a specific list of |
| 482 | // things that are allowable. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 484 | if (Tok.isNot(tok::r_paren)) { |
| 485 | Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 486 | return; |
| 487 | } |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 488 | Lex(Tok); // eat the r_paren. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 489 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 490 | if (Tok.isNot(tok::eod)) { |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 491 | Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 492 | return; |
| 493 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 494 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 495 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
Chris Lattner | 172e336 | 2009-01-16 19:01:46 +0000 | [diff] [blame] | 496 | if (Callbacks) |
| 497 | Callbacks->PragmaComment(CommentLoc, II, ArgumentString); |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Michael J. Spencer | 301669b | 2010-09-27 06:19:02 +0000 | [diff] [blame] | 500 | /// HandlePragmaMessage - Handle the microsoft and gcc #pragma message |
| 501 | /// extension. The syntax is: |
| 502 | /// #pragma message(string) |
| 503 | /// OR, in GCC mode: |
| 504 | /// #pragma message string |
| 505 | /// string is a string, which is fully macro expanded, and permits string |
| 506 | /// concatenation, embedded escape characters, etc... See MSDN for more details. |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 507 | void Preprocessor::HandlePragmaMessage(Token &Tok) { |
| 508 | SourceLocation MessageLoc = Tok.getLocation(); |
| 509 | Lex(Tok); |
Michael J. Spencer | 301669b | 2010-09-27 06:19:02 +0000 | [diff] [blame] | 510 | bool ExpectClosingParen = false; |
Michael J. Spencer | d83fc54 | 2010-09-27 06:34:47 +0000 | [diff] [blame] | 511 | switch (Tok.getKind()) { |
Michael J. Spencer | 301669b | 2010-09-27 06:19:02 +0000 | [diff] [blame] | 512 | case tok::l_paren: |
| 513 | // We have a MSVC style pragma message. |
| 514 | ExpectClosingParen = true; |
| 515 | // Read the string. |
| 516 | Lex(Tok); |
| 517 | break; |
| 518 | case tok::string_literal: |
| 519 | // We have a GCC style pragma message, and we just read the string. |
| 520 | break; |
| 521 | default: |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 522 | Diag(MessageLoc, diag::err_pragma_message_malformed); |
| 523 | return; |
| 524 | } |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 525 | |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 526 | // We need at least one string. |
| 527 | if (Tok.isNot(tok::string_literal)) { |
| 528 | Diag(Tok.getLocation(), diag::err_pragma_message_malformed); |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | // String concatenation allows multiple strings, which can even come from |
| 533 | // macro expansion. |
| 534 | // "foo " "bar" "Baz" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 535 | SmallVector<Token, 4> StrToks; |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 536 | while (Tok.is(tok::string_literal)) { |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 537 | if (Tok.hasUDSuffix()) |
| 538 | Diag(Tok, diag::err_invalid_string_udl); |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 539 | StrToks.push_back(Tok); |
| 540 | Lex(Tok); |
| 541 | } |
| 542 | |
| 543 | // Concatenate and parse the strings. |
| 544 | StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 545 | assert(Literal.isAscii() && "Didn't allow wide strings in"); |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 546 | if (Literal.hadError) |
| 547 | return; |
| 548 | if (Literal.Pascal) { |
| 549 | Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed); |
| 550 | return; |
| 551 | } |
| 552 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 553 | StringRef MessageString(Literal.GetString()); |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 554 | |
Michael J. Spencer | 301669b | 2010-09-27 06:19:02 +0000 | [diff] [blame] | 555 | if (ExpectClosingParen) { |
| 556 | if (Tok.isNot(tok::r_paren)) { |
| 557 | Diag(Tok.getLocation(), diag::err_pragma_message_malformed); |
| 558 | return; |
| 559 | } |
| 560 | Lex(Tok); // eat the r_paren. |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 561 | } |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 562 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 563 | if (Tok.isNot(tok::eod)) { |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 564 | Diag(Tok.getLocation(), diag::err_pragma_message_malformed); |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | // Output the message. |
| 569 | Diag(MessageLoc, diag::warn_pragma_message) << MessageString; |
| 570 | |
| 571 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
| 572 | if (Callbacks) |
| 573 | Callbacks->PragmaMessage(MessageLoc, MessageString); |
| 574 | } |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 575 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 576 | /// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro. |
| 577 | /// Return the IdentifierInfo* associated with the macro to push or pop. |
| 578 | IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) { |
| 579 | // Remember the pragma token location. |
| 580 | Token PragmaTok = Tok; |
| 581 | |
| 582 | // Read the '('. |
| 583 | Lex(Tok); |
| 584 | if (Tok.isNot(tok::l_paren)) { |
| 585 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 586 | << getSpelling(PragmaTok); |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | // Read the macro name string. |
| 591 | Lex(Tok); |
| 592 | if (Tok.isNot(tok::string_literal)) { |
| 593 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 594 | << getSpelling(PragmaTok); |
| 595 | return 0; |
| 596 | } |
| 597 | |
Richard Smith | 99831e4 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 598 | if (Tok.hasUDSuffix()) { |
| 599 | Diag(Tok, diag::err_invalid_string_udl); |
| 600 | return 0; |
| 601 | } |
| 602 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 603 | // Remember the macro string. |
| 604 | std::string StrVal = getSpelling(Tok); |
| 605 | |
| 606 | // Read the ')'. |
| 607 | Lex(Tok); |
| 608 | if (Tok.isNot(tok::r_paren)) { |
| 609 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 610 | << getSpelling(PragmaTok); |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 615 | "Invalid string token!"); |
| 616 | |
| 617 | // Create a Token from the string. |
| 618 | Token MacroTok; |
| 619 | MacroTok.startToken(); |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 620 | MacroTok.setKind(tok::raw_identifier); |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 621 | CreateString(&StrVal[1], StrVal.size() - 2, MacroTok); |
| 622 | |
| 623 | // Get the IdentifierInfo of MacroToPushTok. |
| 624 | return LookUpIdentifierInfo(MacroTok); |
| 625 | } |
| 626 | |
| 627 | /// HandlePragmaPushMacro - Handle #pragma push_macro. |
| 628 | /// The syntax is: |
| 629 | /// #pragma push_macro("macro") |
| 630 | void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) { |
| 631 | // Parse the pragma directive and get the macro IdentifierInfo*. |
| 632 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok); |
| 633 | if (!IdentInfo) return; |
| 634 | |
| 635 | // Get the MacroInfo associated with IdentInfo. |
| 636 | MacroInfo *MI = getMacroInfo(IdentInfo); |
| 637 | |
| 638 | MacroInfo *MacroCopyToPush = 0; |
| 639 | if (MI) { |
| 640 | // Make a clone of MI. |
| 641 | MacroCopyToPush = CloneMacroInfo(*MI); |
| 642 | |
| 643 | // Allow the original MacroInfo to be redefined later. |
| 644 | MI->setIsAllowRedefinitionsWithoutWarning(true); |
| 645 | } |
| 646 | |
| 647 | // Push the cloned MacroInfo so we can retrieve it later. |
| 648 | PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush); |
| 649 | } |
| 650 | |
Ted Kremenek | b275e3d | 2010-10-19 17:40:50 +0000 | [diff] [blame] | 651 | /// HandlePragmaPopMacro - Handle #pragma pop_macro. |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 652 | /// The syntax is: |
| 653 | /// #pragma pop_macro("macro") |
| 654 | void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) { |
| 655 | SourceLocation MessageLoc = PopMacroTok.getLocation(); |
| 656 | |
| 657 | // Parse the pragma directive and get the macro IdentifierInfo*. |
| 658 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok); |
| 659 | if (!IdentInfo) return; |
| 660 | |
| 661 | // Find the vector<MacroInfo*> associated with the macro. |
| 662 | llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter = |
| 663 | PragmaPushMacroInfo.find(IdentInfo); |
| 664 | if (iter != PragmaPushMacroInfo.end()) { |
| 665 | // Release the MacroInfo currently associated with IdentInfo. |
| 666 | MacroInfo *CurrentMI = getMacroInfo(IdentInfo); |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 667 | if (CurrentMI) { |
| 668 | if (CurrentMI->isWarnIfUnused()) |
| 669 | WarnUnusedMacroLocs.erase(CurrentMI->getDefinitionLoc()); |
| 670 | ReleaseMacroInfo(CurrentMI); |
| 671 | } |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 672 | |
| 673 | // Get the MacroInfo we want to reinstall. |
| 674 | MacroInfo *MacroToReInstall = iter->second.back(); |
| 675 | |
| 676 | // Reinstall the previously pushed macro. |
| 677 | setMacroInfo(IdentInfo, MacroToReInstall); |
| 678 | |
| 679 | // Pop PragmaPushMacroInfo stack. |
| 680 | iter->second.pop_back(); |
| 681 | if (iter->second.size() == 0) |
| 682 | PragmaPushMacroInfo.erase(iter); |
| 683 | } else { |
| 684 | Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push) |
| 685 | << IdentInfo->getName(); |
| 686 | } |
| 687 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 688 | |
Aaron Ballman | 4c55c54 | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 689 | void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) { |
| 690 | // We will either get a quoted filename or a bracketed filename, and we |
| 691 | // have to track which we got. The first filename is the source name, |
| 692 | // and the second name is the mapped filename. If the first is quoted, |
| 693 | // the second must be as well (cannot mix and match quotes and brackets). |
Aaron Ballman | 4c55c54 | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 694 | |
| 695 | // Get the open paren |
| 696 | Lex(Tok); |
| 697 | if (Tok.isNot(tok::l_paren)) { |
| 698 | Diag(Tok, diag::warn_pragma_include_alias_expected) << "("; |
| 699 | return; |
| 700 | } |
| 701 | |
| 702 | // We expect either a quoted string literal, or a bracketed name |
| 703 | Token SourceFilenameTok; |
| 704 | CurPPLexer->LexIncludeFilename(SourceFilenameTok); |
| 705 | if (SourceFilenameTok.is(tok::eod)) { |
| 706 | // The diagnostic has already been handled |
| 707 | return; |
| 708 | } |
| 709 | |
| 710 | StringRef SourceFileName; |
| 711 | SmallString<128> FileNameBuffer; |
| 712 | if (SourceFilenameTok.is(tok::string_literal) || |
| 713 | SourceFilenameTok.is(tok::angle_string_literal)) { |
| 714 | SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer); |
| 715 | } else if (SourceFilenameTok.is(tok::less)) { |
| 716 | // This could be a path instead of just a name |
| 717 | FileNameBuffer.push_back('<'); |
| 718 | SourceLocation End; |
| 719 | if (ConcatenateIncludeName(FileNameBuffer, End)) |
| 720 | return; // Diagnostic already emitted |
| 721 | SourceFileName = FileNameBuffer.str(); |
| 722 | } else { |
| 723 | Diag(Tok, diag::warn_pragma_include_alias_expected_filename); |
| 724 | return; |
| 725 | } |
| 726 | FileNameBuffer.clear(); |
| 727 | |
| 728 | // Now we expect a comma, followed by another include name |
| 729 | Lex(Tok); |
| 730 | if (Tok.isNot(tok::comma)) { |
| 731 | Diag(Tok, diag::warn_pragma_include_alias_expected) << ","; |
| 732 | return; |
| 733 | } |
| 734 | |
| 735 | Token ReplaceFilenameTok; |
| 736 | CurPPLexer->LexIncludeFilename(ReplaceFilenameTok); |
| 737 | if (ReplaceFilenameTok.is(tok::eod)) { |
| 738 | // The diagnostic has already been handled |
| 739 | return; |
| 740 | } |
| 741 | |
| 742 | StringRef ReplaceFileName; |
| 743 | if (ReplaceFilenameTok.is(tok::string_literal) || |
| 744 | ReplaceFilenameTok.is(tok::angle_string_literal)) { |
| 745 | ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer); |
| 746 | } else if (ReplaceFilenameTok.is(tok::less)) { |
| 747 | // This could be a path instead of just a name |
| 748 | FileNameBuffer.push_back('<'); |
| 749 | SourceLocation End; |
| 750 | if (ConcatenateIncludeName(FileNameBuffer, End)) |
| 751 | return; // Diagnostic already emitted |
| 752 | ReplaceFileName = FileNameBuffer.str(); |
| 753 | } else { |
| 754 | Diag(Tok, diag::warn_pragma_include_alias_expected_filename); |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | // Finally, we expect the closing paren |
| 759 | Lex(Tok); |
| 760 | if (Tok.isNot(tok::r_paren)) { |
| 761 | Diag(Tok, diag::warn_pragma_include_alias_expected) << ")"; |
| 762 | return; |
| 763 | } |
| 764 | |
| 765 | // Now that we have the source and target filenames, we need to make sure |
| 766 | // they're both of the same type (angled vs non-angled) |
| 767 | StringRef OriginalSource = SourceFileName; |
| 768 | |
| 769 | bool SourceIsAngled = |
| 770 | GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(), |
| 771 | SourceFileName); |
| 772 | bool ReplaceIsAngled = |
| 773 | GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(), |
| 774 | ReplaceFileName); |
| 775 | if (!SourceFileName.empty() && !ReplaceFileName.empty() && |
| 776 | (SourceIsAngled != ReplaceIsAngled)) { |
| 777 | unsigned int DiagID; |
| 778 | if (SourceIsAngled) |
| 779 | DiagID = diag::warn_pragma_include_alias_mismatch_angle; |
| 780 | else |
| 781 | DiagID = diag::warn_pragma_include_alias_mismatch_quote; |
| 782 | |
| 783 | Diag(SourceFilenameTok.getLocation(), DiagID) |
| 784 | << SourceFileName |
| 785 | << ReplaceFileName; |
| 786 | |
| 787 | return; |
| 788 | } |
| 789 | |
| 790 | // Now we can let the include handler know about this mapping |
| 791 | getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName); |
| 792 | } |
| 793 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 794 | /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. |
| 795 | /// If 'Namespace' is non-null, then it is a token required to exist on the |
| 796 | /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 797 | void Preprocessor::AddPragmaHandler(StringRef Namespace, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 798 | PragmaHandler *Handler) { |
| 799 | PragmaNamespace *InsertNS = PragmaHandlers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 801 | // If this is specified to be in a namespace, step down into it. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 802 | if (!Namespace.empty()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 803 | // If there is already a pragma handler with the name of this namespace, |
| 804 | // we either have an error (directive with the same name as a namespace) or |
| 805 | // we already have the namespace to insert into. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 806 | if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 807 | InsertNS = Existing->getIfNamespace(); |
| 808 | assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma" |
| 809 | " handler with the same name!"); |
| 810 | } else { |
| 811 | // Otherwise, this namespace doesn't exist yet, create and insert the |
| 812 | // handler for it. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 813 | InsertNS = new PragmaNamespace(Namespace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 814 | PragmaHandlers->AddPragma(InsertNS); |
| 815 | } |
| 816 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 817 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 818 | // Check to make sure we don't already have a pragma for this identifier. |
| 819 | assert(!InsertNS->FindHandler(Handler->getName()) && |
| 820 | "Pragma handler already exists for this identifier!"); |
| 821 | InsertNS->AddPragma(Handler); |
| 822 | } |
| 823 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 824 | /// RemovePragmaHandler - Remove the specific pragma handler from the |
| 825 | /// preprocessor. If \arg Namespace is non-null, then it should be the |
| 826 | /// namespace that \arg Handler was added to. It is an error to remove |
| 827 | /// a handler that has not been registered. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 828 | void Preprocessor::RemovePragmaHandler(StringRef Namespace, |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 829 | PragmaHandler *Handler) { |
| 830 | PragmaNamespace *NS = PragmaHandlers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 831 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 832 | // If this is specified to be in a namespace, step down into it. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 833 | if (!Namespace.empty()) { |
| 834 | PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace); |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 835 | assert(Existing && "Namespace containing handler does not exist!"); |
| 836 | |
| 837 | NS = Existing->getIfNamespace(); |
| 838 | assert(NS && "Invalid namespace, registered as a regular pragma handler!"); |
| 839 | } |
| 840 | |
| 841 | NS->RemovePragmaHandler(Handler); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 842 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 843 | // If this is a non-default namespace and it is now empty, remove |
| 844 | // it. |
Argyrios Kyrtzidis | ce52bb3 | 2012-01-06 00:22:09 +0000 | [diff] [blame] | 845 | if (NS != PragmaHandlers && NS->IsEmpty()) { |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 846 | PragmaHandlers->RemovePragmaHandler(NS); |
Argyrios Kyrtzidis | ce52bb3 | 2012-01-06 00:22:09 +0000 | [diff] [blame] | 847 | delete NS; |
| 848 | } |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Peter Collingbourne | 9d3f5f7 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 851 | bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) { |
| 852 | Token Tok; |
| 853 | LexUnexpandedToken(Tok); |
| 854 | |
| 855 | if (Tok.isNot(tok::identifier)) { |
| 856 | Diag(Tok, diag::ext_on_off_switch_syntax); |
| 857 | return true; |
| 858 | } |
| 859 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 860 | if (II->isStr("ON")) |
| 861 | Result = tok::OOS_ON; |
| 862 | else if (II->isStr("OFF")) |
| 863 | Result = tok::OOS_OFF; |
| 864 | else if (II->isStr("DEFAULT")) |
| 865 | Result = tok::OOS_DEFAULT; |
| 866 | else { |
| 867 | Diag(Tok, diag::ext_on_off_switch_syntax); |
| 868 | return true; |
| 869 | } |
| 870 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 871 | // Verify that this is followed by EOD. |
Peter Collingbourne | 9d3f5f7 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 872 | LexUnexpandedToken(Tok); |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 873 | if (Tok.isNot(tok::eod)) |
| 874 | Diag(Tok, diag::ext_pragma_syntax_eod); |
Peter Collingbourne | 9d3f5f7 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 875 | return false; |
| 876 | } |
| 877 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 878 | namespace { |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 879 | /// PragmaOnceHandler - "#pragma once" marks the file as atomically included. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 880 | struct PragmaOnceHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 881 | PragmaOnceHandler() : PragmaHandler("once") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 882 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 883 | Token &OnceTok) { |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 884 | PP.CheckEndOfDirective("pragma once"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 885 | PP.HandlePragmaOnce(OnceTok); |
| 886 | } |
| 887 | }; |
| 888 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 889 | /// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the |
| 890 | /// rest of the line is not lexed. |
| 891 | struct PragmaMarkHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 892 | PragmaMarkHandler() : PragmaHandler("mark") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 893 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 894 | Token &MarkTok) { |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 895 | PP.HandlePragmaMark(); |
| 896 | } |
| 897 | }; |
| 898 | |
| 899 | /// PragmaPoisonHandler - "#pragma poison x" marks x as not usable. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 900 | struct PragmaPoisonHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 901 | PragmaPoisonHandler() : PragmaHandler("poison") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 902 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 903 | Token &PoisonTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 904 | PP.HandlePragmaPoison(PoisonTok); |
| 905 | } |
| 906 | }; |
| 907 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 908 | /// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file |
| 909 | /// as a system header, which silences warnings in it. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 910 | struct PragmaSystemHeaderHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 911 | PragmaSystemHeaderHandler() : PragmaHandler("system_header") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 912 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 913 | Token &SHToken) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 914 | PP.HandlePragmaSystemHeader(SHToken); |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 915 | PP.CheckEndOfDirective("pragma"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 916 | } |
| 917 | }; |
| 918 | struct PragmaDependencyHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 919 | PragmaDependencyHandler() : PragmaHandler("dependency") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 920 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 921 | Token &DepToken) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 922 | PP.HandlePragmaDependency(DepToken); |
| 923 | } |
| 924 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 925 | |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 926 | struct PragmaDebugHandler : public PragmaHandler { |
| 927 | PragmaDebugHandler() : PragmaHandler("__debug") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 928 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 929 | Token &DepToken) { |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 930 | Token Tok; |
| 931 | PP.LexUnexpandedToken(Tok); |
| 932 | if (Tok.isNot(tok::identifier)) { |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 933 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 934 | return; |
| 935 | } |
| 936 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 937 | |
Daniel Dunbar | 5505413 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 938 | if (II->isStr("assert")) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 939 | llvm_unreachable("This is an assertion!"); |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 940 | } else if (II->isStr("crash")) { |
Daniel Dunbar | 5505413 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 941 | *(volatile int*) 0x11 = 0; |
| 942 | } else if (II->isStr("llvm_fatal_error")) { |
| 943 | llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error"); |
| 944 | } else if (II->isStr("llvm_unreachable")) { |
| 945 | llvm_unreachable("#pragma clang __debug llvm_unreachable"); |
| 946 | } else if (II->isStr("overflow_stack")) { |
| 947 | DebugOverflowStack(); |
Daniel Dunbar | ff759a6 | 2010-08-18 23:09:23 +0000 | [diff] [blame] | 948 | } else if (II->isStr("handle_crash")) { |
| 949 | llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent(); |
| 950 | if (CRC) |
| 951 | CRC->HandleCrash(); |
Daniel Dunbar | 5505413 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 952 | } else { |
| 953 | PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command) |
| 954 | << II->getName(); |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | |
Francois Pichet | 1066c6c | 2011-05-25 16:15:03 +0000 | [diff] [blame] | 958 | // Disable MSVC warning about runtime stack overflow. |
| 959 | #ifdef _MSC_VER |
| 960 | #pragma warning(disable : 4717) |
| 961 | #endif |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 962 | void DebugOverflowStack() { |
| 963 | DebugOverflowStack(); |
| 964 | } |
Francois Pichet | 1066c6c | 2011-05-25 16:15:03 +0000 | [diff] [blame] | 965 | #ifdef _MSC_VER |
| 966 | #pragma warning(default : 4717) |
| 967 | #endif |
| 968 | |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 969 | }; |
| 970 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 971 | /// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"' |
| 972 | struct PragmaDiagnosticHandler : public PragmaHandler { |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 973 | private: |
| 974 | const char *Namespace; |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 975 | public: |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 976 | explicit PragmaDiagnosticHandler(const char *NS) : |
| 977 | PragmaHandler("diagnostic"), Namespace(NS) {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 978 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 979 | Token &DiagToken) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 980 | SourceLocation DiagLoc = DiagToken.getLocation(); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 981 | Token Tok; |
| 982 | PP.LexUnexpandedToken(Tok); |
| 983 | if (Tok.isNot(tok::identifier)) { |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 984 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 985 | return; |
| 986 | } |
| 987 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 988 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 990 | diag::Mapping Map; |
| 991 | if (II->isStr("warning")) |
| 992 | Map = diag::MAP_WARNING; |
| 993 | else if (II->isStr("error")) |
| 994 | Map = diag::MAP_ERROR; |
| 995 | else if (II->isStr("ignored")) |
| 996 | Map = diag::MAP_IGNORE; |
| 997 | else if (II->isStr("fatal")) |
| 998 | Map = diag::MAP_FATAL; |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 999 | else if (II->isStr("pop")) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1000 | if (!PP.getDiagnostics().popMappings(DiagLoc)) |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1001 | PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1002 | else if (Callbacks) |
| 1003 | Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace); |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1004 | return; |
| 1005 | } else if (II->isStr("push")) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1006 | PP.getDiagnostics().pushMappings(DiagLoc); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1007 | if (Callbacks) |
| 1008 | Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace); |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 1009 | return; |
| 1010 | } else { |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1011 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1012 | return; |
| 1013 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1015 | PP.LexUnexpandedToken(Tok); |
| 1016 | |
| 1017 | // We need at least one string. |
| 1018 | if (Tok.isNot(tok::string_literal)) { |
| 1019 | PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); |
| 1020 | return; |
| 1021 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1023 | // String concatenation allows multiple strings, which can even come from |
| 1024 | // macro expansion. |
| 1025 | // "foo " "bar" "Baz" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1026 | SmallVector<Token, 4> StrToks; |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1027 | while (Tok.is(tok::string_literal)) { |
| 1028 | StrToks.push_back(Tok); |
| 1029 | PP.LexUnexpandedToken(Tok); |
| 1030 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1031 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1032 | if (Tok.isNot(tok::eod)) { |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1033 | PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); |
| 1034 | return; |
| 1035 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1036 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1037 | // Concatenate and parse the strings. |
| 1038 | StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1039 | assert(Literal.isAscii() && "Didn't allow wide strings in"); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1040 | if (Literal.hadError) |
| 1041 | return; |
| 1042 | if (Literal.Pascal) { |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1043 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1044 | return; |
| 1045 | } |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 1046 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1047 | StringRef WarningName(Literal.GetString()); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1048 | |
| 1049 | if (WarningName.size() < 3 || WarningName[0] != '-' || |
| 1050 | WarningName[1] != 'W') { |
| 1051 | PP.Diag(StrToks[0].getLocation(), |
| 1052 | diag::warn_pragma_diagnostic_invalid_option); |
| 1053 | return; |
| 1054 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 1056 | if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2), |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1057 | Map, DiagLoc)) |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1058 | PP.Diag(StrToks[0].getLocation(), |
| 1059 | diag::warn_pragma_diagnostic_unknown_warning) << WarningName; |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1060 | else if (Callbacks) |
| 1061 | Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1062 | } |
| 1063 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1064 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 1065 | /// PragmaCommentHandler - "#pragma comment ...". |
| 1066 | struct PragmaCommentHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1067 | PragmaCommentHandler() : PragmaHandler("comment") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1068 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1069 | Token &CommentTok) { |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 1070 | PP.HandlePragmaComment(CommentTok); |
| 1071 | } |
| 1072 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | |
Aaron Ballman | 4c55c54 | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 1074 | /// PragmaIncludeAliasHandler - "#pragma include_alias("...")". |
| 1075 | struct PragmaIncludeAliasHandler : public PragmaHandler { |
| 1076 | PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {} |
| 1077 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1078 | Token &IncludeAliasTok) { |
| 1079 | PP.HandlePragmaIncludeAlias(IncludeAliasTok); |
| 1080 | } |
| 1081 | }; |
| 1082 | |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1083 | /// PragmaMessageHandler - "#pragma message("...")". |
| 1084 | struct PragmaMessageHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1085 | PragmaMessageHandler() : PragmaHandler("message") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1086 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1087 | Token &CommentTok) { |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1088 | PP.HandlePragmaMessage(CommentTok); |
| 1089 | } |
| 1090 | }; |
| 1091 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1092 | /// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the |
| 1093 | /// macro on the top of the stack. |
| 1094 | struct PragmaPushMacroHandler : public PragmaHandler { |
| 1095 | PragmaPushMacroHandler() : PragmaHandler("push_macro") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1096 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1097 | Token &PushMacroTok) { |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1098 | PP.HandlePragmaPushMacro(PushMacroTok); |
| 1099 | } |
| 1100 | }; |
| 1101 | |
| 1102 | |
| 1103 | /// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the |
| 1104 | /// macro to the value on the top of the stack. |
| 1105 | struct PragmaPopMacroHandler : public PragmaHandler { |
| 1106 | PragmaPopMacroHandler() : PragmaHandler("pop_macro") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1107 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1108 | Token &PopMacroTok) { |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1109 | PP.HandlePragmaPopMacro(PopMacroTok); |
| 1110 | } |
| 1111 | }; |
| 1112 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1113 | // Pragma STDC implementations. |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 1114 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1115 | /// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...". |
| 1116 | struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1117 | PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1118 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1119 | Token &Tok) { |
Peter Collingbourne | 9d3f5f7 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 1120 | tok::OnOffSwitch OOS; |
| 1121 | if (PP.LexOnOffSwitch(OOS)) |
| 1122 | return; |
| 1123 | if (OOS == tok::OOS_ON) |
Chris Lattner | 4d8aac3 | 2009-04-19 21:55:32 +0000 | [diff] [blame] | 1124 | PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1125 | } |
| 1126 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1127 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1128 | /// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...". |
| 1129 | struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1130 | PragmaSTDC_CX_LIMITED_RANGEHandler() |
| 1131 | : PragmaHandler("CX_LIMITED_RANGE") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1132 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1133 | Token &Tok) { |
Peter Collingbourne | 9d3f5f7 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 1134 | tok::OnOffSwitch OOS; |
| 1135 | PP.LexOnOffSwitch(OOS); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1136 | } |
| 1137 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1138 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1139 | /// PragmaSTDC_UnknownHandler - "#pragma STDC ...". |
| 1140 | struct PragmaSTDC_UnknownHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1141 | PragmaSTDC_UnknownHandler() {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 1142 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1143 | Token &UnknownTok) { |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 1144 | // C99 6.10.6p2, unknown forms are not allowed. |
Chris Lattner | f545be5 | 2009-04-19 21:25:37 +0000 | [diff] [blame] | 1145 | PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1146 | } |
| 1147 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1148 | |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1149 | /// PragmaARCCFCodeAuditedHandler - |
| 1150 | /// #pragma clang arc_cf_code_audited begin/end |
| 1151 | struct PragmaARCCFCodeAuditedHandler : public PragmaHandler { |
| 1152 | PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {} |
| 1153 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 1154 | Token &NameTok) { |
| 1155 | SourceLocation Loc = NameTok.getLocation(); |
| 1156 | bool IsBegin; |
| 1157 | |
| 1158 | Token Tok; |
| 1159 | |
| 1160 | // Lex the 'begin' or 'end'. |
| 1161 | PP.LexUnexpandedToken(Tok); |
| 1162 | const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo(); |
| 1163 | if (BeginEnd && BeginEnd->isStr("begin")) { |
| 1164 | IsBegin = true; |
| 1165 | } else if (BeginEnd && BeginEnd->isStr("end")) { |
| 1166 | IsBegin = false; |
| 1167 | } else { |
| 1168 | PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax); |
| 1169 | return; |
| 1170 | } |
| 1171 | |
| 1172 | // Verify that this is followed by EOD. |
| 1173 | PP.LexUnexpandedToken(Tok); |
| 1174 | if (Tok.isNot(tok::eod)) |
| 1175 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 1176 | |
| 1177 | // The start location of the active audit. |
| 1178 | SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc(); |
| 1179 | |
| 1180 | // The start location we want after processing this. |
| 1181 | SourceLocation NewLoc; |
| 1182 | |
| 1183 | if (IsBegin) { |
| 1184 | // Complain about attempts to re-enter an audit. |
| 1185 | if (BeginLoc.isValid()) { |
| 1186 | PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited); |
| 1187 | PP.Diag(BeginLoc, diag::note_pragma_entered_here); |
| 1188 | } |
| 1189 | NewLoc = Loc; |
| 1190 | } else { |
| 1191 | // Complain about attempts to leave an audit that doesn't exist. |
| 1192 | if (!BeginLoc.isValid()) { |
| 1193 | PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited); |
| 1194 | return; |
| 1195 | } |
| 1196 | NewLoc = SourceLocation(); |
| 1197 | } |
| 1198 | |
| 1199 | PP.setPragmaARCCFCodeAuditedLoc(NewLoc); |
| 1200 | } |
| 1201 | }; |
| 1202 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1203 | } // end anonymous namespace |
| 1204 | |
| 1205 | |
| 1206 | /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas: |
| 1207 | /// #pragma GCC poison/system_header/dependency and #pragma once. |
| 1208 | void Preprocessor::RegisterBuiltinPragmas() { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1209 | AddPragmaHandler(new PragmaOnceHandler()); |
| 1210 | AddPragmaHandler(new PragmaMarkHandler()); |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1211 | AddPragmaHandler(new PragmaPushMacroHandler()); |
| 1212 | AddPragmaHandler(new PragmaPopMacroHandler()); |
Michael J. Spencer | 301669b | 2010-09-27 06:19:02 +0000 | [diff] [blame] | 1213 | AddPragmaHandler(new PragmaMessageHandler()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1214 | |
Chris Lattner | e8fa06e | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1215 | // #pragma GCC ... |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1216 | AddPragmaHandler("GCC", new PragmaPoisonHandler()); |
| 1217 | AddPragmaHandler("GCC", new PragmaSystemHeaderHandler()); |
| 1218 | AddPragmaHandler("GCC", new PragmaDependencyHandler()); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1219 | AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC")); |
Chris Lattner | e8fa06e | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1220 | // #pragma clang ... |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1221 | AddPragmaHandler("clang", new PragmaPoisonHandler()); |
| 1222 | AddPragmaHandler("clang", new PragmaSystemHeaderHandler()); |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1223 | AddPragmaHandler("clang", new PragmaDebugHandler()); |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1224 | AddPragmaHandler("clang", new PragmaDependencyHandler()); |
Douglas Gregor | c09ce12 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1225 | AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang")); |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1226 | AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler()); |
Chris Lattner | e8fa06e | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1227 | |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1228 | AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler()); |
| 1229 | AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler()); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1230 | AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1231 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 1232 | // MS extensions. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1233 | if (LangOpts.MicrosoftExt) { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1234 | AddPragmaHandler(new PragmaCommentHandler()); |
Aaron Ballman | 4c55c54 | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 1235 | AddPragmaHandler(new PragmaIncludeAliasHandler()); |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1236 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1237 | } |