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. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 57 | PragmaHandler *PragmaNamespace::FindHandler(llvm::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; |
| 61 | return IgnoreNull ? 0 : Handlers.lookup(llvm::StringRef()); |
| 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() |
| 88 | : llvm::StringRef(), |
| 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. |
Chris Lattner | 027cff6 | 2009-06-18 05:55:53 +0000 | [diff] [blame] | 113 | if (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 114 | DiscardUntilEndOfDirective(); |
| 115 | } |
| 116 | |
| 117 | /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then |
| 118 | /// return the first token after the directive. The _Pragma token has just |
| 119 | /// been read into 'Tok'. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 120 | void Preprocessor::Handle_Pragma(Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | // Remember the pragma token location. |
| 122 | SourceLocation PragmaLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | // Read the '('. |
| 125 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 126 | if (Tok.isNot(tok::l_paren)) { |
| 127 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 128 | return; |
| 129 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | |
| 131 | // Read the '"..."'. |
| 132 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 133 | if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) { |
| 134 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 135 | return; |
| 136 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 138 | // Remember the string. |
| 139 | std::string StrVal = getSpelling(Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 140 | |
| 141 | // Read the ')'. |
| 142 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 143 | if (Tok.isNot(tok::r_paren)) { |
| 144 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 145 | return; |
| 146 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 148 | SourceLocation RParenLoc = Tok.getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 150 | // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1: |
| 151 | // "The string literal is destringized by deleting the L prefix, if present, |
| 152 | // deleting the leading and trailing double-quotes, replacing each escape |
| 153 | // sequence \" by a double-quote, and replacing each escape sequence \\ by a |
| 154 | // single backslash." |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 155 | if (StrVal[0] == 'L') // Remove L prefix. |
| 156 | StrVal.erase(StrVal.begin()); |
| 157 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 158 | "Invalid string token!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | // Remove the front quote, replacing it with a space, so that the pragma |
| 161 | // contents appear to have a space before them. |
| 162 | StrVal[0] = ' '; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 164 | // Replace the terminating quote with a \n. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 165 | StrVal[StrVal.size()-1] = '\n'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 167 | // Remove escaped quotes and escapes. |
| 168 | for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) { |
| 169 | if (StrVal[i] == '\\' && |
| 170 | (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) { |
| 171 | // \\ -> '\' and \" -> '"'. |
| 172 | StrVal.erase(StrVal.begin()+i); |
| 173 | --e; |
| 174 | } |
| 175 | } |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 176 | |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 177 | Handle_Pragma(PIK__Pragma, StrVal, PragmaLoc, RParenLoc); |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 178 | |
| 179 | // Finally, return whatever came after the pragma directive. |
| 180 | return Lex(Tok); |
| 181 | } |
| 182 | |
| 183 | /// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text |
| 184 | /// is not enclosed within a string literal. |
| 185 | void Preprocessor::HandleMicrosoft__pragma(Token &Tok) { |
| 186 | // Remember the pragma token location. |
| 187 | SourceLocation PragmaLoc = Tok.getLocation(); |
| 188 | |
| 189 | // Read the '('. |
| 190 | Lex(Tok); |
| 191 | if (Tok.isNot(tok::l_paren)) { |
| 192 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | // Get the tokens enclosed within the __pragma(). |
| 197 | llvm::SmallVector<Token, 32> PragmaToks; |
| 198 | int NumParens = 0; |
| 199 | Lex(Tok); |
| 200 | while (Tok.isNot(tok::eof)) { |
| 201 | if (Tok.is(tok::l_paren)) |
| 202 | NumParens++; |
| 203 | else if (Tok.is(tok::r_paren) && NumParens-- == 0) |
| 204 | break; |
| 205 | PragmaToks.push_back(Tok); |
| 206 | Lex(Tok); |
| 207 | } |
| 208 | |
John McCall | 3da92a9 | 2010-08-29 01:09:54 +0000 | [diff] [blame] | 209 | if (Tok.is(tok::eof)) { |
| 210 | Diag(PragmaLoc, diag::err_unterminated___pragma); |
| 211 | return; |
| 212 | } |
| 213 | |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 214 | // Build the pragma string. |
| 215 | std::string StrVal = " "; |
| 216 | for (llvm::SmallVector<Token, 32>::iterator I = |
| 217 | PragmaToks.begin(), E = PragmaToks.end(); I != E; ++I) { |
| 218 | StrVal += getSpelling(*I); |
| 219 | } |
| 220 | |
| 221 | SourceLocation RParenLoc = Tok.getLocation(); |
| 222 | |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 223 | Handle_Pragma(PIK___pragma, StrVal, PragmaLoc, RParenLoc); |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 224 | |
| 225 | // Finally, return whatever came after the pragma directive. |
| 226 | return Lex(Tok); |
| 227 | } |
| 228 | |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 229 | void Preprocessor::Handle_Pragma(unsigned Introducer, |
| 230 | const std::string &StrVal, |
John McCall | 1ef8a2e | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 231 | SourceLocation PragmaLoc, |
| 232 | SourceLocation RParenLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 234 | // Plop the string (including the newline and trailing null) into a buffer |
| 235 | // where we can lex it. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 236 | Token TmpTok; |
| 237 | TmpTok.startToken(); |
| 238 | CreateString(&StrVal[0], StrVal.size(), TmpTok); |
| 239 | SourceLocation TokLoc = TmpTok.getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 240 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 241 | // Make and enter a lexer object so that we lex and expand the tokens just |
| 242 | // like any others. |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 243 | Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc, |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 244 | StrVal.size(), *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 245 | |
| 246 | EnterSourceFileWithLexer(TL, 0); |
| 247 | |
| 248 | // With everything set up, lex this as a #pragma directive. |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 249 | HandlePragmaDirective(Introducer); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | |
| 253 | |
| 254 | /// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'. |
| 255 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 256 | void Preprocessor::HandlePragmaOnce(Token &OnceTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 257 | if (isInPrimaryFile()) { |
| 258 | Diag(OnceTok, diag::pp_pragma_once_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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 263 | // Mark the file as a once-only file now. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 264 | HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 267 | void Preprocessor::HandlePragmaMark() { |
Ted Kremenek | 17ff58a | 2008-11-19 22:21:33 +0000 | [diff] [blame] | 268 | assert(CurPPLexer && "No current lexer?"); |
Chris Lattner | 6896a37 | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 269 | if (CurLexer) |
| 270 | CurLexer->ReadToEndOfLine(); |
| 271 | else |
| 272 | CurPTHLexer->DiscardToEndOfLine(); |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 276 | /// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'. |
| 277 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 278 | void Preprocessor::HandlePragmaPoison(Token &PoisonTok) { |
| 279 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | |
| 281 | while (1) { |
| 282 | // Read the next token to poison. While doing this, pretend that we are |
| 283 | // skipping while reading the identifier to poison. |
| 284 | // This avoids errors on code like: |
| 285 | // #pragma GCC poison X |
| 286 | // #pragma GCC poison X |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 287 | if (CurPPLexer) CurPPLexer->LexingRawMode = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 288 | LexUnexpandedToken(Tok); |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 289 | if (CurPPLexer) CurPPLexer->LexingRawMode = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 291 | // If we reached the end of line, we're done. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 292 | if (Tok.is(tok::eom)) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 294 | // Can only poison identifiers. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 295 | if (Tok.isNot(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 296 | Diag(Tok, diag::err_pp_invalid_poison); |
| 297 | return; |
| 298 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 300 | // Look up the identifier info for the token. We disabled identifier lookup |
| 301 | // by saying we're skipping contents, so we need to do this manually. |
| 302 | IdentifierInfo *II = LookUpIdentifierInfo(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 304 | // Already poisoned. |
| 305 | if (II->isPoisoned()) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 307 | // If this is a macro identifier, emit a warning. |
Chris Lattner | 0edde55 | 2007-10-07 08:04:56 +0000 | [diff] [blame] | 308 | if (II->hasMacroDefinition()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 309 | Diag(Tok, diag::pp_poisoning_existing_macro); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 310 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 311 | // Finally, poison it! |
| 312 | II->setIsPoisoned(); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know |
| 317 | /// that the whole directive has been parsed. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 318 | void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 319 | if (isInPrimaryFile()) { |
| 320 | Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file); |
| 321 | return; |
| 322 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 324 | // 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] | 325 | PreprocessorLexer *TheLexer = getCurrentFileLexer(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 327 | // Mark the file as a system header. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 328 | HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | |
| 330 | |
Chris Lattner | 6896a37 | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 331 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation()); |
| 332 | unsigned FilenameLen = strlen(PLoc.getFilename()); |
| 333 | unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename(), |
| 334 | FilenameLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | |
Chris Lattner | 6896a37 | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 336 | // Emit a line marker. This will change any source locations from this point |
| 337 | // forward to realize they are in a system header. |
| 338 | // Create a line note with this information. |
| 339 | SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine(), FilenameID, |
| 340 | false, false, true, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 342 | // Notify the client, if desired, that we are in a new source file. |
| 343 | if (Callbacks) |
Ted Kremenek | 35c10c2 | 2008-11-20 01:45:11 +0000 | [diff] [blame] | 344 | Callbacks->FileChanged(SysHeaderTok.getLocation(), |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 345 | PPCallbacks::SystemHeaderPragma, SrcMgr::C_System); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah. |
| 349 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 350 | void Preprocessor::HandlePragmaDependency(Token &DependencyTok) { |
| 351 | Token FilenameTok; |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 352 | CurPPLexer->LexIncludeFilename(FilenameTok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 353 | |
| 354 | // If the token kind is EOM, the error has already been diagnosed. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 355 | if (FilenameTok.is(tok::eom)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 356 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 358 | // Reserve a buffer to get the spelling. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 359 | llvm::SmallString<128> FilenameBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 360 | bool Invalid = false; |
| 361 | llvm::StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid); |
| 362 | if (Invalid) |
| 363 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 364 | |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 365 | bool isAngled = |
| 366 | GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 367 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 368 | // error. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 369 | if (Filename.empty()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 370 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 371 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 372 | // Search include directories for this file. |
| 373 | const DirectoryLookup *CurDir; |
Chris Lattner | f45b646 | 2010-01-22 00:14:44 +0000 | [diff] [blame] | 374 | const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir); |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 375 | if (File == 0) { |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 376 | Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 377 | return; |
| 378 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 380 | const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 381 | |
| 382 | // If this file is older than the file it depends on, emit a diagnostic. |
| 383 | if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) { |
| 384 | // Lex tokens at the end of the message and include them in the message. |
| 385 | std::string Message; |
| 386 | Lex(DependencyTok); |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 387 | while (DependencyTok.isNot(tok::eom)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 388 | Message += getSpelling(DependencyTok) + " "; |
| 389 | Lex(DependencyTok); |
| 390 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | |
Chris Lattner | 96de259 | 2010-09-05 23:16:09 +0000 | [diff] [blame] | 392 | // Remove the trailing ' ' if present. |
| 393 | if (!Message.empty()) |
| 394 | Message.erase(Message.end()-1); |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 395 | Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 399 | /// HandlePragmaComment - Handle the microsoft #pragma comment extension. The |
| 400 | /// syntax is: |
| 401 | /// #pragma comment(linker, "foo") |
| 402 | /// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user. |
| 403 | /// "foo" is a string, which is fully macro expanded, and permits string |
Gabor Greif | d7ee349 | 2009-03-17 11:39:38 +0000 | [diff] [blame] | 404 | /// concatenation, embedded escape characters etc. See MSDN for more details. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 405 | void Preprocessor::HandlePragmaComment(Token &Tok) { |
| 406 | SourceLocation CommentLoc = Tok.getLocation(); |
| 407 | Lex(Tok); |
| 408 | if (Tok.isNot(tok::l_paren)) { |
| 409 | Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 410 | return; |
| 411 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 413 | // Read the identifier. |
| 414 | Lex(Tok); |
| 415 | if (Tok.isNot(tok::identifier)) { |
| 416 | Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 417 | return; |
| 418 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 420 | // Verify that this is one of the 5 whitelisted options. |
| 421 | // FIXME: warn that 'exestr' is deprecated. |
| 422 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") && |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 424 | !II->isStr("linker") && !II->isStr("user")) { |
| 425 | Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind); |
| 426 | return; |
| 427 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 429 | // Read the optional string if present. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 430 | Lex(Tok); |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 431 | std::string ArgumentString; |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 432 | if (Tok.is(tok::comma)) { |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 433 | Lex(Tok); // eat the comma. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 434 | |
| 435 | // We need at least one string. |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 436 | if (Tok.isNot(tok::string_literal)) { |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 437 | Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | // String concatenation allows multiple strings, which can even come from |
| 442 | // macro expansion. |
| 443 | // "foo " "bar" "Baz" |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 444 | llvm::SmallVector<Token, 4> StrToks; |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 445 | while (Tok.is(tok::string_literal)) { |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 446 | StrToks.push_back(Tok); |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 447 | Lex(Tok); |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | // Concatenate and parse the strings. |
| 451 | StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this); |
| 452 | assert(!Literal.AnyWide && "Didn't allow wide strings in"); |
| 453 | if (Literal.hadError) |
| 454 | return; |
| 455 | if (Literal.Pascal) { |
| 456 | Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed); |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | ArgumentString = std::string(Literal.GetString(), |
| 461 | Literal.GetString()+Literal.GetStringLength()); |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 462 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 464 | // FIXME: If the kind is "compiler" warn if the string is present (it is |
| 465 | // ignored). |
| 466 | // FIXME: 'lib' requires a comment string. |
| 467 | // FIXME: 'linker' requires a comment string, and has a specific list of |
| 468 | // things that are allowable. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 470 | if (Tok.isNot(tok::r_paren)) { |
| 471 | Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 472 | return; |
| 473 | } |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 474 | Lex(Tok); // eat the r_paren. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 475 | |
| 476 | if (Tok.isNot(tok::eom)) { |
| 477 | Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 478 | return; |
| 479 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 481 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
Chris Lattner | 172e336 | 2009-01-16 19:01:46 +0000 | [diff] [blame] | 482 | if (Callbacks) |
| 483 | Callbacks->PragmaComment(CommentLoc, II, ArgumentString); |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Michael J. Spencer | 301669b | 2010-09-27 06:19:02 +0000 | [diff] [blame] | 486 | /// HandlePragmaMessage - Handle the microsoft and gcc #pragma message |
| 487 | /// extension. The syntax is: |
| 488 | /// #pragma message(string) |
| 489 | /// OR, in GCC mode: |
| 490 | /// #pragma message string |
| 491 | /// string is a string, which is fully macro expanded, and permits string |
| 492 | /// concatenation, embedded escape characters, etc... See MSDN for more details. |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 493 | void Preprocessor::HandlePragmaMessage(Token &Tok) { |
| 494 | SourceLocation MessageLoc = Tok.getLocation(); |
| 495 | Lex(Tok); |
Michael J. Spencer | 301669b | 2010-09-27 06:19:02 +0000 | [diff] [blame] | 496 | bool ExpectClosingParen = false; |
Michael J. Spencer | d83fc54 | 2010-09-27 06:34:47 +0000 | [diff] [blame] | 497 | switch (Tok.getKind()) { |
Michael J. Spencer | 301669b | 2010-09-27 06:19:02 +0000 | [diff] [blame] | 498 | case tok::l_paren: |
| 499 | // We have a MSVC style pragma message. |
| 500 | ExpectClosingParen = true; |
| 501 | // Read the string. |
| 502 | Lex(Tok); |
| 503 | break; |
| 504 | case tok::string_literal: |
| 505 | // We have a GCC style pragma message, and we just read the string. |
| 506 | break; |
| 507 | default: |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 508 | Diag(MessageLoc, diag::err_pragma_message_malformed); |
| 509 | return; |
| 510 | } |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 511 | |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 512 | // We need at least one string. |
| 513 | if (Tok.isNot(tok::string_literal)) { |
| 514 | Diag(Tok.getLocation(), diag::err_pragma_message_malformed); |
| 515 | return; |
| 516 | } |
| 517 | |
| 518 | // String concatenation allows multiple strings, which can even come from |
| 519 | // macro expansion. |
| 520 | // "foo " "bar" "Baz" |
| 521 | llvm::SmallVector<Token, 4> StrToks; |
| 522 | while (Tok.is(tok::string_literal)) { |
| 523 | StrToks.push_back(Tok); |
| 524 | Lex(Tok); |
| 525 | } |
| 526 | |
| 527 | // Concatenate and parse the strings. |
| 528 | StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this); |
| 529 | assert(!Literal.AnyWide && "Didn't allow wide strings in"); |
| 530 | if (Literal.hadError) |
| 531 | return; |
| 532 | if (Literal.Pascal) { |
| 533 | Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed); |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | llvm::StringRef MessageString(Literal.GetString(), Literal.GetStringLength()); |
| 538 | |
Michael J. Spencer | 301669b | 2010-09-27 06:19:02 +0000 | [diff] [blame] | 539 | if (ExpectClosingParen) { |
| 540 | if (Tok.isNot(tok::r_paren)) { |
| 541 | Diag(Tok.getLocation(), diag::err_pragma_message_malformed); |
| 542 | return; |
| 543 | } |
| 544 | Lex(Tok); // eat the r_paren. |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 545 | } |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 546 | |
| 547 | if (Tok.isNot(tok::eom)) { |
| 548 | Diag(Tok.getLocation(), diag::err_pragma_message_malformed); |
| 549 | return; |
| 550 | } |
| 551 | |
| 552 | // Output the message. |
| 553 | Diag(MessageLoc, diag::warn_pragma_message) << MessageString; |
| 554 | |
| 555 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
| 556 | if (Callbacks) |
| 557 | Callbacks->PragmaMessage(MessageLoc, MessageString); |
| 558 | } |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 559 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 560 | /// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro. |
| 561 | /// Return the IdentifierInfo* associated with the macro to push or pop. |
| 562 | IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) { |
| 563 | // Remember the pragma token location. |
| 564 | Token PragmaTok = Tok; |
| 565 | |
| 566 | // Read the '('. |
| 567 | Lex(Tok); |
| 568 | if (Tok.isNot(tok::l_paren)) { |
| 569 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 570 | << getSpelling(PragmaTok); |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | // Read the macro name string. |
| 575 | Lex(Tok); |
| 576 | if (Tok.isNot(tok::string_literal)) { |
| 577 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 578 | << getSpelling(PragmaTok); |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | // Remember the macro string. |
| 583 | std::string StrVal = getSpelling(Tok); |
| 584 | |
| 585 | // Read the ')'. |
| 586 | Lex(Tok); |
| 587 | if (Tok.isNot(tok::r_paren)) { |
| 588 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 589 | << getSpelling(PragmaTok); |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 594 | "Invalid string token!"); |
| 595 | |
| 596 | // Create a Token from the string. |
| 597 | Token MacroTok; |
| 598 | MacroTok.startToken(); |
| 599 | MacroTok.setKind(tok::identifier); |
| 600 | CreateString(&StrVal[1], StrVal.size() - 2, MacroTok); |
| 601 | |
| 602 | // Get the IdentifierInfo of MacroToPushTok. |
| 603 | return LookUpIdentifierInfo(MacroTok); |
| 604 | } |
| 605 | |
| 606 | /// HandlePragmaPushMacro - Handle #pragma push_macro. |
| 607 | /// The syntax is: |
| 608 | /// #pragma push_macro("macro") |
| 609 | void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) { |
| 610 | // Parse the pragma directive and get the macro IdentifierInfo*. |
| 611 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok); |
| 612 | if (!IdentInfo) return; |
| 613 | |
| 614 | // Get the MacroInfo associated with IdentInfo. |
| 615 | MacroInfo *MI = getMacroInfo(IdentInfo); |
| 616 | |
| 617 | MacroInfo *MacroCopyToPush = 0; |
| 618 | if (MI) { |
| 619 | // Make a clone of MI. |
| 620 | MacroCopyToPush = CloneMacroInfo(*MI); |
| 621 | |
| 622 | // Allow the original MacroInfo to be redefined later. |
| 623 | MI->setIsAllowRedefinitionsWithoutWarning(true); |
| 624 | } |
| 625 | |
| 626 | // Push the cloned MacroInfo so we can retrieve it later. |
| 627 | PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush); |
| 628 | } |
| 629 | |
Ted Kremenek | b275e3d | 2010-10-19 17:40:50 +0000 | [diff] [blame] | 630 | /// HandlePragmaPopMacro - Handle #pragma pop_macro. |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 631 | /// The syntax is: |
| 632 | /// #pragma pop_macro("macro") |
| 633 | void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) { |
| 634 | SourceLocation MessageLoc = PopMacroTok.getLocation(); |
| 635 | |
| 636 | // Parse the pragma directive and get the macro IdentifierInfo*. |
| 637 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok); |
| 638 | if (!IdentInfo) return; |
| 639 | |
| 640 | // Find the vector<MacroInfo*> associated with the macro. |
| 641 | llvm::DenseMap<IdentifierInfo*, std::vector<MacroInfo*> >::iterator iter = |
| 642 | PragmaPushMacroInfo.find(IdentInfo); |
| 643 | if (iter != PragmaPushMacroInfo.end()) { |
| 644 | // Release the MacroInfo currently associated with IdentInfo. |
| 645 | MacroInfo *CurrentMI = getMacroInfo(IdentInfo); |
| 646 | if (CurrentMI) ReleaseMacroInfo(CurrentMI); |
| 647 | |
| 648 | // Get the MacroInfo we want to reinstall. |
| 649 | MacroInfo *MacroToReInstall = iter->second.back(); |
| 650 | |
| 651 | // Reinstall the previously pushed macro. |
| 652 | setMacroInfo(IdentInfo, MacroToReInstall); |
| 653 | |
| 654 | // Pop PragmaPushMacroInfo stack. |
| 655 | iter->second.pop_back(); |
| 656 | if (iter->second.size() == 0) |
| 657 | PragmaPushMacroInfo.erase(iter); |
| 658 | } else { |
| 659 | Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push) |
| 660 | << IdentInfo->getName(); |
| 661 | } |
| 662 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 663 | |
| 664 | /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. |
| 665 | /// If 'Namespace' is non-null, then it is a token required to exist on the |
| 666 | /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 667 | void Preprocessor::AddPragmaHandler(llvm::StringRef Namespace, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 668 | PragmaHandler *Handler) { |
| 669 | PragmaNamespace *InsertNS = PragmaHandlers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 670 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 671 | // 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] | 672 | if (!Namespace.empty()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 673 | // If there is already a pragma handler with the name of this namespace, |
| 674 | // we either have an error (directive with the same name as a namespace) or |
| 675 | // we already have the namespace to insert into. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 676 | if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 677 | InsertNS = Existing->getIfNamespace(); |
| 678 | assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma" |
| 679 | " handler with the same name!"); |
| 680 | } else { |
| 681 | // Otherwise, this namespace doesn't exist yet, create and insert the |
| 682 | // handler for it. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 683 | InsertNS = new PragmaNamespace(Namespace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 684 | PragmaHandlers->AddPragma(InsertNS); |
| 685 | } |
| 686 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 687 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 688 | // Check to make sure we don't already have a pragma for this identifier. |
| 689 | assert(!InsertNS->FindHandler(Handler->getName()) && |
| 690 | "Pragma handler already exists for this identifier!"); |
| 691 | InsertNS->AddPragma(Handler); |
| 692 | } |
| 693 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 694 | /// RemovePragmaHandler - Remove the specific pragma handler from the |
| 695 | /// preprocessor. If \arg Namespace is non-null, then it should be the |
| 696 | /// namespace that \arg Handler was added to. It is an error to remove |
| 697 | /// a handler that has not been registered. |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 698 | void Preprocessor::RemovePragmaHandler(llvm::StringRef Namespace, |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 699 | PragmaHandler *Handler) { |
| 700 | PragmaNamespace *NS = PragmaHandlers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 701 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 702 | // 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] | 703 | if (!Namespace.empty()) { |
| 704 | PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace); |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 705 | assert(Existing && "Namespace containing handler does not exist!"); |
| 706 | |
| 707 | NS = Existing->getIfNamespace(); |
| 708 | assert(NS && "Invalid namespace, registered as a regular pragma handler!"); |
| 709 | } |
| 710 | |
| 711 | NS->RemovePragmaHandler(Handler); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 713 | // If this is a non-default namespace and it is now empty, remove |
| 714 | // it. |
| 715 | if (NS != PragmaHandlers && NS->IsEmpty()) |
| 716 | PragmaHandlers->RemovePragmaHandler(NS); |
| 717 | } |
| 718 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 719 | namespace { |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 720 | /// PragmaOnceHandler - "#pragma once" marks the file as atomically included. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 721 | struct PragmaOnceHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 722 | PragmaOnceHandler() : PragmaHandler("once") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 723 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 724 | Token &OnceTok) { |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 725 | PP.CheckEndOfDirective("pragma once"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 726 | PP.HandlePragmaOnce(OnceTok); |
| 727 | } |
| 728 | }; |
| 729 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 730 | /// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the |
| 731 | /// rest of the line is not lexed. |
| 732 | struct PragmaMarkHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 733 | PragmaMarkHandler() : PragmaHandler("mark") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 734 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 735 | Token &MarkTok) { |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 736 | PP.HandlePragmaMark(); |
| 737 | } |
| 738 | }; |
| 739 | |
| 740 | /// PragmaPoisonHandler - "#pragma poison x" marks x as not usable. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 741 | struct PragmaPoisonHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 742 | PragmaPoisonHandler() : PragmaHandler("poison") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 743 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 744 | Token &PoisonTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 745 | PP.HandlePragmaPoison(PoisonTok); |
| 746 | } |
| 747 | }; |
| 748 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 749 | /// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file |
| 750 | /// as a system header, which silences warnings in it. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 751 | struct PragmaSystemHeaderHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 752 | PragmaSystemHeaderHandler() : PragmaHandler("system_header") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 753 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 754 | Token &SHToken) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 755 | PP.HandlePragmaSystemHeader(SHToken); |
Chris Lattner | 35410d5 | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 756 | PP.CheckEndOfDirective("pragma"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 757 | } |
| 758 | }; |
| 759 | struct PragmaDependencyHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 760 | PragmaDependencyHandler() : PragmaHandler("dependency") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 761 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 762 | Token &DepToken) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 763 | PP.HandlePragmaDependency(DepToken); |
| 764 | } |
| 765 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 767 | struct PragmaDebugHandler : public PragmaHandler { |
| 768 | PragmaDebugHandler() : PragmaHandler("__debug") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 769 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 770 | Token &DepToken) { |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 771 | Token Tok; |
| 772 | PP.LexUnexpandedToken(Tok); |
| 773 | if (Tok.isNot(tok::identifier)) { |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 774 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 775 | return; |
| 776 | } |
| 777 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 778 | |
Daniel Dunbar | 5505413 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 779 | if (II->isStr("assert")) { |
| 780 | assert(0 && "This is an assertion!"); |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 781 | } else if (II->isStr("crash")) { |
Daniel Dunbar | 5505413 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 782 | *(volatile int*) 0x11 = 0; |
| 783 | } else if (II->isStr("llvm_fatal_error")) { |
| 784 | llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error"); |
| 785 | } else if (II->isStr("llvm_unreachable")) { |
| 786 | llvm_unreachable("#pragma clang __debug llvm_unreachable"); |
| 787 | } else if (II->isStr("overflow_stack")) { |
| 788 | DebugOverflowStack(); |
Daniel Dunbar | ff759a6 | 2010-08-18 23:09:23 +0000 | [diff] [blame] | 789 | } else if (II->isStr("handle_crash")) { |
| 790 | llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent(); |
| 791 | if (CRC) |
| 792 | CRC->HandleCrash(); |
Daniel Dunbar | 5505413 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 793 | } else { |
| 794 | PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command) |
| 795 | << II->getName(); |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
| 799 | void DebugOverflowStack() { |
| 800 | DebugOverflowStack(); |
| 801 | } |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 802 | }; |
| 803 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 804 | /// PragmaDiagnosticHandler - e.g. '#pragma GCC diagnostic ignored "-Wformat"' |
| 805 | struct PragmaDiagnosticHandler : public PragmaHandler { |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 806 | public: |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 807 | explicit PragmaDiagnosticHandler() : PragmaHandler("diagnostic") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 808 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 809 | Token &DiagToken) { |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 810 | Token Tok; |
| 811 | PP.LexUnexpandedToken(Tok); |
| 812 | if (Tok.isNot(tok::identifier)) { |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 813 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 814 | return; |
| 815 | } |
| 816 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 817 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 818 | diag::Mapping Map; |
| 819 | if (II->isStr("warning")) |
| 820 | Map = diag::MAP_WARNING; |
| 821 | else if (II->isStr("error")) |
| 822 | Map = diag::MAP_ERROR; |
| 823 | else if (II->isStr("ignored")) |
| 824 | Map = diag::MAP_IGNORE; |
| 825 | else if (II->isStr("fatal")) |
| 826 | Map = diag::MAP_FATAL; |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 827 | else if (II->isStr("pop")) { |
| 828 | if (!PP.getDiagnostics().popMappings()) |
| 829 | PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop); |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 830 | |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 831 | return; |
| 832 | } else if (II->isStr("push")) { |
| 833 | PP.getDiagnostics().pushMappings(); |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 834 | return; |
| 835 | } else { |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 836 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 837 | return; |
| 838 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 839 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 840 | PP.LexUnexpandedToken(Tok); |
| 841 | |
| 842 | // We need at least one string. |
| 843 | if (Tok.isNot(tok::string_literal)) { |
| 844 | PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); |
| 845 | return; |
| 846 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 848 | // String concatenation allows multiple strings, which can even come from |
| 849 | // macro expansion. |
| 850 | // "foo " "bar" "Baz" |
| 851 | llvm::SmallVector<Token, 4> StrToks; |
| 852 | while (Tok.is(tok::string_literal)) { |
| 853 | StrToks.push_back(Tok); |
| 854 | PP.LexUnexpandedToken(Tok); |
| 855 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 856 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 857 | if (Tok.isNot(tok::eom)) { |
| 858 | PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); |
| 859 | return; |
| 860 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 862 | // Concatenate and parse the strings. |
| 863 | StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP); |
| 864 | assert(!Literal.AnyWide && "Didn't allow wide strings in"); |
| 865 | if (Literal.hadError) |
| 866 | return; |
| 867 | if (Literal.Pascal) { |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 868 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 869 | return; |
| 870 | } |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 871 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 872 | std::string WarningName(Literal.GetString(), |
| 873 | Literal.GetString()+Literal.GetStringLength()); |
| 874 | |
| 875 | if (WarningName.size() < 3 || WarningName[0] != '-' || |
| 876 | WarningName[1] != 'W') { |
| 877 | PP.Diag(StrToks[0].getLocation(), |
| 878 | diag::warn_pragma_diagnostic_invalid_option); |
| 879 | return; |
| 880 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 881 | |
Chris Lattner | edaf877 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 882 | if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2, |
| 883 | Map)) |
| 884 | PP.Diag(StrToks[0].getLocation(), |
| 885 | diag::warn_pragma_diagnostic_unknown_warning) << WarningName; |
| 886 | } |
| 887 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 889 | /// PragmaCommentHandler - "#pragma comment ...". |
| 890 | struct PragmaCommentHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 891 | PragmaCommentHandler() : PragmaHandler("comment") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 892 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 893 | Token &CommentTok) { |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 894 | PP.HandlePragmaComment(CommentTok); |
| 895 | } |
| 896 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 898 | /// PragmaMessageHandler - "#pragma message("...")". |
| 899 | struct PragmaMessageHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 900 | PragmaMessageHandler() : PragmaHandler("message") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 901 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 902 | Token &CommentTok) { |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 903 | PP.HandlePragmaMessage(CommentTok); |
| 904 | } |
| 905 | }; |
| 906 | |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 907 | /// PragmaPushMacroHandler - "#pragma push_macro" saves the value of the |
| 908 | /// macro on the top of the stack. |
| 909 | struct PragmaPushMacroHandler : public PragmaHandler { |
| 910 | PragmaPushMacroHandler() : PragmaHandler("push_macro") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 911 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 912 | Token &PushMacroTok) { |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 913 | PP.HandlePragmaPushMacro(PushMacroTok); |
| 914 | } |
| 915 | }; |
| 916 | |
| 917 | |
| 918 | /// PragmaPopMacroHandler - "#pragma pop_macro" sets the value of the |
| 919 | /// macro to the value on the top of the stack. |
| 920 | struct PragmaPopMacroHandler : public PragmaHandler { |
| 921 | PragmaPopMacroHandler() : PragmaHandler("pop_macro") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 922 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 923 | Token &PopMacroTok) { |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 924 | PP.HandlePragmaPopMacro(PopMacroTok); |
| 925 | } |
| 926 | }; |
| 927 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 928 | // Pragma STDC implementations. |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 929 | |
| 930 | enum STDCSetting { |
| 931 | STDC_ON, STDC_OFF, STDC_DEFAULT, STDC_INVALID |
| 932 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 933 | |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 934 | static STDCSetting LexOnOffSwitch(Preprocessor &PP) { |
| 935 | Token Tok; |
| 936 | PP.LexUnexpandedToken(Tok); |
| 937 | |
| 938 | if (Tok.isNot(tok::identifier)) { |
| 939 | PP.Diag(Tok, diag::ext_stdc_pragma_syntax); |
| 940 | return STDC_INVALID; |
| 941 | } |
| 942 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 943 | STDCSetting Result; |
| 944 | if (II->isStr("ON")) |
| 945 | Result = STDC_ON; |
| 946 | else if (II->isStr("OFF")) |
| 947 | Result = STDC_OFF; |
| 948 | else if (II->isStr("DEFAULT")) |
| 949 | Result = STDC_DEFAULT; |
| 950 | else { |
| 951 | PP.Diag(Tok, diag::ext_stdc_pragma_syntax); |
| 952 | return STDC_INVALID; |
| 953 | } |
| 954 | |
| 955 | // Verify that this is followed by EOM. |
| 956 | PP.LexUnexpandedToken(Tok); |
| 957 | if (Tok.isNot(tok::eom)) |
| 958 | PP.Diag(Tok, diag::ext_stdc_pragma_syntax_eom); |
| 959 | return Result; |
| 960 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 962 | /// PragmaSTDC_FP_CONTRACTHandler - "#pragma STDC FP_CONTRACT ...". |
| 963 | struct PragmaSTDC_FP_CONTRACTHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 964 | PragmaSTDC_FP_CONTRACTHandler() : PragmaHandler("FP_CONTRACT") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 965 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 966 | Token &Tok) { |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 967 | // We just ignore the setting of FP_CONTRACT. Since we don't do contractions |
| 968 | // at all, our default is OFF and setting it to ON is an optimization hint |
| 969 | // we can safely ignore. When we support -ffma or something, we would need |
| 970 | // to diagnose that we are ignoring FMA. |
| 971 | LexOnOffSwitch(PP); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 972 | } |
| 973 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 974 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 975 | /// PragmaSTDC_FENV_ACCESSHandler - "#pragma STDC FENV_ACCESS ...". |
| 976 | struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 977 | PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 978 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 979 | Token &Tok) { |
Chris Lattner | 4d8aac3 | 2009-04-19 21:55:32 +0000 | [diff] [blame] | 980 | if (LexOnOffSwitch(PP) == STDC_ON) |
| 981 | PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 982 | } |
| 983 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 985 | /// PragmaSTDC_CX_LIMITED_RANGEHandler - "#pragma STDC CX_LIMITED_RANGE ...". |
| 986 | struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 987 | PragmaSTDC_CX_LIMITED_RANGEHandler() |
| 988 | : PragmaHandler("CX_LIMITED_RANGE") {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 989 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 990 | Token &Tok) { |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 991 | LexOnOffSwitch(PP); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 992 | } |
| 993 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 994 | |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 995 | /// PragmaSTDC_UnknownHandler - "#pragma STDC ...". |
| 996 | struct PragmaSTDC_UnknownHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 997 | PragmaSTDC_UnknownHandler() {} |
Douglas Gregor | 80c60f7 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 998 | virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, |
| 999 | Token &UnknownTok) { |
Chris Lattner | 6c5cf4a | 2009-04-19 21:50:08 +0000 | [diff] [blame] | 1000 | // C99 6.10.6p2, unknown forms are not allowed. |
Chris Lattner | f545be5 | 2009-04-19 21:25:37 +0000 | [diff] [blame] | 1001 | PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1002 | } |
| 1003 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1005 | } // end anonymous namespace |
| 1006 | |
| 1007 | |
| 1008 | /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas: |
| 1009 | /// #pragma GCC poison/system_header/dependency and #pragma once. |
| 1010 | void Preprocessor::RegisterBuiltinPragmas() { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1011 | AddPragmaHandler(new PragmaOnceHandler()); |
| 1012 | AddPragmaHandler(new PragmaMarkHandler()); |
Chris Lattner | f47724b | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1013 | AddPragmaHandler(new PragmaPushMacroHandler()); |
| 1014 | AddPragmaHandler(new PragmaPopMacroHandler()); |
Michael J. Spencer | 301669b | 2010-09-27 06:19:02 +0000 | [diff] [blame] | 1015 | AddPragmaHandler(new PragmaMessageHandler()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | |
Chris Lattner | e8fa06e | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1017 | // #pragma GCC ... |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1018 | AddPragmaHandler("GCC", new PragmaPoisonHandler()); |
| 1019 | AddPragmaHandler("GCC", new PragmaSystemHeaderHandler()); |
| 1020 | AddPragmaHandler("GCC", new PragmaDependencyHandler()); |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1021 | AddPragmaHandler("GCC", new PragmaDiagnosticHandler()); |
Chris Lattner | e8fa06e | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1022 | // #pragma clang ... |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1023 | AddPragmaHandler("clang", new PragmaPoisonHandler()); |
| 1024 | AddPragmaHandler("clang", new PragmaSystemHeaderHandler()); |
Daniel Dunbar | abf7b72 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1025 | AddPragmaHandler("clang", new PragmaDebugHandler()); |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1026 | AddPragmaHandler("clang", new PragmaDependencyHandler()); |
Douglas Gregor | 6493a4d | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1027 | AddPragmaHandler("clang", new PragmaDiagnosticHandler()); |
Chris Lattner | e8fa06e | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1028 | |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1029 | AddPragmaHandler("STDC", new PragmaSTDC_FP_CONTRACTHandler()); |
| 1030 | AddPragmaHandler("STDC", new PragmaSTDC_FENV_ACCESSHandler()); |
| 1031 | AddPragmaHandler("STDC", new PragmaSTDC_CX_LIMITED_RANGEHandler()); |
Chris Lattner | 062f232 | 2009-04-19 21:20:35 +0000 | [diff] [blame] | 1032 | AddPragmaHandler("STDC", new PragmaSTDC_UnknownHandler()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1033 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 1034 | // MS extensions. |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1035 | if (Features.Microsoft) { |
Argyrios Kyrtzidis | 9b36c3f | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1036 | AddPragmaHandler(new PragmaCommentHandler()); |
Chris Lattner | abfe094 | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1037 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1038 | } |