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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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" |
| 16 | #include "clang/Lex/PPCallbacks.h" |
| 17 | #include "clang/Lex/HeaderSearch.h" |
| 18 | #include "clang/Lex/Preprocessor.h" |
| 19 | #include "clang/Basic/Diagnostic.h" |
| 20 | #include "clang/Basic/FileManager.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
| 22 | #include "llvm/ADT/SmallVector.h" |
| 23 | using namespace clang; |
| 24 | |
| 25 | // Out-of-line destructor to provide a home for the class. |
| 26 | PragmaHandler::~PragmaHandler() { |
| 27 | } |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | // PragmaNamespace Implementation. |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| 33 | |
| 34 | PragmaNamespace::~PragmaNamespace() { |
| 35 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) |
| 36 | delete Handlers[i]; |
| 37 | } |
| 38 | |
| 39 | /// FindHandler - Check to see if there is already a handler for the |
| 40 | /// specified name. If not, return the handler for the null identifier if it |
| 41 | /// exists, otherwise return null. If IgnoreNull is true (the default) then |
| 42 | /// the null handler isn't returned on failure to match. |
| 43 | PragmaHandler *PragmaNamespace::FindHandler(const IdentifierInfo *Name, |
| 44 | bool IgnoreNull) const { |
| 45 | PragmaHandler *NullHandler = 0; |
| 46 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
| 47 | if (Handlers[i]->getName() == Name) |
| 48 | return Handlers[i]; |
| 49 | |
| 50 | if (Handlers[i]->getName() == 0) |
| 51 | NullHandler = Handlers[i]; |
| 52 | } |
| 53 | return IgnoreNull ? 0 : NullHandler; |
| 54 | } |
| 55 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 56 | void PragmaNamespace::HandlePragma(Preprocessor &PP, Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 57 | // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro |
| 58 | // expand it, the user can have a STDC #define, that should not affect this. |
| 59 | PP.LexUnexpandedToken(Tok); |
| 60 | |
| 61 | // Get the handler for this token. If there is no handler, ignore the pragma. |
| 62 | PragmaHandler *Handler = FindHandler(Tok.getIdentifierInfo(), false); |
| 63 | if (Handler == 0) return; |
| 64 | |
| 65 | // Otherwise, pass it down. |
| 66 | Handler->HandlePragma(PP, Tok); |
| 67 | } |
| 68 | |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | // Preprocessor Pragma Directive Handling. |
| 71 | //===----------------------------------------------------------------------===// |
| 72 | |
| 73 | /// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the |
| 74 | /// rest of the pragma, passing it to the registered pragma handlers. |
| 75 | void Preprocessor::HandlePragmaDirective() { |
| 76 | ++NumPragma; |
| 77 | |
| 78 | // Invoke the first level of pragma handlers which reads the namespace id. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 79 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 80 | PragmaHandlers->HandlePragma(*this, Tok); |
| 81 | |
| 82 | // If the pragma handler didn't read the rest of the line, consume it now. |
| 83 | if (CurLexer->ParsingPreprocessorDirective) |
| 84 | DiscardUntilEndOfDirective(); |
| 85 | } |
| 86 | |
| 87 | /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then |
| 88 | /// return the first token after the directive. The _Pragma token has just |
| 89 | /// been read into 'Tok'. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 90 | void Preprocessor::Handle_Pragma(Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 91 | // Remember the pragma token location. |
| 92 | SourceLocation PragmaLoc = Tok.getLocation(); |
| 93 | |
| 94 | // Read the '('. |
| 95 | Lex(Tok); |
| 96 | if (Tok.getKind() != tok::l_paren) |
| 97 | return Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 98 | |
| 99 | // Read the '"..."'. |
| 100 | Lex(Tok); |
| 101 | if (Tok.getKind() != tok::string_literal && |
| 102 | Tok.getKind() != tok::wide_string_literal) |
| 103 | return Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 104 | |
| 105 | // Remember the string. |
| 106 | std::string StrVal = getSpelling(Tok); |
| 107 | SourceLocation StrLoc = Tok.getLocation(); |
| 108 | |
| 109 | // Read the ')'. |
| 110 | Lex(Tok); |
| 111 | if (Tok.getKind() != tok::r_paren) |
| 112 | return Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 113 | |
| 114 | // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1. |
| 115 | if (StrVal[0] == 'L') // Remove L prefix. |
| 116 | StrVal.erase(StrVal.begin()); |
| 117 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 118 | "Invalid string token!"); |
| 119 | |
| 120 | // Remove the front quote, replacing it with a space, so that the pragma |
| 121 | // contents appear to have a space before them. |
| 122 | StrVal[0] = ' '; |
| 123 | |
| 124 | // Replace the terminating quote with a \n\0. |
| 125 | StrVal[StrVal.size()-1] = '\n'; |
| 126 | StrVal += '\0'; |
| 127 | |
| 128 | // Remove escaped quotes and escapes. |
| 129 | for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) { |
| 130 | if (StrVal[i] == '\\' && |
| 131 | (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) { |
| 132 | // \\ -> '\' and \" -> '"'. |
| 133 | StrVal.erase(StrVal.begin()+i); |
| 134 | --e; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Plop the string (including the newline and trailing null) into a buffer |
| 139 | // where we can lex it. |
| 140 | SourceLocation TokLoc = CreateString(&StrVal[0], StrVal.size(), StrLoc); |
| 141 | const char *StrData = SourceMgr.getCharacterData(TokLoc); |
| 142 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 143 | // Make and enter a lexer object so that we lex and expand the tokens just |
| 144 | // like any others. |
Chris Lattner | 25bdb51 | 2007-07-20 16:52:03 +0000 | [diff] [blame] | 145 | Lexer *TL = new Lexer(TokLoc, *this, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 146 | StrData, StrData+StrVal.size()-1 /* no null */); |
| 147 | |
| 148 | // Ensure that the lexer thinks it is inside a directive, so that end \n will |
| 149 | // return an EOM token. |
| 150 | TL->ParsingPreprocessorDirective = true; |
| 151 | |
| 152 | // This lexer really is for _Pragma. |
| 153 | TL->Is_PragmaLexer = true; |
| 154 | |
| 155 | EnterSourceFileWithLexer(TL, 0); |
| 156 | |
| 157 | // With everything set up, lex this as a #pragma directive. |
| 158 | HandlePragmaDirective(); |
| 159 | |
| 160 | // Finally, return whatever came after the pragma directive. |
| 161 | return Lex(Tok); |
| 162 | } |
| 163 | |
| 164 | |
| 165 | |
| 166 | /// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'. |
| 167 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 168 | void Preprocessor::HandlePragmaOnce(Token &OnceTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | if (isInPrimaryFile()) { |
| 170 | Diag(OnceTok, diag::pp_pragma_once_in_main_file); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 175 | SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 176 | |
| 177 | // Mark the file as a once-only file now. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 178 | HeaderInfo.MarkFileIncludeOnce(SourceMgr.getFileEntryForLoc(FileLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'. |
| 182 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 183 | void Preprocessor::HandlePragmaPoison(Token &PoisonTok) { |
| 184 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 185 | |
| 186 | while (1) { |
| 187 | // Read the next token to poison. While doing this, pretend that we are |
| 188 | // skipping while reading the identifier to poison. |
| 189 | // This avoids errors on code like: |
| 190 | // #pragma GCC poison X |
| 191 | // #pragma GCC poison X |
| 192 | if (CurLexer) CurLexer->LexingRawMode = true; |
| 193 | LexUnexpandedToken(Tok); |
| 194 | if (CurLexer) CurLexer->LexingRawMode = false; |
| 195 | |
| 196 | // If we reached the end of line, we're done. |
| 197 | if (Tok.getKind() == tok::eom) return; |
| 198 | |
| 199 | // Can only poison identifiers. |
| 200 | if (Tok.getKind() != tok::identifier) { |
| 201 | Diag(Tok, diag::err_pp_invalid_poison); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | // Look up the identifier info for the token. We disabled identifier lookup |
| 206 | // by saying we're skipping contents, so we need to do this manually. |
| 207 | IdentifierInfo *II = LookUpIdentifierInfo(Tok); |
| 208 | |
| 209 | // Already poisoned. |
| 210 | if (II->isPoisoned()) continue; |
| 211 | |
| 212 | // If this is a macro identifier, emit a warning. |
Chris Lattner | 0edde55 | 2007-10-07 08:04:56 +0000 | [diff] [blame^] | 213 | if (II->hasMacroDefinition()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 214 | Diag(Tok, diag::pp_poisoning_existing_macro); |
| 215 | |
| 216 | // Finally, poison it! |
| 217 | II->setIsPoisoned(); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know |
| 222 | /// that the whole directive has been parsed. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 223 | void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 224 | if (isInPrimaryFile()) { |
| 225 | Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file); |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc. |
| 230 | Lexer *TheLexer = getCurrentFileLexer(); |
| 231 | |
| 232 | // Mark the file as a system header. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 233 | const FileEntry *File = SourceMgr.getFileEntryForLoc(TheLexer->getFileLoc()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 234 | HeaderInfo.MarkFileSystemHeader(File); |
| 235 | |
| 236 | // Notify the client, if desired, that we are in a new source file. |
| 237 | if (Callbacks) |
| 238 | Callbacks->FileChanged(TheLexer->getSourceLocation(TheLexer->BufferPtr), |
| 239 | PPCallbacks::SystemHeaderPragma, |
| 240 | DirectoryLookup::SystemHeaderDir); |
| 241 | } |
| 242 | |
| 243 | /// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah. |
| 244 | /// |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 245 | void Preprocessor::HandlePragmaDependency(Token &DependencyTok) { |
| 246 | Token FilenameTok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 247 | CurLexer->LexIncludeFilename(FilenameTok); |
| 248 | |
| 249 | // If the token kind is EOM, the error has already been diagnosed. |
| 250 | if (FilenameTok.getKind() == tok::eom) |
| 251 | return; |
| 252 | |
| 253 | // Reserve a buffer to get the spelling. |
| 254 | llvm::SmallVector<char, 128> FilenameBuffer; |
| 255 | FilenameBuffer.resize(FilenameTok.getLength()); |
| 256 | |
Chris Lattner | f1c99ac | 2007-07-23 04:15:27 +0000 | [diff] [blame] | 257 | const char *FilenameStart = &FilenameBuffer[0]; |
| 258 | unsigned Len = getSpelling(FilenameTok, FilenameStart); |
| 259 | const char *FilenameEnd = FilenameStart+Len; |
| 260 | bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 261 | FilenameStart, FilenameEnd); |
| 262 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 263 | // error. |
| 264 | if (FilenameStart == 0) |
| 265 | return; |
| 266 | |
| 267 | // Search include directories for this file. |
| 268 | const DirectoryLookup *CurDir; |
| 269 | const FileEntry *File = LookupFile(FilenameStart, FilenameEnd, |
| 270 | isAngled, 0, CurDir); |
| 271 | if (File == 0) |
| 272 | return Diag(FilenameTok, diag::err_pp_file_not_found, |
| 273 | std::string(FilenameStart, FilenameEnd)); |
| 274 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 275 | SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc(); |
| 276 | const FileEntry *CurFile = SourceMgr.getFileEntryForLoc(FileLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 277 | |
| 278 | // If this file is older than the file it depends on, emit a diagnostic. |
| 279 | if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) { |
| 280 | // Lex tokens at the end of the message and include them in the message. |
| 281 | std::string Message; |
| 282 | Lex(DependencyTok); |
| 283 | while (DependencyTok.getKind() != tok::eom) { |
| 284 | Message += getSpelling(DependencyTok) + " "; |
| 285 | Lex(DependencyTok); |
| 286 | } |
| 287 | |
| 288 | Message.erase(Message.end()-1); |
| 289 | Diag(FilenameTok, diag::pp_out_of_date_dependency, Message); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | |
| 294 | /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. |
| 295 | /// If 'Namespace' is non-null, then it is a token required to exist on the |
| 296 | /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". |
| 297 | void Preprocessor::AddPragmaHandler(const char *Namespace, |
| 298 | PragmaHandler *Handler) { |
| 299 | PragmaNamespace *InsertNS = PragmaHandlers; |
| 300 | |
| 301 | // If this is specified to be in a namespace, step down into it. |
| 302 | if (Namespace) { |
| 303 | IdentifierInfo *NSID = getIdentifierInfo(Namespace); |
| 304 | |
| 305 | // If there is already a pragma handler with the name of this namespace, |
| 306 | // we either have an error (directive with the same name as a namespace) or |
| 307 | // we already have the namespace to insert into. |
| 308 | if (PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID)) { |
| 309 | InsertNS = Existing->getIfNamespace(); |
| 310 | assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma" |
| 311 | " handler with the same name!"); |
| 312 | } else { |
| 313 | // Otherwise, this namespace doesn't exist yet, create and insert the |
| 314 | // handler for it. |
| 315 | InsertNS = new PragmaNamespace(NSID); |
| 316 | PragmaHandlers->AddPragma(InsertNS); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | // Check to make sure we don't already have a pragma for this identifier. |
| 321 | assert(!InsertNS->FindHandler(Handler->getName()) && |
| 322 | "Pragma handler already exists for this identifier!"); |
| 323 | InsertNS->AddPragma(Handler); |
| 324 | } |
| 325 | |
| 326 | namespace { |
| 327 | struct PragmaOnceHandler : public PragmaHandler { |
| 328 | PragmaOnceHandler(const IdentifierInfo *OnceID) : PragmaHandler(OnceID) {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 329 | virtual void HandlePragma(Preprocessor &PP, Token &OnceTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 330 | PP.CheckEndOfDirective("#pragma once"); |
| 331 | PP.HandlePragmaOnce(OnceTok); |
| 332 | } |
| 333 | }; |
| 334 | |
| 335 | struct PragmaPoisonHandler : public PragmaHandler { |
| 336 | PragmaPoisonHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 337 | virtual void HandlePragma(Preprocessor &PP, Token &PoisonTok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 338 | PP.HandlePragmaPoison(PoisonTok); |
| 339 | } |
| 340 | }; |
| 341 | |
| 342 | struct PragmaSystemHeaderHandler : public PragmaHandler { |
| 343 | PragmaSystemHeaderHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 344 | virtual void HandlePragma(Preprocessor &PP, Token &SHToken) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 345 | PP.HandlePragmaSystemHeader(SHToken); |
| 346 | PP.CheckEndOfDirective("#pragma"); |
| 347 | } |
| 348 | }; |
| 349 | struct PragmaDependencyHandler : public PragmaHandler { |
| 350 | PragmaDependencyHandler(const IdentifierInfo *ID) : PragmaHandler(ID) {} |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 351 | virtual void HandlePragma(Preprocessor &PP, Token &DepToken) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 352 | PP.HandlePragmaDependency(DepToken); |
| 353 | } |
| 354 | }; |
| 355 | } // end anonymous namespace |
| 356 | |
| 357 | |
| 358 | /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas: |
| 359 | /// #pragma GCC poison/system_header/dependency and #pragma once. |
| 360 | void Preprocessor::RegisterBuiltinPragmas() { |
| 361 | AddPragmaHandler(0, new PragmaOnceHandler(getIdentifierInfo("once"))); |
| 362 | AddPragmaHandler("GCC", new PragmaPoisonHandler(getIdentifierInfo("poison"))); |
| 363 | AddPragmaHandler("GCC", new PragmaSystemHeaderHandler( |
| 364 | getIdentifierInfo("system_header"))); |
| 365 | AddPragmaHandler("GCC", new PragmaDependencyHandler( |
| 366 | getIdentifierInfo("dependency"))); |
| 367 | } |