Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Pragma.cpp - Pragma registration and handling --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the PragmaHandler/PragmaTable interfaces and implements |
| 11 | // pragma related methods of the Preprocessor class. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Lex/Pragma.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/Lex/HeaderSearch.h" |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 17 | #include "clang/Lex/LiteralSupport.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 19 | #include "clang/Lex/LexDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/Basic/FileManager.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
| 24 | // Out-of-line destructor to provide a home for the class. |
| 25 | PragmaHandler::~PragmaHandler() { |
| 26 | } |
| 27 | |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | // PragmaNamespace Implementation. |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
| 32 | |
| 33 | PragmaNamespace::~PragmaNamespace() { |
| 34 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) |
| 35 | delete Handlers[i]; |
| 36 | } |
| 37 | |
| 38 | /// FindHandler - Check to see if there is already a handler for the |
| 39 | /// specified name. If not, return the handler for the null identifier if it |
| 40 | /// exists, otherwise return null. If IgnoreNull is true (the default) then |
| 41 | /// the null handler isn't returned on failure to match. |
| 42 | PragmaHandler *PragmaNamespace::FindHandler(const IdentifierInfo *Name, |
| 43 | bool IgnoreNull) const { |
| 44 | PragmaHandler *NullHandler = 0; |
| 45 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
| 46 | if (Handlers[i]->getName() == Name) |
| 47 | return Handlers[i]; |
| 48 | |
| 49 | if (Handlers[i]->getName() == 0) |
| 50 | NullHandler = Handlers[i]; |
| 51 | } |
| 52 | return IgnoreNull ? 0 : NullHandler; |
| 53 | } |
| 54 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 55 | void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) { |
| 56 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
| 57 | if (Handlers[i] == Handler) { |
| 58 | Handlers[i] = Handlers.back(); |
| 59 | Handlers.pop_back(); |
| 60 | return; |
| 61 | } |
| 62 | } |
| 63 | assert(0 && "Handler not registered in this namespace"); |
| 64 | } |
| 65 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 66 | void PragmaNamespace::HandlePragma(Preprocessor &PP, Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 67 | // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro |
| 68 | // expand it, the user can have a STDC #define, that should not affect this. |
| 69 | PP.LexUnexpandedToken(Tok); |
| 70 | |
| 71 | // Get the handler for this token. If there is no handler, ignore the pragma. |
| 72 | PragmaHandler *Handler = FindHandler(Tok.getIdentifierInfo(), false); |
| 73 | if (Handler == 0) return; |
| 74 | |
| 75 | // Otherwise, pass it down. |
| 76 | Handler->HandlePragma(PP, Tok); |
| 77 | } |
| 78 | |
| 79 | //===----------------------------------------------------------------------===// |
| 80 | // Preprocessor Pragma Directive Handling. |
| 81 | //===----------------------------------------------------------------------===// |
| 82 | |
| 83 | /// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the |
| 84 | /// rest of the pragma, passing it to the registered pragma handlers. |
| 85 | void Preprocessor::HandlePragmaDirective() { |
| 86 | ++NumPragma; |
| 87 | |
| 88 | // Invoke the first level of pragma handlers which reads the namespace id. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 89 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 90 | PragmaHandlers->HandlePragma(*this, Tok); |
| 91 | |
| 92 | // If the pragma handler didn't read the rest of the line, consume it now. |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 93 | if (CurPPLexer->ParsingPreprocessorDirective) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 94 | DiscardUntilEndOfDirective(); |
| 95 | } |
| 96 | |
| 97 | /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then |
| 98 | /// return the first token after the directive. The _Pragma token has just |
| 99 | /// been read into 'Tok'. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 100 | void Preprocessor::Handle_Pragma(Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | // Remember the pragma token location. |
| 102 | SourceLocation PragmaLoc = Tok.getLocation(); |
| 103 | |
| 104 | // Read the '('. |
| 105 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 106 | if (Tok.isNot(tok::l_paren)) { |
| 107 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 108 | return; |
| 109 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | |
| 111 | // Read the '"..."'. |
| 112 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 113 | if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) { |
| 114 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 115 | return; |
| 116 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 117 | |
| 118 | // Remember the string. |
| 119 | std::string StrVal = getSpelling(Tok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | |
| 121 | // Read the ')'. |
| 122 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 123 | if (Tok.isNot(tok::r_paren)) { |
| 124 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 125 | return; |
| 126 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 127 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 128 | SourceLocation RParenLoc = Tok.getLocation(); |
| 129 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 130 | // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1: |
| 131 | // "The string literal is destringized by deleting the L prefix, if present, |
| 132 | // deleting the leading and trailing double-quotes, replacing each escape |
| 133 | // sequence \" by a double-quote, and replacing each escape sequence \\ by a |
| 134 | // single backslash." |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 135 | if (StrVal[0] == 'L') // Remove L prefix. |
| 136 | StrVal.erase(StrVal.begin()); |
| 137 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 138 | "Invalid string token!"); |
| 139 | |
| 140 | // Remove the front quote, replacing it with a space, so that the pragma |
| 141 | // contents appear to have a space before them. |
| 142 | StrVal[0] = ' '; |
| 143 | |
| 144 | // Replace the terminating quote with a \n\0. |
| 145 | StrVal[StrVal.size()-1] = '\n'; |
| 146 | StrVal += '\0'; |
| 147 | |
| 148 | // Remove escaped quotes and escapes. |
| 149 | for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) { |
| 150 | if (StrVal[i] == '\\' && |
| 151 | (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) { |
| 152 | // \\ -> '\' and \" -> '"'. |
| 153 | StrVal.erase(StrVal.begin()+i); |
| 154 | --e; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Plop the string (including the newline and trailing null) into a buffer |
| 159 | // where we can lex it. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 160 | Token TmpTok; |
| 161 | TmpTok.startToken(); |
| 162 | CreateString(&StrVal[0], StrVal.size(), TmpTok); |
| 163 | SourceLocation TokLoc = TmpTok.getLocation(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 164 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 165 | // Make and enter a lexer object so that we lex and expand the tokens just |
| 166 | // like any others. |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 167 | Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc, |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 168 | // do not include the null in the count. |
| 169 | StrVal.size()-1, *this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | |
| 171 | EnterSourceFileWithLexer(TL, 0); |
| 172 | |
| 173 | // With everything set up, lex this as a #pragma directive. |
| 174 | HandlePragmaDirective(); |
| 175 | |
| 176 | // Finally, return whatever came after the pragma directive. |
| 177 | return Lex(Tok); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | |
| 182 | /// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'. |
| 183 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 184 | void Preprocessor::HandlePragmaOnce(Token &OnceTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 185 | if (isInPrimaryFile()) { |
| 186 | Diag(OnceTok, diag::pp_pragma_once_in_main_file); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | // 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] | 191 | // Mark the file as a once-only file now. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 192 | HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 195 | void Preprocessor::HandlePragmaMark() { |
Ted Kremenek | 17ff58a | 2008-11-19 22:21:33 +0000 | [diff] [blame] | 196 | assert(CurPPLexer && "No current lexer?"); |
| 197 | if (CurLexer) CurLexer->ReadToEndOfLine(); |
| 198 | else CurPTHLexer->DiscardToEndOfLine(); |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 202 | /// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'. |
| 203 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 204 | void Preprocessor::HandlePragmaPoison(Token &PoisonTok) { |
| 205 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 206 | |
| 207 | while (1) { |
| 208 | // Read the next token to poison. While doing this, pretend that we are |
| 209 | // skipping while reading the identifier to poison. |
| 210 | // This avoids errors on code like: |
| 211 | // #pragma GCC poison X |
| 212 | // #pragma GCC poison X |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 213 | if (CurPPLexer) CurPPLexer->LexingRawMode = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 214 | LexUnexpandedToken(Tok); |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 215 | if (CurPPLexer) CurPPLexer->LexingRawMode = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 216 | |
| 217 | // If we reached the end of line, we're done. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 218 | if (Tok.is(tok::eom)) return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 219 | |
| 220 | // Can only poison identifiers. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 221 | if (Tok.isNot(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 222 | Diag(Tok, diag::err_pp_invalid_poison); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | // Look up the identifier info for the token. We disabled identifier lookup |
| 227 | // by saying we're skipping contents, so we need to do this manually. |
| 228 | IdentifierInfo *II = LookUpIdentifierInfo(Tok); |
| 229 | |
| 230 | // Already poisoned. |
| 231 | if (II->isPoisoned()) continue; |
| 232 | |
| 233 | // If this is a macro identifier, emit a warning. |
Chris Lattner | 0edde55 | 2007-10-07 08:04:56 +0000 | [diff] [blame] | 234 | if (II->hasMacroDefinition()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 235 | Diag(Tok, diag::pp_poisoning_existing_macro); |
| 236 | |
| 237 | // Finally, poison it! |
| 238 | II->setIsPoisoned(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know |
| 243 | /// that the whole directive has been parsed. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 244 | void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 245 | if (isInPrimaryFile()) { |
| 246 | Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file); |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | // 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] | 251 | PreprocessorLexer *TheLexer = getCurrentFileLexer(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 252 | |
| 253 | // Mark the file as a system header. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 254 | HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 255 | |
| 256 | // Notify the client, if desired, that we are in a new source file. |
| 257 | if (Callbacks) |
Ted Kremenek | 35c10c2 | 2008-11-20 01:45:11 +0000 | [diff] [blame] | 258 | Callbacks->FileChanged(SysHeaderTok.getLocation(), |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 259 | PPCallbacks::SystemHeaderPragma, SrcMgr::C_System); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah. |
| 263 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 264 | void Preprocessor::HandlePragmaDependency(Token &DependencyTok) { |
| 265 | Token FilenameTok; |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 266 | CurPPLexer->LexIncludeFilename(FilenameTok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 267 | |
| 268 | // If the token kind is EOM, the error has already been diagnosed. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 269 | if (FilenameTok.is(tok::eom)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 270 | return; |
| 271 | |
| 272 | // Reserve a buffer to get the spelling. |
| 273 | llvm::SmallVector<char, 128> FilenameBuffer; |
| 274 | FilenameBuffer.resize(FilenameTok.getLength()); |
| 275 | |
Chris Lattner | f1c99ac | 2007-07-23 04:15:27 +0000 | [diff] [blame] | 276 | const char *FilenameStart = &FilenameBuffer[0]; |
| 277 | unsigned Len = getSpelling(FilenameTok, FilenameStart); |
| 278 | const char *FilenameEnd = FilenameStart+Len; |
| 279 | bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | FilenameStart, FilenameEnd); |
| 281 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 282 | // error. |
| 283 | if (FilenameStart == 0) |
| 284 | return; |
| 285 | |
| 286 | // Search include directories for this file. |
| 287 | const DirectoryLookup *CurDir; |
| 288 | const FileEntry *File = LookupFile(FilenameStart, FilenameEnd, |
| 289 | isAngled, 0, CurDir); |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 290 | if (File == 0) { |
| 291 | Diag(FilenameTok, diag::err_pp_file_not_found) |
| 292 | << std::string(FilenameStart, FilenameEnd); |
| 293 | return; |
| 294 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 295 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 296 | const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 297 | |
| 298 | // If this file is older than the file it depends on, emit a diagnostic. |
| 299 | if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) { |
| 300 | // Lex tokens at the end of the message and include them in the message. |
| 301 | std::string Message; |
| 302 | Lex(DependencyTok); |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 303 | while (DependencyTok.isNot(tok::eom)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 304 | Message += getSpelling(DependencyTok) + " "; |
| 305 | Lex(DependencyTok); |
| 306 | } |
| 307 | |
| 308 | Message.erase(Message.end()-1); |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 309 | Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 313 | /// HandlePragmaComment - Handle the microsoft #pragma comment extension. The |
| 314 | /// syntax is: |
| 315 | /// #pragma comment(linker, "foo") |
| 316 | /// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user. |
| 317 | /// "foo" is a string, which is fully macro expanded, and permits string |
| 318 | /// concatenation, embeded escape characters etc. See MSDN for more details. |
| 319 | void Preprocessor::HandlePragmaComment(Token &Tok) { |
| 320 | SourceLocation CommentLoc = Tok.getLocation(); |
| 321 | Lex(Tok); |
| 322 | if (Tok.isNot(tok::l_paren)) { |
| 323 | Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | // Read the identifier. |
| 328 | Lex(Tok); |
| 329 | if (Tok.isNot(tok::identifier)) { |
| 330 | Diag(CommentLoc, diag::err_pragma_comment_malformed); |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | // Verify that this is one of the 5 whitelisted options. |
| 335 | // FIXME: warn that 'exestr' is deprecated. |
| 336 | const IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 337 | if (!II->isStr("compiler") && !II->isStr("exestr") && !II->isStr("lib") && |
| 338 | !II->isStr("linker") && !II->isStr("user")) { |
| 339 | Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind); |
| 340 | return; |
| 341 | } |
| 342 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 343 | // Read the optional string if present. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 344 | Lex(Tok); |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 345 | std::string ArgumentString; |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 346 | if (Tok.is(tok::comma)) { |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 347 | Lex(Tok); // eat the comma. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 348 | |
| 349 | // We need at least one string. |
| 350 | if (Tok.getKind() != tok::string_literal) { |
| 351 | Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | // String concatenation allows multiple strings, which can even come from |
| 356 | // macro expansion. |
| 357 | // "foo " "bar" "Baz" |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 358 | llvm::SmallVector<Token, 4> StrToks; |
| 359 | while (Tok.getKind() == tok::string_literal) { |
| 360 | StrToks.push_back(Tok); |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 361 | Lex(Tok); |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | // Concatenate and parse the strings. |
| 365 | StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this); |
| 366 | assert(!Literal.AnyWide && "Didn't allow wide strings in"); |
| 367 | if (Literal.hadError) |
| 368 | return; |
| 369 | if (Literal.Pascal) { |
| 370 | Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed); |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | ArgumentString = std::string(Literal.GetString(), |
| 375 | Literal.GetString()+Literal.GetStringLength()); |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 378 | // FIXME: If the kind is "compiler" warn if the string is present (it is |
| 379 | // ignored). |
| 380 | // FIXME: 'lib' requires a comment string. |
| 381 | // FIXME: 'linker' requires a comment string, and has a specific list of |
| 382 | // things that are allowable. |
| 383 | |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 384 | if (Tok.isNot(tok::r_paren)) { |
| 385 | Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 386 | return; |
| 387 | } |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 388 | Lex(Tok); // eat the r_paren. |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 389 | |
| 390 | if (Tok.isNot(tok::eom)) { |
| 391 | Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); |
| 392 | return; |
| 393 | } |
Chris Lattner | a9d9145 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 394 | |
| 395 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
Chris Lattner | 172e336 | 2009-01-16 19:01:46 +0000 | [diff] [blame] | 396 | if (Callbacks) |
| 397 | Callbacks->PragmaComment(CommentLoc, II, ArgumentString); |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | |
| 401 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 402 | |
| 403 | /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. |
| 404 | /// If 'Namespace' is non-null, then it is a token required to exist on the |
| 405 | /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". |
| 406 | void Preprocessor::AddPragmaHandler(const char *Namespace, |
| 407 | PragmaHandler *Handler) { |
| 408 | PragmaNamespace *InsertNS = PragmaHandlers; |
| 409 | |
| 410 | // If this is specified to be in a namespace, step down into it. |
| 411 | if (Namespace) { |
| 412 | IdentifierInfo *NSID = getIdentifierInfo(Namespace); |
| 413 | |
| 414 | // If there is already a pragma handler with the name of this namespace, |
| 415 | // we either have an error (directive with the same name as a namespace) or |
| 416 | // we already have the namespace to insert into. |
| 417 | if (PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID)) { |
| 418 | InsertNS = Existing->getIfNamespace(); |
| 419 | assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma" |
| 420 | " handler with the same name!"); |
| 421 | } else { |
| 422 | // Otherwise, this namespace doesn't exist yet, create and insert the |
| 423 | // handler for it. |
| 424 | InsertNS = new PragmaNamespace(NSID); |
| 425 | PragmaHandlers->AddPragma(InsertNS); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | // Check to make sure we don't already have a pragma for this identifier. |
| 430 | assert(!InsertNS->FindHandler(Handler->getName()) && |
| 431 | "Pragma handler already exists for this identifier!"); |
| 432 | InsertNS->AddPragma(Handler); |
| 433 | } |
| 434 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 435 | /// RemovePragmaHandler - Remove the specific pragma handler from the |
| 436 | /// preprocessor. If \arg Namespace is non-null, then it should be the |
| 437 | /// namespace that \arg Handler was added to. It is an error to remove |
| 438 | /// a handler that has not been registered. |
| 439 | void Preprocessor::RemovePragmaHandler(const char *Namespace, |
| 440 | PragmaHandler *Handler) { |
| 441 | PragmaNamespace *NS = PragmaHandlers; |
| 442 | |
| 443 | // If this is specified to be in a namespace, step down into it. |
| 444 | if (Namespace) { |
| 445 | IdentifierInfo *NSID = getIdentifierInfo(Namespace); |
| 446 | PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID); |
| 447 | assert(Existing && "Namespace containing handler does not exist!"); |
| 448 | |
| 449 | NS = Existing->getIfNamespace(); |
| 450 | assert(NS && "Invalid namespace, registered as a regular pragma handler!"); |
| 451 | } |
| 452 | |
| 453 | NS->RemovePragmaHandler(Handler); |
| 454 | |
| 455 | // If this is a non-default namespace and it is now empty, remove |
| 456 | // it. |
| 457 | if (NS != PragmaHandlers && NS->IsEmpty()) |
| 458 | PragmaHandlers->RemovePragmaHandler(NS); |
| 459 | } |
| 460 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 461 | namespace { |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 462 | /// PragmaOnceHandler - "#pragma once" marks the file as atomically included. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 463 | struct PragmaOnceHandler : public PragmaHandler { |
| 464 | PragmaOnceHandler(const IdentifierInfo *OnceID) : PragmaHandler(OnceID) {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 465 | virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 466 | PP.CheckEndOfDirective("#pragma once"); |
| 467 | PP.HandlePragmaOnce(OnceTok); |
| 468 | } |
| 469 | }; |
| 470 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 471 | /// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the |
| 472 | /// rest of the line is not lexed. |
| 473 | struct PragmaMarkHandler : public PragmaHandler { |
| 474 | PragmaMarkHandler(const IdentifierInfo *MarkID) : PragmaHandler(MarkID) {} |
| 475 | virtual void HandlePragma(Preprocessor &PP, Token &MarkTok) { |
| 476 | PP.HandlePragmaMark(); |
| 477 | } |
| 478 | }; |
| 479 | |
| 480 | /// PragmaPoisonHandler - "#pragma poison x" marks x as not usable. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 481 | struct PragmaPoisonHandler : public PragmaHandler { |
| 482 | PragmaPoisonHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 483 | virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 484 | PP.HandlePragmaPoison(PoisonTok); |
| 485 | } |
| 486 | }; |
| 487 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 488 | /// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file |
| 489 | /// as a system header, which silences warnings in it. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 490 | struct PragmaSystemHeaderHandler : public PragmaHandler { |
| 491 | PragmaSystemHeaderHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 492 | virtual void HandlePragma(Preprocessor &PP, Token &SHToken) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 493 | PP.HandlePragmaSystemHeader(SHToken); |
| 494 | PP.CheckEndOfDirective("#pragma"); |
| 495 | } |
| 496 | }; |
| 497 | struct PragmaDependencyHandler : public PragmaHandler { |
| 498 | PragmaDependencyHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 499 | virtual void HandlePragma(Preprocessor &PP, Token &DepToken) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 500 | PP.HandlePragmaDependency(DepToken); |
| 501 | } |
| 502 | }; |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 503 | |
| 504 | /// PragmaCommentHandler - "#pragma comment ...". |
| 505 | struct PragmaCommentHandler : public PragmaHandler { |
| 506 | PragmaCommentHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {} |
| 507 | virtual void HandlePragma(Preprocessor &PP, Token &CommentTok) { |
| 508 | PP.HandlePragmaComment(CommentTok); |
| 509 | } |
| 510 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 511 | } // end anonymous namespace |
| 512 | |
| 513 | |
| 514 | /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas: |
| 515 | /// #pragma GCC poison/system_header/dependency and #pragma once. |
| 516 | void Preprocessor::RegisterBuiltinPragmas() { |
| 517 | AddPragmaHandler(0, new PragmaOnceHandler(getIdentifierInfo("once"))); |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 518 | AddPragmaHandler(0, new PragmaMarkHandler(getIdentifierInfo("mark"))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 519 | AddPragmaHandler("GCC", new PragmaPoisonHandler(getIdentifierInfo("poison"))); |
| 520 | AddPragmaHandler("GCC", new PragmaSystemHeaderHandler( |
| 521 | getIdentifierInfo("system_header"))); |
| 522 | AddPragmaHandler("GCC", new PragmaDependencyHandler( |
| 523 | getIdentifierInfo("dependency"))); |
Chris Lattner | 636c5ef | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 524 | |
| 525 | // MS extensions. |
| 526 | if (Features.Microsoft) |
| 527 | AddPragmaHandler(0, new PragmaCommentHandler(getIdentifierInfo("comment"))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 528 | } |