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" |
| 17 | #include "clang/Lex/Preprocessor.h" |
| 18 | #include "clang/Basic/Diagnostic.h" |
| 19 | #include "clang/Basic/FileManager.h" |
| 20 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
| 23 | // Out-of-line destructor to provide a home for the class. |
| 24 | PragmaHandler::~PragmaHandler() { |
| 25 | } |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // PragmaNamespace Implementation. |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | |
| 32 | PragmaNamespace::~PragmaNamespace() { |
| 33 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) |
| 34 | delete Handlers[i]; |
| 35 | } |
| 36 | |
| 37 | /// FindHandler - Check to see if there is already a handler for the |
| 38 | /// specified name. If not, return the handler for the null identifier if it |
| 39 | /// exists, otherwise return null. If IgnoreNull is true (the default) then |
| 40 | /// the null handler isn't returned on failure to match. |
| 41 | PragmaHandler *PragmaNamespace::FindHandler(const IdentifierInfo *Name, |
| 42 | bool IgnoreNull) const { |
| 43 | PragmaHandler *NullHandler = 0; |
| 44 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
| 45 | if (Handlers[i]->getName() == Name) |
| 46 | return Handlers[i]; |
| 47 | |
| 48 | if (Handlers[i]->getName() == 0) |
| 49 | NullHandler = Handlers[i]; |
| 50 | } |
| 51 | return IgnoreNull ? 0 : NullHandler; |
| 52 | } |
| 53 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 54 | void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) { |
| 55 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
| 56 | if (Handlers[i] == Handler) { |
| 57 | Handlers[i] = Handlers.back(); |
| 58 | Handlers.pop_back(); |
| 59 | return; |
| 60 | } |
| 61 | } |
| 62 | assert(0 && "Handler not registered in this namespace"); |
| 63 | } |
| 64 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 65 | void PragmaNamespace::HandlePragma(Preprocessor &PP, Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro |
| 67 | // expand it, the user can have a STDC #define, that should not affect this. |
| 68 | PP.LexUnexpandedToken(Tok); |
| 69 | |
| 70 | // Get the handler for this token. If there is no handler, ignore the pragma. |
| 71 | PragmaHandler *Handler = FindHandler(Tok.getIdentifierInfo(), false); |
| 72 | if (Handler == 0) return; |
| 73 | |
| 74 | // Otherwise, pass it down. |
| 75 | Handler->HandlePragma(PP, Tok); |
| 76 | } |
| 77 | |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | // Preprocessor Pragma Directive Handling. |
| 80 | //===----------------------------------------------------------------------===// |
| 81 | |
| 82 | /// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the |
| 83 | /// rest of the pragma, passing it to the registered pragma handlers. |
| 84 | void Preprocessor::HandlePragmaDirective() { |
| 85 | ++NumPragma; |
| 86 | |
| 87 | // Invoke the first level of pragma handlers which reads the namespace id. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 88 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 89 | PragmaHandlers->HandlePragma(*this, Tok); |
| 90 | |
| 91 | // 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] | 92 | if (CurPPLexer->ParsingPreprocessorDirective) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 93 | DiscardUntilEndOfDirective(); |
| 94 | } |
| 95 | |
| 96 | /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then |
| 97 | /// return the first token after the directive. The _Pragma token has just |
| 98 | /// been read into 'Tok'. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 99 | void Preprocessor::Handle_Pragma(Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 100 | // Remember the pragma token location. |
| 101 | SourceLocation PragmaLoc = Tok.getLocation(); |
| 102 | |
| 103 | // Read the '('. |
| 104 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 105 | if (Tok.isNot(tok::l_paren)) { |
| 106 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 107 | return; |
| 108 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 109 | |
| 110 | // Read the '"..."'. |
| 111 | Lex(Tok); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 112 | if (Tok.isNot(tok::string_literal) && Tok.isNot(tok::wide_string_literal)) { |
| 113 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 114 | return; |
| 115 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | |
| 117 | // Remember the string. |
| 118 | std::string StrVal = getSpelling(Tok); |
| 119 | SourceLocation StrLoc = Tok.getLocation(); |
| 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 | |
| 128 | // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1. |
| 129 | if (StrVal[0] == 'L') // Remove L prefix. |
| 130 | StrVal.erase(StrVal.begin()); |
| 131 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 132 | "Invalid string token!"); |
| 133 | |
| 134 | // Remove the front quote, replacing it with a space, so that the pragma |
| 135 | // contents appear to have a space before them. |
| 136 | StrVal[0] = ' '; |
| 137 | |
| 138 | // Replace the terminating quote with a \n\0. |
| 139 | StrVal[StrVal.size()-1] = '\n'; |
| 140 | StrVal += '\0'; |
| 141 | |
| 142 | // Remove escaped quotes and escapes. |
| 143 | for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) { |
| 144 | if (StrVal[i] == '\\' && |
| 145 | (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) { |
| 146 | // \\ -> '\' and \" -> '"'. |
| 147 | StrVal.erase(StrVal.begin()+i); |
| 148 | --e; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Plop the string (including the newline and trailing null) into a buffer |
| 153 | // where we can lex it. |
| 154 | SourceLocation TokLoc = CreateString(&StrVal[0], StrVal.size(), StrLoc); |
| 155 | const char *StrData = SourceMgr.getCharacterData(TokLoc); |
| 156 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 157 | // Make and enter a lexer object so that we lex and expand the tokens just |
| 158 | // like any others. |
Chris Lattner | 25bdb51 | 2007-07-20 16:52:03 +0000 | [diff] [blame] | 159 | Lexer *TL = new Lexer(TokLoc, *this, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | StrData, StrData+StrVal.size()-1 /* no null */); |
| 161 | |
| 162 | // Ensure that the lexer thinks it is inside a directive, so that end \n will |
| 163 | // return an EOM token. |
| 164 | TL->ParsingPreprocessorDirective = true; |
| 165 | |
| 166 | // This lexer really is for _Pragma. |
| 167 | TL->Is_PragmaLexer = true; |
| 168 | |
| 169 | EnterSourceFileWithLexer(TL, 0); |
| 170 | |
| 171 | // With everything set up, lex this as a #pragma directive. |
| 172 | HandlePragmaDirective(); |
| 173 | |
| 174 | // Finally, return whatever came after the pragma directive. |
| 175 | return Lex(Tok); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | |
| 180 | /// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'. |
| 181 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 182 | void Preprocessor::HandlePragmaOnce(Token &OnceTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 183 | if (isInPrimaryFile()) { |
| 184 | Diag(OnceTok, diag::pp_pragma_once_in_main_file); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc. |
Ted Kremenek | ac80c6e | 2008-11-19 22:55:25 +0000 | [diff] [blame] | 189 | unsigned FileID = getCurrentFileLexer()->getFileID(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | |
| 191 | // Mark the file as a once-only file now. |
Ted Kremenek | ac80c6e | 2008-11-19 22:55:25 +0000 | [diff] [blame] | 192 | HeaderInfo.MarkFileIncludeOnce(SourceMgr.getFileEntryForID(FileID)); |
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. |
Ted Kremenek | ac80c6e | 2008-11-19 22:55:25 +0000 | [diff] [blame] | 254 | const FileEntry *File = SourceMgr.getFileEntryForID(TheLexer->getFileID()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 255 | HeaderInfo.MarkFileSystemHeader(File); |
| 256 | |
| 257 | // Notify the client, if desired, that we are in a new source file. |
| 258 | if (Callbacks) |
Ted Kremenek | 35c10c2 | 2008-11-20 01:45:11 +0000 | [diff] [blame] | 259 | Callbacks->FileChanged(SysHeaderTok.getLocation(), |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 260 | PPCallbacks::SystemHeaderPragma, SrcMgr::C_System); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah. |
| 264 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 265 | void Preprocessor::HandlePragmaDependency(Token &DependencyTok) { |
| 266 | Token FilenameTok; |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 267 | CurPPLexer->LexIncludeFilename(FilenameTok); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 268 | |
| 269 | // If the token kind is EOM, the error has already been diagnosed. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 270 | if (FilenameTok.is(tok::eom)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 271 | return; |
| 272 | |
| 273 | // Reserve a buffer to get the spelling. |
| 274 | llvm::SmallVector<char, 128> FilenameBuffer; |
| 275 | FilenameBuffer.resize(FilenameTok.getLength()); |
| 276 | |
Chris Lattner | f1c99ac | 2007-07-23 04:15:27 +0000 | [diff] [blame] | 277 | const char *FilenameStart = &FilenameBuffer[0]; |
| 278 | unsigned Len = getSpelling(FilenameTok, FilenameStart); |
| 279 | const char *FilenameEnd = FilenameStart+Len; |
| 280 | bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | FilenameStart, FilenameEnd); |
| 282 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 283 | // error. |
| 284 | if (FilenameStart == 0) |
| 285 | return; |
| 286 | |
| 287 | // Search include directories for this file. |
| 288 | const DirectoryLookup *CurDir; |
| 289 | const FileEntry *File = LookupFile(FilenameStart, FilenameEnd, |
| 290 | isAngled, 0, CurDir); |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 291 | if (File == 0) { |
| 292 | Diag(FilenameTok, diag::err_pp_file_not_found) |
| 293 | << std::string(FilenameStart, FilenameEnd); |
| 294 | return; |
| 295 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 296 | |
Ted Kremenek | ac80c6e | 2008-11-19 22:55:25 +0000 | [diff] [blame] | 297 | unsigned FileID = getCurrentFileLexer()->getFileID(); |
| 298 | const FileEntry *CurFile = SourceMgr.getFileEntryForID(FileID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 299 | |
| 300 | // If this file is older than the file it depends on, emit a diagnostic. |
| 301 | if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) { |
| 302 | // Lex tokens at the end of the message and include them in the message. |
| 303 | std::string Message; |
| 304 | Lex(DependencyTok); |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 305 | while (DependencyTok.isNot(tok::eom)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 306 | Message += getSpelling(DependencyTok) + " "; |
| 307 | Lex(DependencyTok); |
| 308 | } |
| 309 | |
| 310 | Message.erase(Message.end()-1); |
Chris Lattner | 56b05c8 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 311 | Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | |
| 316 | /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. |
| 317 | /// If 'Namespace' is non-null, then it is a token required to exist on the |
| 318 | /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". |
| 319 | void Preprocessor::AddPragmaHandler(const char *Namespace, |
| 320 | PragmaHandler *Handler) { |
| 321 | PragmaNamespace *InsertNS = PragmaHandlers; |
| 322 | |
| 323 | // If this is specified to be in a namespace, step down into it. |
| 324 | if (Namespace) { |
| 325 | IdentifierInfo *NSID = getIdentifierInfo(Namespace); |
| 326 | |
| 327 | // If there is already a pragma handler with the name of this namespace, |
| 328 | // we either have an error (directive with the same name as a namespace) or |
| 329 | // we already have the namespace to insert into. |
| 330 | if (PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID)) { |
| 331 | InsertNS = Existing->getIfNamespace(); |
| 332 | assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma" |
| 333 | " handler with the same name!"); |
| 334 | } else { |
| 335 | // Otherwise, this namespace doesn't exist yet, create and insert the |
| 336 | // handler for it. |
| 337 | InsertNS = new PragmaNamespace(NSID); |
| 338 | PragmaHandlers->AddPragma(InsertNS); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | // Check to make sure we don't already have a pragma for this identifier. |
| 343 | assert(!InsertNS->FindHandler(Handler->getName()) && |
| 344 | "Pragma handler already exists for this identifier!"); |
| 345 | InsertNS->AddPragma(Handler); |
| 346 | } |
| 347 | |
Daniel Dunbar | 4095080 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 348 | /// RemovePragmaHandler - Remove the specific pragma handler from the |
| 349 | /// preprocessor. If \arg Namespace is non-null, then it should be the |
| 350 | /// namespace that \arg Handler was added to. It is an error to remove |
| 351 | /// a handler that has not been registered. |
| 352 | void Preprocessor::RemovePragmaHandler(const char *Namespace, |
| 353 | PragmaHandler *Handler) { |
| 354 | PragmaNamespace *NS = PragmaHandlers; |
| 355 | |
| 356 | // If this is specified to be in a namespace, step down into it. |
| 357 | if (Namespace) { |
| 358 | IdentifierInfo *NSID = getIdentifierInfo(Namespace); |
| 359 | PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID); |
| 360 | assert(Existing && "Namespace containing handler does not exist!"); |
| 361 | |
| 362 | NS = Existing->getIfNamespace(); |
| 363 | assert(NS && "Invalid namespace, registered as a regular pragma handler!"); |
| 364 | } |
| 365 | |
| 366 | NS->RemovePragmaHandler(Handler); |
| 367 | |
| 368 | // If this is a non-default namespace and it is now empty, remove |
| 369 | // it. |
| 370 | if (NS != PragmaHandlers && NS->IsEmpty()) |
| 371 | PragmaHandlers->RemovePragmaHandler(NS); |
| 372 | } |
| 373 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 374 | namespace { |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 375 | /// PragmaOnceHandler - "#pragma once" marks the file as atomically included. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 376 | struct PragmaOnceHandler : public PragmaHandler { |
| 377 | PragmaOnceHandler(const IdentifierInfo *OnceID) : PragmaHandler(OnceID) {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 378 | virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 379 | PP.CheckEndOfDirective("#pragma once"); |
| 380 | PP.HandlePragmaOnce(OnceTok); |
| 381 | } |
| 382 | }; |
| 383 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 384 | /// PragmaMarkHandler - "#pragma mark ..." is ignored by the compiler, and the |
| 385 | /// rest of the line is not lexed. |
| 386 | struct PragmaMarkHandler : public PragmaHandler { |
| 387 | PragmaMarkHandler(const IdentifierInfo *MarkID) : PragmaHandler(MarkID) {} |
| 388 | virtual void HandlePragma(Preprocessor &PP, Token &MarkTok) { |
| 389 | PP.HandlePragmaMark(); |
| 390 | } |
| 391 | }; |
| 392 | |
| 393 | /// PragmaPoisonHandler - "#pragma poison x" marks x as not usable. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 394 | struct PragmaPoisonHandler : public PragmaHandler { |
| 395 | PragmaPoisonHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 396 | virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 397 | PP.HandlePragmaPoison(PoisonTok); |
| 398 | } |
| 399 | }; |
| 400 | |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 401 | /// PragmaSystemHeaderHandler - "#pragma system_header" marks the current file |
| 402 | /// as a system header, which silences warnings in it. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 403 | struct PragmaSystemHeaderHandler : public PragmaHandler { |
| 404 | PragmaSystemHeaderHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 405 | virtual void HandlePragma(Preprocessor &PP, Token &SHToken) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 406 | PP.HandlePragmaSystemHeader(SHToken); |
| 407 | PP.CheckEndOfDirective("#pragma"); |
| 408 | } |
| 409 | }; |
| 410 | struct PragmaDependencyHandler : public PragmaHandler { |
| 411 | PragmaDependencyHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 412 | virtual void HandlePragma(Preprocessor &PP, Token &DepToken) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 413 | PP.HandlePragmaDependency(DepToken); |
| 414 | } |
| 415 | }; |
| 416 | } // end anonymous namespace |
| 417 | |
| 418 | |
| 419 | /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas: |
| 420 | /// #pragma GCC poison/system_header/dependency and #pragma once. |
| 421 | void Preprocessor::RegisterBuiltinPragmas() { |
| 422 | AddPragmaHandler(0, new PragmaOnceHandler(getIdentifierInfo("once"))); |
Chris Lattner | 2243449 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 423 | AddPragmaHandler(0, new PragmaMarkHandler(getIdentifierInfo("mark"))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 424 | AddPragmaHandler("GCC", new PragmaPoisonHandler(getIdentifierInfo("poison"))); |
| 425 | AddPragmaHandler("GCC", new PragmaSystemHeaderHandler( |
| 426 | getIdentifierInfo("system_header"))); |
| 427 | AddPragmaHandler("GCC", new PragmaDependencyHandler( |
| 428 | getIdentifierInfo("dependency"))); |
| 429 | } |