Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 1 | //===- Pragma.cpp - Pragma registration and handling ----------------------===// |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 9 | // This file implements the PragmaHandler/PragmaTable interfaces and implements |
| 10 | // pragma related methods of the Preprocessor class. |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Lex/Pragma.h" |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileManager.h" |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 17 | #include "clang/Basic/IdentifierTable.h" |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 18 | #include "clang/Basic/LLVM.h" |
| 19 | #include "clang/Basic/LangOptions.h" |
| 20 | #include "clang/Basic/Module.h" |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceLocation.h" |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TokenKinds.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Lex/HeaderSearch.h" |
| 25 | #include "clang/Lex/LexDiagnostic.h" |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 26 | #include "clang/Lex/Lexer.h" |
Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 27 | #include "clang/Lex/LiteralSupport.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 28 | #include "clang/Lex/MacroInfo.h" |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 29 | #include "clang/Lex/ModuleLoader.h" |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 30 | #include "clang/Lex/PPCallbacks.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "clang/Lex/Preprocessor.h" |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 32 | #include "clang/Lex/PreprocessorLexer.h" |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 33 | #include "clang/Lex/Token.h" |
| 34 | #include "clang/Lex/TokenLexer.h" |
| 35 | #include "llvm/ADT/ArrayRef.h" |
| 36 | #include "llvm/ADT/DenseMap.h" |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/STLExtras.h" |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallString.h" |
| 39 | #include "llvm/ADT/SmallVector.h" |
Andrew V. Tischenko | c88deb1 | 2018-04-10 10:34:13 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/StringSwitch.h" |
Nico Weber | ade321e | 2018-04-10 18:53:28 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/StringRef.h" |
Andrew V. Tischenko | c88deb1 | 2018-04-10 10:34:13 +0000 | [diff] [blame] | 42 | #include "llvm/Support/CrashRecoveryContext.h" |
Nico Weber | ade321e | 2018-04-10 18:53:28 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Compiler.h" |
Daniel Dunbar | f2cf329 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 44 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 45 | #include <algorithm> |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 46 | #include <cassert> |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 47 | #include <cstddef> |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 48 | #include <cstdint> |
| 49 | #include <limits> |
| 50 | #include <string> |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 51 | #include <utility> |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 52 | #include <vector> |
| 53 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 54 | using namespace clang; |
| 55 | |
| 56 | // Out-of-line destructor to provide a home for the class. |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 57 | PragmaHandler::~PragmaHandler() = default; |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 2e15530 | 2006-07-03 05:34:41 +0000 | [diff] [blame] | 59 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d839e77 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 60 | // EmptyPragmaHandler Implementation. |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | |
Hans Wennborg | 7357bbc | 2015-10-12 20:47:58 +0000 | [diff] [blame] | 63 | EmptyPragmaHandler::EmptyPragmaHandler(StringRef Name) : PragmaHandler(Name) {} |
Daniel Dunbar | d839e77 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 64 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 65 | void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 66 | PragmaIntroducer Introducer, |
Douglas Gregor | c7d6576 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 67 | Token &FirstToken) {} |
Daniel Dunbar | d839e77 | 2010-06-11 20:10:12 +0000 | [diff] [blame] | 68 | |
| 69 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2e15530 | 2006-07-03 05:34:41 +0000 | [diff] [blame] | 70 | // PragmaNamespace Implementation. |
| 71 | //===----------------------------------------------------------------------===// |
| 72 | |
Chris Lattner | 2e15530 | 2006-07-03 05:34:41 +0000 | [diff] [blame] | 73 | PragmaNamespace::~PragmaNamespace() { |
Reid Kleckner | 588c937 | 2014-02-19 23:44:52 +0000 | [diff] [blame] | 74 | llvm::DeleteContainerSeconds(Handlers); |
Chris Lattner | 2e15530 | 2006-07-03 05:34:41 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /// FindHandler - Check to see if there is already a handler for the |
| 78 | /// specified name. If not, return the handler for the null identifier if it |
| 79 | /// exists, otherwise return null. If IgnoreNull is true (the default) then |
| 80 | /// the null handler isn't returned on failure to match. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 81 | PragmaHandler *PragmaNamespace::FindHandler(StringRef Name, |
Chris Lattner | 2e15530 | 2006-07-03 05:34:41 +0000 | [diff] [blame] | 82 | bool IgnoreNull) const { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 83 | if (PragmaHandler *Handler = Handlers.lookup(Name)) |
| 84 | return Handler; |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 85 | return IgnoreNull ? nullptr : Handlers.lookup(StringRef()); |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 86 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 88 | void PragmaNamespace::AddPragma(PragmaHandler *Handler) { |
| 89 | assert(!Handlers.lookup(Handler->getName()) && |
| 90 | "A handler with this name is already registered in this namespace"); |
David Blaikie | 13156b6 | 2014-11-19 03:06:06 +0000 | [diff] [blame] | 91 | Handlers[Handler->getName()] = Handler; |
Chris Lattner | 2e15530 | 2006-07-03 05:34:41 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 94 | void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 95 | assert(Handlers.lookup(Handler->getName()) && |
| 96 | "Handler not registered in this namespace"); |
| 97 | Handlers.erase(Handler->getName()); |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 100 | void PragmaNamespace::HandlePragma(Preprocessor &PP, |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 101 | PragmaIntroducer Introducer, Token &Tok) { |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 102 | // Read the 'namespace' that the directive is in, e.g. STDC. Do not macro |
| 103 | // expand it, the user can have a STDC #define, that should not affect this. |
| 104 | PP.LexUnexpandedToken(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 106 | // Get the handler for this token. If there is no handler, ignore the pragma. |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 107 | PragmaHandler *Handler |
| 108 | = FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName() |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 109 | : StringRef(), |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 110 | /*IgnoreNull=*/false); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 111 | if (!Handler) { |
Chris Lattner | 21656f2 | 2009-04-19 21:10:26 +0000 | [diff] [blame] | 112 | PP.Diag(Tok, diag::warn_pragma_ignored); |
| 113 | return; |
| 114 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 116 | // Otherwise, pass it down. |
Douglas Gregor | c7d6576 | 2010-09-09 22:45:38 +0000 | [diff] [blame] | 117 | Handler->HandlePragma(PP, Introducer, Tok); |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 118 | } |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 119 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 120 | //===----------------------------------------------------------------------===// |
| 121 | // Preprocessor Pragma Directive Handling. |
| 122 | //===----------------------------------------------------------------------===// |
| 123 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 124 | /// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 125 | /// rest of the pragma, passing it to the registered pragma handlers. |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 126 | void Preprocessor::HandlePragmaDirective(PragmaIntroducer Introducer) { |
Enea Zaffanella | 5afb04a | 2013-07-20 20:09:11 +0000 | [diff] [blame] | 127 | if (Callbacks) |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 128 | Callbacks->PragmaDirective(Introducer.Loc, Introducer.Kind); |
Enea Zaffanella | 5afb04a | 2013-07-20 20:09:11 +0000 | [diff] [blame] | 129 | |
Jordan Rose | de1a292 | 2012-06-08 18:06:21 +0000 | [diff] [blame] | 130 | if (!PragmasEnabled) |
| 131 | return; |
| 132 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 133 | ++NumPragma; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 135 | // Invoke the first level of pragma handlers which reads the namespace id. |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 136 | Token Tok; |
Enea Zaffanella | 5afb04a | 2013-07-20 20:09:11 +0000 | [diff] [blame] | 137 | PragmaHandlers->HandlePragma(*this, Introducer, Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 139 | // If the pragma handler didn't read the rest of the line, consume it now. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 140 | if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective()) |
Peter Collingbourne | 2c9f966 | 2011-02-22 13:49:00 +0000 | [diff] [blame] | 141 | || (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective)) |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 142 | DiscardUntilEndOfDirective(); |
| 143 | } |
| 144 | |
| 145 | /// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then |
| 146 | /// return the first token after the directive. The _Pragma token has just |
| 147 | /// been read into 'Tok'. |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 148 | void Preprocessor::Handle_Pragma(Token &Tok) { |
Richard Smith | 75f9681 | 2019-04-11 21:18:22 +0000 | [diff] [blame] | 149 | // C11 6.10.3.4/3: |
| 150 | // all pragma unary operator expressions within [a completely |
| 151 | // macro-replaced preprocessing token sequence] are [...] processed [after |
| 152 | // rescanning is complete] |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 153 | // |
Richard Smith | 75f9681 | 2019-04-11 21:18:22 +0000 | [diff] [blame] | 154 | // This means that we execute _Pragma operators in two cases: |
| 155 | // |
| 156 | // 1) on token sequences that would otherwise be produced as the output of |
| 157 | // phase 4 of preprocessing, and |
| 158 | // 2) on token sequences formed as the macro-replaced token sequence of a |
| 159 | // macro argument |
| 160 | // |
| 161 | // Case #2 appears to be a wording bug: only _Pragmas that would survive to |
| 162 | // the end of phase 4 should actually be executed. Discussion on the WG14 |
| 163 | // mailing list suggests that a _Pragma operator is notionally checked early, |
| 164 | // but only pragmas that survive to the end of phase 4 should be executed. |
| 165 | // |
| 166 | // In Case #2, we check the syntax now, but then put the tokens back into the |
| 167 | // token stream for later consumption. |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 168 | |
Richard Smith | 75f9681 | 2019-04-11 21:18:22 +0000 | [diff] [blame] | 169 | struct TokenCollector { |
| 170 | Preprocessor &Self; |
| 171 | bool Collect; |
| 172 | SmallVector<Token, 3> Tokens; |
| 173 | Token &Tok; |
| 174 | |
| 175 | void lex() { |
| 176 | if (Collect) |
| 177 | Tokens.push_back(Tok); |
| 178 | Self.Lex(Tok); |
| 179 | } |
| 180 | |
| 181 | void revert() { |
| 182 | assert(Collect && "did not collect tokens"); |
| 183 | assert(!Tokens.empty() && "collected unexpected number of tokens"); |
| 184 | |
| 185 | // Push the ( "string" ) tokens into the token stream. |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 186 | auto Toks = std::make_unique<Token[]>(Tokens.size()); |
Richard Smith | 75f9681 | 2019-04-11 21:18:22 +0000 | [diff] [blame] | 187 | std::copy(Tokens.begin() + 1, Tokens.end(), Toks.get()); |
| 188 | Toks[Tokens.size() - 1] = Tok; |
| 189 | Self.EnterTokenStream(std::move(Toks), Tokens.size(), |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame] | 190 | /*DisableMacroExpansion*/ true, |
| 191 | /*IsReinject*/ true); |
Richard Smith | 75f9681 | 2019-04-11 21:18:22 +0000 | [diff] [blame] | 192 | |
| 193 | // ... and return the _Pragma token unchanged. |
| 194 | Tok = *Tokens.begin(); |
| 195 | } |
| 196 | }; |
| 197 | |
| 198 | TokenCollector Toks = {*this, InMacroArgPreExpansion, {}, Tok}; |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 199 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 200 | // Remember the pragma token location. |
| 201 | SourceLocation PragmaLoc = Tok.getLocation(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 203 | // Read the '('. |
Richard Smith | 75f9681 | 2019-04-11 21:18:22 +0000 | [diff] [blame] | 204 | Toks.lex(); |
Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 205 | if (Tok.isNot(tok::l_paren)) { |
| 206 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
Richard Smith | 75f9681 | 2019-04-11 21:18:22 +0000 | [diff] [blame] | 207 | return; |
Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 208 | } |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 209 | |
| 210 | // Read the '"..."'. |
Richard Smith | 75f9681 | 2019-04-11 21:18:22 +0000 | [diff] [blame] | 211 | Toks.lex(); |
Richard Smith | c98bb4e | 2013-03-09 23:30:15 +0000 | [diff] [blame] | 212 | if (!tok::isStringLiteral(Tok.getKind())) { |
Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 213 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
Hubert Tong | 0deb694 | 2015-07-30 21:30:00 +0000 | [diff] [blame] | 214 | // Skip bad tokens, and the ')', if present. |
Reid Kleckner | 53e6a5d | 2014-08-14 19:47:06 +0000 | [diff] [blame] | 215 | if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eof)) |
Richard Smith | d67aea2 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 216 | Lex(Tok); |
Hubert Tong | 0deb694 | 2015-07-30 21:30:00 +0000 | [diff] [blame] | 217 | while (Tok.isNot(tok::r_paren) && |
| 218 | !Tok.isAtStartOfLine() && |
| 219 | Tok.isNot(tok::eof)) |
| 220 | Lex(Tok); |
Richard Smith | d67aea2 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 221 | if (Tok.is(tok::r_paren)) |
| 222 | Lex(Tok); |
Richard Smith | 75f9681 | 2019-04-11 21:18:22 +0000 | [diff] [blame] | 223 | return; |
Richard Smith | d67aea2 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | if (Tok.hasUDSuffix()) { |
| 227 | Diag(Tok, diag::err_invalid_string_udl); |
| 228 | // Skip this token, and the ')', if present. |
| 229 | Lex(Tok); |
| 230 | if (Tok.is(tok::r_paren)) |
| 231 | Lex(Tok); |
Richard Smith | 75f9681 | 2019-04-11 21:18:22 +0000 | [diff] [blame] | 232 | return; |
Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 233 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 234 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 235 | // Remember the string. |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 236 | Token StrTok = Tok; |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 237 | |
| 238 | // Read the ')'. |
Richard Smith | 75f9681 | 2019-04-11 21:18:22 +0000 | [diff] [blame] | 239 | Toks.lex(); |
Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 240 | if (Tok.isNot(tok::r_paren)) { |
| 241 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
Richard Smith | 75f9681 | 2019-04-11 21:18:22 +0000 | [diff] [blame] | 242 | return; |
Chris Lattner | 907dfe9 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 243 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
Richard Smith | 75f9681 | 2019-04-11 21:18:22 +0000 | [diff] [blame] | 245 | // If we're expanding a macro argument, put the tokens back. |
| 246 | if (InMacroArgPreExpansion) { |
| 247 | Toks.revert(); |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 248 | return; |
Richard Smith | 75f9681 | 2019-04-11 21:18:22 +0000 | [diff] [blame] | 249 | } |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 250 | |
Chris Lattner | 9dc9c20 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 251 | SourceLocation RParenLoc = Tok.getLocation(); |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 252 | std::string StrVal = getSpelling(StrTok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | |
Richard Smith | c98bb4e | 2013-03-09 23:30:15 +0000 | [diff] [blame] | 254 | // The _Pragma is lexically sound. Destringize according to C11 6.10.9.1: |
| 255 | // "The string literal is destringized by deleting any encoding prefix, |
Chris Lattner | 262d4e3 | 2009-01-16 18:59:23 +0000 | [diff] [blame] | 256 | // deleting the leading and trailing double-quotes, replacing each escape |
| 257 | // sequence \" by a double-quote, and replacing each escape sequence \\ by a |
| 258 | // single backslash." |
Richard Smith | c98bb4e | 2013-03-09 23:30:15 +0000 | [diff] [blame] | 259 | if (StrVal[0] == 'L' || StrVal[0] == 'U' || |
| 260 | (StrVal[0] == 'u' && StrVal[1] != '8')) |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 261 | StrVal.erase(StrVal.begin()); |
Richard Smith | c98bb4e | 2013-03-09 23:30:15 +0000 | [diff] [blame] | 262 | else if (StrVal[0] == 'u') |
| 263 | StrVal.erase(StrVal.begin(), StrVal.begin() + 2); |
| 264 | |
| 265 | if (StrVal[0] == 'R') { |
| 266 | // FIXME: C++11 does not specify how to handle raw-string-literals here. |
| 267 | // We strip off the 'R', the quotes, the d-char-sequences, and the parens. |
| 268 | assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' && |
| 269 | "Invalid raw string token!"); |
| 270 | |
| 271 | // Measure the length of the d-char-sequence. |
| 272 | unsigned NumDChars = 0; |
| 273 | while (StrVal[2 + NumDChars] != '(') { |
| 274 | assert(NumDChars < (StrVal.size() - 5) / 2 && |
| 275 | "Invalid raw string token!"); |
| 276 | ++NumDChars; |
| 277 | } |
| 278 | assert(StrVal[StrVal.size() - 2 - NumDChars] == ')'); |
| 279 | |
| 280 | // Remove 'R " d-char-sequence' and 'd-char-sequence "'. We'll replace the |
| 281 | // parens below. |
| 282 | StrVal.erase(0, 2 + NumDChars); |
| 283 | StrVal.erase(StrVal.size() - 1 - NumDChars); |
| 284 | } else { |
| 285 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 286 | "Invalid string token!"); |
| 287 | |
| 288 | // Remove escaped quotes and escapes. |
Benjamin Kramer | c2f5f29 | 2013-05-04 10:37:20 +0000 | [diff] [blame] | 289 | unsigned ResultPos = 1; |
Erik Verbruggen | e4fd652 | 2016-10-26 13:06:13 +0000 | [diff] [blame] | 290 | for (size_t i = 1, e = StrVal.size() - 1; i != e; ++i) { |
Reid Kleckner | 95e036c | 2013-09-25 16:42:48 +0000 | [diff] [blame] | 291 | // Skip escapes. \\ -> '\' and \" -> '"'. |
| 292 | if (StrVal[i] == '\\' && i + 1 < e && |
| 293 | (StrVal[i + 1] == '\\' || StrVal[i + 1] == '"')) |
| 294 | ++i; |
| 295 | StrVal[ResultPos++] = StrVal[i]; |
Richard Smith | c98bb4e | 2013-03-09 23:30:15 +0000 | [diff] [blame] | 296 | } |
Reid Kleckner | 95e036c | 2013-09-25 16:42:48 +0000 | [diff] [blame] | 297 | StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 1); |
Richard Smith | c98bb4e | 2013-03-09 23:30:15 +0000 | [diff] [blame] | 298 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 300 | // Remove the front quote, replacing it with a space, so that the pragma |
| 301 | // contents appear to have a space before them. |
| 302 | StrVal[0] = ' '; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Chris Lattner | fa217bd | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 304 | // Replace the terminating quote with a \n. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 305 | StrVal[StrVal.size()-1] = '\n'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | |
Peter Collingbourne | f29ce97 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 307 | // Plop the string (including the newline and trailing null) into a buffer |
| 308 | // where we can lex it. |
| 309 | Token TmpTok; |
| 310 | TmpTok.startToken(); |
Dmitri Gribenko | b8e9e75 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 311 | CreateString(StrVal, TmpTok); |
Peter Collingbourne | f29ce97 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 312 | SourceLocation TokLoc = TmpTok.getLocation(); |
| 313 | |
| 314 | // Make and enter a lexer object so that we lex and expand the tokens just |
| 315 | // like any others. |
| 316 | Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc, |
| 317 | StrVal.size(), *this); |
| 318 | |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 319 | EnterSourceFileWithLexer(TL, nullptr); |
Peter Collingbourne | f29ce97 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 320 | |
| 321 | // With everything set up, lex this as a #pragma directive. |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 322 | HandlePragmaDirective({PIK__Pragma, PragmaLoc}); |
John McCall | 89e925d | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 323 | |
| 324 | // Finally, return whatever came after the pragma directive. |
| 325 | return Lex(Tok); |
| 326 | } |
| 327 | |
| 328 | /// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text |
| 329 | /// is not enclosed within a string literal. |
| 330 | void Preprocessor::HandleMicrosoft__pragma(Token &Tok) { |
| 331 | // Remember the pragma token location. |
| 332 | SourceLocation PragmaLoc = Tok.getLocation(); |
| 333 | |
| 334 | // Read the '('. |
| 335 | Lex(Tok); |
| 336 | if (Tok.isNot(tok::l_paren)) { |
| 337 | Diag(PragmaLoc, diag::err__Pragma_malformed); |
| 338 | return; |
| 339 | } |
| 340 | |
Peter Collingbourne | f29ce97 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 341 | // Get the tokens enclosed within the __pragma(), as well as the final ')'. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 342 | SmallVector<Token, 32> PragmaToks; |
John McCall | 89e925d | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 343 | int NumParens = 0; |
| 344 | Lex(Tok); |
| 345 | while (Tok.isNot(tok::eof)) { |
Peter Collingbourne | f29ce97 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 346 | PragmaToks.push_back(Tok); |
John McCall | 89e925d | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 347 | if (Tok.is(tok::l_paren)) |
| 348 | NumParens++; |
| 349 | else if (Tok.is(tok::r_paren) && NumParens-- == 0) |
| 350 | break; |
John McCall | 89e925d | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 351 | Lex(Tok); |
| 352 | } |
| 353 | |
John McCall | 49039d4 | 2010-08-29 01:09:54 +0000 | [diff] [blame] | 354 | if (Tok.is(tok::eof)) { |
| 355 | Diag(PragmaLoc, diag::err_unterminated___pragma); |
| 356 | return; |
| 357 | } |
| 358 | |
Peter Collingbourne | f29ce97 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 359 | PragmaToks.front().setFlag(Token::LeadingSpace); |
John McCall | 89e925d | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 360 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 361 | // Replace the ')' with an EOD to mark the end of the pragma. |
| 362 | PragmaToks.back().setKind(tok::eod); |
Peter Collingbourne | f29ce97 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 363 | |
| 364 | Token *TokArray = new Token[PragmaToks.size()]; |
| 365 | std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray); |
| 366 | |
| 367 | // Push the tokens onto the stack. |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame] | 368 | EnterTokenStream(TokArray, PragmaToks.size(), true, true, |
| 369 | /*IsReinject*/ false); |
Peter Collingbourne | f29ce97 | 2011-02-22 13:49:06 +0000 | [diff] [blame] | 370 | |
| 371 | // With everything set up, lex this as a #pragma directive. |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 372 | HandlePragmaDirective({PIK___pragma, PragmaLoc}); |
John McCall | 89e925d | 2010-08-28 22:34:47 +0000 | [diff] [blame] | 373 | |
| 374 | // Finally, return whatever came after the pragma directive. |
| 375 | return Lex(Tok); |
| 376 | } |
| 377 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 378 | /// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'. |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 379 | void Preprocessor::HandlePragmaOnce(Token &OnceTok) { |
Sunil Srivastava | fe58327 | 2016-07-25 17:17:06 +0000 | [diff] [blame] | 380 | // Don't honor the 'once' when handling the primary source file, unless |
Erik Verbruggen | e0bde75 | 2016-10-27 14:17:10 +0000 | [diff] [blame] | 381 | // this is a prefix to a TU, which indicates we're generating a PCH file, or |
| 382 | // when the main file is a header (e.g. when -xc-header is provided on the |
| 383 | // commandline). |
| 384 | if (isInPrimaryFile() && TUKind != TU_Prefix && !getLangOpts().IsHeaderFile) { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 385 | Diag(OnceTok, diag::pp_pragma_once_in_main_file); |
| 386 | return; |
| 387 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 389 | // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 390 | // Mark the file as a once-only file now. |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 391 | HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry()); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Chris Lattner | c238331 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 394 | void Preprocessor::HandlePragmaMark() { |
Ted Kremenek | 76c3441 | 2008-11-19 22:21:33 +0000 | [diff] [blame] | 395 | assert(CurPPLexer && "No current lexer?"); |
Erich Keane | 0a6b5b6 | 2018-12-04 14:34:09 +0000 | [diff] [blame] | 396 | CurLexer->ReadToEndOfLine(); |
Chris Lattner | c238331 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 397 | } |
| 398 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 399 | /// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'. |
Erik Verbruggen | 851ce0e | 2016-10-26 11:46:10 +0000 | [diff] [blame] | 400 | void Preprocessor::HandlePragmaPoison() { |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 401 | Token Tok; |
Chris Lattner | 538d7f3 | 2006-07-20 04:31:52 +0000 | [diff] [blame] | 402 | |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 403 | while (true) { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 404 | // Read the next token to poison. While doing this, pretend that we are |
| 405 | // skipping while reading the identifier to poison. |
| 406 | // This avoids errors on code like: |
| 407 | // #pragma GCC poison X |
| 408 | // #pragma GCC poison X |
Ted Kremenek | 551c82a | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 409 | if (CurPPLexer) CurPPLexer->LexingRawMode = true; |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 410 | LexUnexpandedToken(Tok); |
Ted Kremenek | 551c82a | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 411 | if (CurPPLexer) CurPPLexer->LexingRawMode = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 413 | // If we reached the end of line, we're done. |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 414 | if (Tok.is(tok::eod)) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 416 | // Can only poison identifiers. |
Abramo Bagnara | ea4f7c7 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 417 | if (Tok.isNot(tok::raw_identifier)) { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 418 | Diag(Tok, diag::err_pp_invalid_poison); |
| 419 | return; |
| 420 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 422 | // Look up the identifier info for the token. We disabled identifier lookup |
| 423 | // by saying we're skipping contents, so we need to do this manually. |
| 424 | IdentifierInfo *II = LookUpIdentifierInfo(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 426 | // Already poisoned. |
| 427 | if (II->isPoisoned()) continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 429 | // If this is a macro identifier, emit a warning. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 430 | if (isMacroDefined(II)) |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 431 | Diag(Tok, diag::pp_poisoning_existing_macro); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 433 | // Finally, poison it! |
| 434 | II->setIsPoisoned(); |
Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 435 | if (II->isFromAST()) |
| 436 | II->setChangedSinceDeserialization(); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 440 | /// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 441 | /// that the whole directive has been parsed. |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 442 | void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 443 | if (isInPrimaryFile()) { |
| 444 | Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file); |
| 445 | return; |
| 446 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 448 | // Get the current file lexer we're looking at. Ignore _Pragma 'files' etc. |
Ted Kremenek | 300590b | 2008-11-20 01:45:11 +0000 | [diff] [blame] | 449 | PreprocessorLexer *TheLexer = getCurrentFileLexer(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 451 | // Mark the file as a system header. |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 452 | HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
Chris Lattner | d9efb6e | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 454 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation()); |
Douglas Gregor | 453b012 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 455 | if (PLoc.isInvalid()) |
| 456 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 457 | |
Jay Foad | 9a6b098 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 458 | unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
Chris Lattner | 3bdc767 | 2011-05-22 22:10:16 +0000 | [diff] [blame] | 460 | // Notify the client, if desired, that we are in a new source file. |
| 461 | if (Callbacks) |
| 462 | Callbacks->FileChanged(SysHeaderTok.getLocation(), |
| 463 | PPCallbacks::SystemHeaderPragma, SrcMgr::C_System); |
| 464 | |
Chris Lattner | d9efb6e | 2009-06-15 05:02:34 +0000 | [diff] [blame] | 465 | // Emit a line marker. This will change any source locations from this point |
| 466 | // forward to realize they are in a system header. |
| 467 | // Create a line note with this information. |
Reid Kleckner | eb00ee0 | 2017-05-22 21:42:58 +0000 | [diff] [blame] | 468 | SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine() + 1, |
Jordan Rose | 111c4a6 | 2013-04-17 19:09:18 +0000 | [diff] [blame] | 469 | FilenameID, /*IsEntry=*/false, /*IsExit=*/false, |
Reid Kleckner | eb00ee0 | 2017-05-22 21:42:58 +0000 | [diff] [blame] | 470 | SrcMgr::C_System); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 471 | } |
| 472 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 473 | /// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah. |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 474 | void Preprocessor::HandlePragmaDependency(Token &DependencyTok) { |
| 475 | Token FilenameTok; |
Richard Smith | b9b0510 | 2019-03-19 01:51:19 +0000 | [diff] [blame] | 476 | if (LexHeaderName(FilenameTok, /*AllowConcatenation*/false)) |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 477 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | |
Richard Smith | b9b0510 | 2019-03-19 01:51:19 +0000 | [diff] [blame] | 479 | // If the next token wasn't a header-name, diagnose the error. |
Richard Smith | 91e150d | 2019-03-19 22:09:55 +0000 | [diff] [blame] | 480 | if (FilenameTok.isNot(tok::header_name)) { |
Richard Smith | b9b0510 | 2019-03-19 01:51:19 +0000 | [diff] [blame] | 481 | Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename); |
| 482 | return; |
| 483 | } |
| 484 | |
Chris Lattner | c07ba1f | 2006-10-30 05:58:32 +0000 | [diff] [blame] | 485 | // Reserve a buffer to get the spelling. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 486 | SmallString<128> FilenameBuffer; |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 487 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 488 | StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid); |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 489 | if (Invalid) |
| 490 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 492 | bool isAngled = |
| 493 | GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename); |
Chris Lattner | c07ba1f | 2006-10-30 05:58:32 +0000 | [diff] [blame] | 494 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 495 | // error. |
Chris Lattner | d081f8c | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 496 | if (Filename.empty()) |
Chris Lattner | c07ba1f | 2006-10-30 05:58:32 +0000 | [diff] [blame] | 497 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 499 | // Search include directories for this file. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 500 | const DirectoryLookup *CurDir; |
Alex Lorenz | 4dc5573 | 2019-08-22 18:15:50 +0000 | [diff] [blame] | 501 | Optional<FileEntryRef> File = |
Richard Smith | 25d5075 | 2014-10-20 00:15:49 +0000 | [diff] [blame] | 502 | LookupFile(FilenameTok.getLocation(), Filename, isAngled, nullptr, |
Volodymyr Sapsai | 421380a | 2019-02-05 22:34:55 +0000 | [diff] [blame] | 503 | nullptr, CurDir, nullptr, nullptr, nullptr, nullptr, nullptr); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 504 | if (!File) { |
Eli Friedman | 3781a36 | 2011-08-30 23:07:51 +0000 | [diff] [blame] | 505 | if (!SuppressIncludeNotFoundError) |
| 506 | Diag(FilenameTok, diag::err_pp_file_not_found) << Filename; |
Chris Lattner | 97b8e84 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 507 | return; |
| 508 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 510 | const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry(); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 511 | |
| 512 | // If this file is older than the file it depends on, emit a diagnostic. |
| 513 | if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) { |
| 514 | // Lex tokens at the end of the message and include them in the message. |
| 515 | std::string Message; |
| 516 | Lex(DependencyTok); |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 517 | while (DependencyTok.isNot(tok::eod)) { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 518 | Message += getSpelling(DependencyTok) + " "; |
| 519 | Lex(DependencyTok); |
| 520 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 521 | |
Chris Lattner | f0b0497 | 2010-09-05 23:16:09 +0000 | [diff] [blame] | 522 | // Remove the trailing ' ' if present. |
| 523 | if (!Message.empty()) |
| 524 | Message.erase(Message.end()-1); |
Chris Lattner | 97b8e84 | 2008-11-18 08:02:48 +0000 | [diff] [blame] | 525 | Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message; |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Reid Kleckner | 002562a | 2013-05-06 21:02:12 +0000 | [diff] [blame] | 529 | /// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro. |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 530 | /// Return the IdentifierInfo* associated with the macro to push or pop. |
| 531 | IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) { |
| 532 | // Remember the pragma token location. |
| 533 | Token PragmaTok = Tok; |
| 534 | |
| 535 | // Read the '('. |
| 536 | Lex(Tok); |
| 537 | if (Tok.isNot(tok::l_paren)) { |
| 538 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 539 | << getSpelling(PragmaTok); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 540 | return nullptr; |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | // Read the macro name string. |
| 544 | Lex(Tok); |
| 545 | if (Tok.isNot(tok::string_literal)) { |
| 546 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 547 | << getSpelling(PragmaTok); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 548 | return nullptr; |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Richard Smith | d67aea2 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 551 | if (Tok.hasUDSuffix()) { |
| 552 | Diag(Tok, diag::err_invalid_string_udl); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 553 | return nullptr; |
Richard Smith | d67aea2 | 2012-03-06 03:21:47 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 556 | // Remember the macro string. |
| 557 | std::string StrVal = getSpelling(Tok); |
| 558 | |
| 559 | // Read the ')'. |
| 560 | Lex(Tok); |
| 561 | if (Tok.isNot(tok::r_paren)) { |
| 562 | Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed) |
| 563 | << getSpelling(PragmaTok); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 564 | return nullptr; |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' && |
| 568 | "Invalid string token!"); |
| 569 | |
| 570 | // Create a Token from the string. |
| 571 | Token MacroTok; |
| 572 | MacroTok.startToken(); |
Abramo Bagnara | ea4f7c7 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 573 | MacroTok.setKind(tok::raw_identifier); |
Dmitri Gribenko | b8e9e75 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 574 | CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok); |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 575 | |
| 576 | // Get the IdentifierInfo of MacroToPushTok. |
| 577 | return LookUpIdentifierInfo(MacroTok); |
| 578 | } |
| 579 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 580 | /// Handle \#pragma push_macro. |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 581 | /// |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 582 | /// The syntax is: |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 583 | /// \code |
Dmitri Gribenko | 9ebd161 | 2012-11-30 20:04:39 +0000 | [diff] [blame] | 584 | /// #pragma push_macro("macro") |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 585 | /// \endcode |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 586 | void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) { |
| 587 | // Parse the pragma directive and get the macro IdentifierInfo*. |
| 588 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok); |
| 589 | if (!IdentInfo) return; |
| 590 | |
| 591 | // Get the MacroInfo associated with IdentInfo. |
| 592 | MacroInfo *MI = getMacroInfo(IdentInfo); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 593 | |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 594 | if (MI) { |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 595 | // Allow the original MacroInfo to be redefined later. |
| 596 | MI->setIsAllowRedefinitionsWithoutWarning(true); |
| 597 | } |
| 598 | |
| 599 | // Push the cloned MacroInfo so we can retrieve it later. |
Argyrios Kyrtzidis | 09c9e81 | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 600 | PragmaPushMacroInfo[IdentInfo].push_back(MI); |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 603 | /// Handle \#pragma pop_macro. |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 604 | /// |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 605 | /// The syntax is: |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 606 | /// \code |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 607 | /// #pragma pop_macro("macro") |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 608 | /// \endcode |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 609 | void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) { |
| 610 | SourceLocation MessageLoc = PopMacroTok.getLocation(); |
| 611 | |
| 612 | // Parse the pragma directive and get the macro IdentifierInfo*. |
| 613 | IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok); |
| 614 | if (!IdentInfo) return; |
| 615 | |
| 616 | // Find the vector<MacroInfo*> associated with the macro. |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 617 | llvm::DenseMap<IdentifierInfo *, std::vector<MacroInfo *>>::iterator iter = |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 618 | PragmaPushMacroInfo.find(IdentInfo); |
| 619 | if (iter != PragmaPushMacroInfo.end()) { |
Alexander Kornienko | 8b3f623 | 2012-08-29 00:20:03 +0000 | [diff] [blame] | 620 | // Forget the MacroInfo currently associated with IdentInfo. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 621 | if (MacroInfo *MI = getMacroInfo(IdentInfo)) { |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 622 | if (MI->isWarnIfUnused()) |
| 623 | WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); |
| 624 | appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc)); |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 625 | } |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 626 | |
| 627 | // Get the MacroInfo we want to reinstall. |
| 628 | MacroInfo *MacroToReInstall = iter->second.back(); |
| 629 | |
Richard Smith | 713369b | 2015-04-23 20:40:50 +0000 | [diff] [blame] | 630 | if (MacroToReInstall) |
Alexander Kornienko | c0b4928 | 2012-08-29 16:56:24 +0000 | [diff] [blame] | 631 | // Reinstall the previously pushed macro. |
Richard Smith | 713369b | 2015-04-23 20:40:50 +0000 | [diff] [blame] | 632 | appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc); |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 633 | |
| 634 | // Pop PragmaPushMacroInfo stack. |
| 635 | iter->second.pop_back(); |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 636 | if (iter->second.empty()) |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 637 | PragmaPushMacroInfo.erase(iter); |
| 638 | } else { |
| 639 | Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push) |
| 640 | << IdentInfo->getName(); |
| 641 | } |
| 642 | } |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 643 | |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 644 | void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 645 | // We will either get a quoted filename or a bracketed filename, and we |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 646 | // have to track which we got. The first filename is the source name, |
| 647 | // and the second name is the mapped filename. If the first is quoted, |
| 648 | // the second must be as well (cannot mix and match quotes and brackets). |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 649 | |
| 650 | // Get the open paren |
| 651 | Lex(Tok); |
| 652 | if (Tok.isNot(tok::l_paren)) { |
| 653 | Diag(Tok, diag::warn_pragma_include_alias_expected) << "("; |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | // We expect either a quoted string literal, or a bracketed name |
| 658 | Token SourceFilenameTok; |
Richard Smith | b9b0510 | 2019-03-19 01:51:19 +0000 | [diff] [blame] | 659 | if (LexHeaderName(SourceFilenameTok)) |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 660 | return; |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 661 | |
| 662 | StringRef SourceFileName; |
| 663 | SmallString<128> FileNameBuffer; |
Richard Smith | 91e150d | 2019-03-19 22:09:55 +0000 | [diff] [blame] | 664 | if (SourceFilenameTok.is(tok::header_name)) { |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 665 | SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer); |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 666 | } else { |
| 667 | Diag(Tok, diag::warn_pragma_include_alias_expected_filename); |
| 668 | return; |
| 669 | } |
| 670 | FileNameBuffer.clear(); |
| 671 | |
| 672 | // Now we expect a comma, followed by another include name |
| 673 | Lex(Tok); |
| 674 | if (Tok.isNot(tok::comma)) { |
| 675 | Diag(Tok, diag::warn_pragma_include_alias_expected) << ","; |
| 676 | return; |
| 677 | } |
| 678 | |
| 679 | Token ReplaceFilenameTok; |
Richard Smith | b9b0510 | 2019-03-19 01:51:19 +0000 | [diff] [blame] | 680 | if (LexHeaderName(ReplaceFilenameTok)) |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 681 | return; |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 682 | |
| 683 | StringRef ReplaceFileName; |
Richard Smith | 91e150d | 2019-03-19 22:09:55 +0000 | [diff] [blame] | 684 | if (ReplaceFilenameTok.is(tok::header_name)) { |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 685 | ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer); |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 686 | } else { |
| 687 | Diag(Tok, diag::warn_pragma_include_alias_expected_filename); |
| 688 | return; |
| 689 | } |
| 690 | |
| 691 | // Finally, we expect the closing paren |
| 692 | Lex(Tok); |
| 693 | if (Tok.isNot(tok::r_paren)) { |
| 694 | Diag(Tok, diag::warn_pragma_include_alias_expected) << ")"; |
| 695 | return; |
| 696 | } |
| 697 | |
| 698 | // Now that we have the source and target filenames, we need to make sure |
| 699 | // they're both of the same type (angled vs non-angled) |
| 700 | StringRef OriginalSource = SourceFileName; |
| 701 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 702 | bool SourceIsAngled = |
| 703 | GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(), |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 704 | SourceFileName); |
| 705 | bool ReplaceIsAngled = |
| 706 | GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(), |
| 707 | ReplaceFileName); |
| 708 | if (!SourceFileName.empty() && !ReplaceFileName.empty() && |
| 709 | (SourceIsAngled != ReplaceIsAngled)) { |
| 710 | unsigned int DiagID; |
| 711 | if (SourceIsAngled) |
| 712 | DiagID = diag::warn_pragma_include_alias_mismatch_angle; |
| 713 | else |
| 714 | DiagID = diag::warn_pragma_include_alias_mismatch_quote; |
| 715 | |
| 716 | Diag(SourceFilenameTok.getLocation(), DiagID) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 717 | << SourceFileName |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 718 | << ReplaceFileName; |
| 719 | |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | // Now we can let the include handler know about this mapping |
| 724 | getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName); |
| 725 | } |
| 726 | |
Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 727 | // Lex a component of a module name: either an identifier or a string literal; |
| 728 | // for components that can be expressed both ways, the two forms are equivalent. |
| 729 | static bool LexModuleNameComponent( |
| 730 | Preprocessor &PP, Token &Tok, |
| 731 | std::pair<IdentifierInfo *, SourceLocation> &ModuleNameComponent, |
| 732 | bool First) { |
| 733 | PP.LexUnexpandedToken(Tok); |
| 734 | if (Tok.is(tok::string_literal) && !Tok.hasUDSuffix()) { |
| 735 | StringLiteralParser Literal(Tok, PP); |
| 736 | if (Literal.hadError) |
| 737 | return true; |
| 738 | ModuleNameComponent = std::make_pair( |
| 739 | PP.getIdentifierInfo(Literal.GetString()), Tok.getLocation()); |
| 740 | } else if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) { |
| 741 | ModuleNameComponent = |
| 742 | std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 743 | } else { |
| 744 | PP.Diag(Tok.getLocation(), diag::err_pp_expected_module_name) << First; |
| 745 | return true; |
| 746 | } |
| 747 | return false; |
| 748 | } |
| 749 | |
| 750 | static bool LexModuleName( |
| 751 | Preprocessor &PP, Token &Tok, |
| 752 | llvm::SmallVectorImpl<std::pair<IdentifierInfo *, SourceLocation>> |
| 753 | &ModuleName) { |
| 754 | while (true) { |
| 755 | std::pair<IdentifierInfo*, SourceLocation> NameComponent; |
| 756 | if (LexModuleNameComponent(PP, Tok, NameComponent, ModuleName.empty())) |
| 757 | return true; |
| 758 | ModuleName.push_back(NameComponent); |
| 759 | |
| 760 | PP.LexUnexpandedToken(Tok); |
| 761 | if (Tok.isNot(tok::period)) |
| 762 | return false; |
| 763 | } |
| 764 | } |
| 765 | |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 766 | void Preprocessor::HandlePragmaModuleBuild(Token &Tok) { |
| 767 | SourceLocation Loc = Tok.getLocation(); |
| 768 | |
Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 769 | std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc; |
| 770 | if (LexModuleNameComponent(*this, Tok, ModuleNameLoc, true)) |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 771 | return; |
Richard Smith | 9565c75b | 2017-06-19 23:09:36 +0000 | [diff] [blame] | 772 | IdentifierInfo *ModuleName = ModuleNameLoc.first; |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 773 | |
| 774 | LexUnexpandedToken(Tok); |
| 775 | if (Tok.isNot(tok::eod)) { |
| 776 | Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 777 | DiscardUntilEndOfDirective(); |
| 778 | } |
| 779 | |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 780 | CurLexer->LexingRawMode = true; |
| 781 | |
| 782 | auto TryConsumeIdentifier = [&](StringRef Ident) -> bool { |
| 783 | if (Tok.getKind() != tok::raw_identifier || |
| 784 | Tok.getRawIdentifier() != Ident) |
| 785 | return false; |
| 786 | CurLexer->Lex(Tok); |
| 787 | return true; |
| 788 | }; |
| 789 | |
| 790 | // Scan forward looking for the end of the module. |
| 791 | const char *Start = CurLexer->getBufferLocation(); |
| 792 | const char *End = nullptr; |
| 793 | unsigned NestingLevel = 1; |
| 794 | while (true) { |
| 795 | End = CurLexer->getBufferLocation(); |
| 796 | CurLexer->Lex(Tok); |
| 797 | |
| 798 | if (Tok.is(tok::eof)) { |
| 799 | Diag(Loc, diag::err_pp_module_build_missing_end); |
| 800 | break; |
| 801 | } |
| 802 | |
| 803 | if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) { |
| 804 | // Token was part of module; keep going. |
| 805 | continue; |
| 806 | } |
| 807 | |
| 808 | // We hit something directive-shaped; check to see if this is the end |
| 809 | // of the module build. |
| 810 | CurLexer->ParsingPreprocessorDirective = true; |
| 811 | CurLexer->Lex(Tok); |
| 812 | if (TryConsumeIdentifier("pragma") && TryConsumeIdentifier("clang") && |
| 813 | TryConsumeIdentifier("module")) { |
| 814 | if (TryConsumeIdentifier("build")) |
| 815 | // #pragma clang module build -> entering a nested module build. |
| 816 | ++NestingLevel; |
| 817 | else if (TryConsumeIdentifier("endbuild")) { |
| 818 | // #pragma clang module endbuild -> leaving a module build. |
| 819 | if (--NestingLevel == 0) |
| 820 | break; |
| 821 | } |
| 822 | // We should either be looking at the EOD or more of the current directive |
| 823 | // preceding the EOD. Either way we can ignore this token and keep going. |
| 824 | assert(Tok.getKind() != tok::eof && "missing EOD before EOF"); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | CurLexer->LexingRawMode = false; |
| 829 | |
| 830 | // Load the extracted text as a preprocessed module. |
| 831 | assert(CurLexer->getBuffer().begin() <= Start && |
| 832 | Start <= CurLexer->getBuffer().end() && |
| 833 | CurLexer->getBuffer().begin() <= End && |
| 834 | End <= CurLexer->getBuffer().end() && |
| 835 | "module source range not contained within same file buffer"); |
| 836 | TheModuleLoader.loadModuleFromSource(Loc, ModuleName->getName(), |
| 837 | StringRef(Start, End - Start)); |
| 838 | } |
| 839 | |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 840 | void Preprocessor::HandlePragmaHdrstop(Token &Tok) { |
| 841 | Lex(Tok); |
| 842 | if (Tok.is(tok::l_paren)) { |
| 843 | Diag(Tok.getLocation(), diag::warn_pp_hdrstop_filename_ignored); |
| 844 | |
| 845 | std::string FileName; |
| 846 | if (!LexStringLiteral(Tok, FileName, "pragma hdrstop", false)) |
| 847 | return; |
| 848 | |
| 849 | if (Tok.isNot(tok::r_paren)) { |
| 850 | Diag(Tok, diag::err_expected) << tok::r_paren; |
| 851 | return; |
| 852 | } |
| 853 | Lex(Tok); |
| 854 | } |
| 855 | if (Tok.isNot(tok::eod)) |
| 856 | Diag(Tok.getLocation(), diag::ext_pp_extra_tokens_at_eol) |
| 857 | << "pragma hdrstop"; |
| 858 | |
| 859 | if (creatingPCHWithPragmaHdrStop() && |
| 860 | SourceMgr.isInMainFile(Tok.getLocation())) { |
| 861 | assert(CurLexer && "no lexer for #pragma hdrstop processing"); |
| 862 | Token &Result = Tok; |
| 863 | Result.startToken(); |
| 864 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof); |
| 865 | CurLexer->cutOffLexing(); |
| 866 | } |
| 867 | if (usingPCHWithPragmaHdrStop()) |
| 868 | SkippingUntilPragmaHdrStop = false; |
| 869 | } |
| 870 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 871 | /// AddPragmaHandler - Add the specified pragma handler to the preprocessor. |
| 872 | /// If 'Namespace' is non-null, then it is a token required to exist on the |
| 873 | /// pragma line before the pragma string starts, e.g. "STDC" or "GCC". |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 874 | void Preprocessor::AddPragmaHandler(StringRef Namespace, |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 875 | PragmaHandler *Handler) { |
Craig Topper | be25030 | 2014-09-12 05:19:24 +0000 | [diff] [blame] | 876 | PragmaNamespace *InsertNS = PragmaHandlers.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 877 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 878 | // If this is specified to be in a namespace, step down into it. |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 879 | if (!Namespace.empty()) { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 880 | // If there is already a pragma handler with the name of this namespace, |
| 881 | // we either have an error (directive with the same name as a namespace) or |
| 882 | // we already have the namespace to insert into. |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 883 | if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 884 | InsertNS = Existing->getIfNamespace(); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 885 | assert(InsertNS != nullptr && "Cannot have a pragma namespace and pragma" |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 886 | " handler with the same name!"); |
| 887 | } else { |
| 888 | // Otherwise, this namespace doesn't exist yet, create and insert the |
| 889 | // handler for it. |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 890 | InsertNS = new PragmaNamespace(Namespace); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 891 | PragmaHandlers->AddPragma(InsertNS); |
| 892 | } |
| 893 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 895 | // Check to make sure we don't already have a pragma for this identifier. |
| 896 | assert(!InsertNS->FindHandler(Handler->getName()) && |
| 897 | "Pragma handler already exists for this identifier!"); |
| 898 | InsertNS->AddPragma(Handler); |
| 899 | } |
| 900 | |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 901 | /// RemovePragmaHandler - Remove the specific pragma handler from the |
| 902 | /// preprocessor. If \arg Namespace is non-null, then it should be the |
| 903 | /// namespace that \arg Handler was added to. It is an error to remove |
| 904 | /// a handler that has not been registered. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 905 | void Preprocessor::RemovePragmaHandler(StringRef Namespace, |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 906 | PragmaHandler *Handler) { |
Craig Topper | be25030 | 2014-09-12 05:19:24 +0000 | [diff] [blame] | 907 | PragmaNamespace *NS = PragmaHandlers.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 908 | |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 909 | // If this is specified to be in a namespace, step down into it. |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 910 | if (!Namespace.empty()) { |
| 911 | PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace); |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 912 | assert(Existing && "Namespace containing handler does not exist!"); |
| 913 | |
| 914 | NS = Existing->getIfNamespace(); |
| 915 | assert(NS && "Invalid namespace, registered as a regular pragma handler!"); |
| 916 | } |
| 917 | |
| 918 | NS->RemovePragmaHandler(Handler); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 919 | |
Craig Topper | be25030 | 2014-09-12 05:19:24 +0000 | [diff] [blame] | 920 | // If this is a non-default namespace and it is now empty, remove it. |
| 921 | if (NS != PragmaHandlers.get() && NS->IsEmpty()) { |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 922 | PragmaHandlers->RemovePragmaHandler(NS); |
Argyrios Kyrtzidis | 7ce7526 | 2012-01-06 00:22:09 +0000 | [diff] [blame] | 923 | delete NS; |
| 924 | } |
Daniel Dunbar | 4059653 | 2008-10-04 19:17:46 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Peter Collingbourne | 3bffa52 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 927 | bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) { |
| 928 | Token Tok; |
| 929 | LexUnexpandedToken(Tok); |
| 930 | |
| 931 | if (Tok.isNot(tok::identifier)) { |
| 932 | Diag(Tok, diag::ext_on_off_switch_syntax); |
| 933 | return true; |
| 934 | } |
| 935 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 936 | if (II->isStr("ON")) |
| 937 | Result = tok::OOS_ON; |
| 938 | else if (II->isStr("OFF")) |
| 939 | Result = tok::OOS_OFF; |
| 940 | else if (II->isStr("DEFAULT")) |
| 941 | Result = tok::OOS_DEFAULT; |
| 942 | else { |
| 943 | Diag(Tok, diag::ext_on_off_switch_syntax); |
| 944 | return true; |
| 945 | } |
| 946 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 947 | // Verify that this is followed by EOD. |
Peter Collingbourne | 3bffa52 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 948 | LexUnexpandedToken(Tok); |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 949 | if (Tok.isNot(tok::eod)) |
| 950 | Diag(Tok, diag::ext_pragma_syntax_eod); |
Peter Collingbourne | 3bffa52 | 2011-02-14 01:42:24 +0000 | [diff] [blame] | 951 | return false; |
| 952 | } |
| 953 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 954 | namespace { |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 955 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 956 | /// PragmaOnceHandler - "\#pragma once" marks the file as atomically included. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 957 | struct PragmaOnceHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 958 | PragmaOnceHandler() : PragmaHandler("once") {} |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 959 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 960 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 961 | Token &OnceTok) override { |
Chris Lattner | ce2ab6f | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 962 | PP.CheckEndOfDirective("pragma once"); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 963 | PP.HandlePragmaOnce(OnceTok); |
| 964 | } |
| 965 | }; |
| 966 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 967 | /// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the |
Chris Lattner | c238331 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 968 | /// rest of the line is not lexed. |
| 969 | struct PragmaMarkHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 970 | PragmaMarkHandler() : PragmaHandler("mark") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 971 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 972 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 973 | Token &MarkTok) override { |
Chris Lattner | c238331 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 974 | PP.HandlePragmaMark(); |
| 975 | } |
| 976 | }; |
| 977 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 978 | /// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 979 | struct PragmaPoisonHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 980 | PragmaPoisonHandler() : PragmaHandler("poison") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 981 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 982 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 983 | Token &PoisonTok) override { |
Erik Verbruggen | 851ce0e | 2016-10-26 11:46:10 +0000 | [diff] [blame] | 984 | PP.HandlePragmaPoison(); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 985 | } |
| 986 | }; |
| 987 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 988 | /// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file |
Chris Lattner | c238331 | 2007-12-19 19:38:36 +0000 | [diff] [blame] | 989 | /// as a system header, which silences warnings in it. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 990 | struct PragmaSystemHeaderHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 991 | PragmaSystemHeaderHandler() : PragmaHandler("system_header") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 992 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 993 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 994 | Token &SHToken) override { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 995 | PP.HandlePragmaSystemHeader(SHToken); |
Chris Lattner | ce2ab6f | 2009-04-14 05:07:49 +0000 | [diff] [blame] | 996 | PP.CheckEndOfDirective("pragma"); |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 997 | } |
| 998 | }; |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 999 | |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 1000 | struct PragmaDependencyHandler : public PragmaHandler { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1001 | PragmaDependencyHandler() : PragmaHandler("dependency") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1002 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1003 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1004 | Token &DepToken) override { |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 1005 | PP.HandlePragmaDependency(DepToken); |
| 1006 | } |
| 1007 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1008 | |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1009 | struct PragmaDebugHandler : public PragmaHandler { |
| 1010 | PragmaDebugHandler() : PragmaHandler("__debug") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1011 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1012 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Richard Smith | 77e53cbe | 2019-04-18 00:57:01 +0000 | [diff] [blame] | 1013 | Token &DebugToken) override { |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1014 | Token Tok; |
| 1015 | PP.LexUnexpandedToken(Tok); |
| 1016 | if (Tok.isNot(tok::identifier)) { |
Douglas Gregor | 3cc2648 | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1017 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1018 | return; |
| 1019 | } |
| 1020 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1021 | |
Daniel Dunbar | f2cf329 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 1022 | if (II->isStr("assert")) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1023 | llvm_unreachable("This is an assertion!"); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1024 | } else if (II->isStr("crash")) { |
David Blaikie | 5bd4c2a | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 1025 | LLVM_BUILTIN_TRAP; |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 1026 | } else if (II->isStr("parser_crash")) { |
| 1027 | Token Crasher; |
Benjamin Kramer | 3162f29 | 2015-03-08 19:28:24 +0000 | [diff] [blame] | 1028 | Crasher.startToken(); |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 1029 | Crasher.setKind(tok::annot_pragma_parser_crash); |
Benjamin Kramer | 3162f29 | 2015-03-08 19:28:24 +0000 | [diff] [blame] | 1030 | Crasher.setAnnotationRange(SourceRange(Tok.getLocation())); |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame] | 1031 | PP.EnterToken(Crasher, /*IsReinject*/false); |
Richard Smith | ba3a4f9 | 2016-01-12 21:59:26 +0000 | [diff] [blame] | 1032 | } else if (II->isStr("dump")) { |
| 1033 | Token Identifier; |
| 1034 | PP.LexUnexpandedToken(Identifier); |
| 1035 | if (auto *DumpII = Identifier.getIdentifierInfo()) { |
| 1036 | Token DumpAnnot; |
| 1037 | DumpAnnot.startToken(); |
| 1038 | DumpAnnot.setKind(tok::annot_pragma_dump); |
| 1039 | DumpAnnot.setAnnotationRange( |
| 1040 | SourceRange(Tok.getLocation(), Identifier.getLocation())); |
| 1041 | DumpAnnot.setAnnotationValue(DumpII); |
| 1042 | PP.DiscardUntilEndOfDirective(); |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame] | 1043 | PP.EnterToken(DumpAnnot, /*IsReinject*/false); |
Richard Smith | ba3a4f9 | 2016-01-12 21:59:26 +0000 | [diff] [blame] | 1044 | } else { |
| 1045 | PP.Diag(Identifier, diag::warn_pragma_debug_missing_argument) |
| 1046 | << II->getName(); |
| 1047 | } |
Richard Smith | 6c2b5a8 | 2018-02-09 01:15:13 +0000 | [diff] [blame] | 1048 | } else if (II->isStr("diag_mapping")) { |
| 1049 | Token DiagName; |
| 1050 | PP.LexUnexpandedToken(DiagName); |
| 1051 | if (DiagName.is(tok::eod)) |
| 1052 | PP.getDiagnostics().dump(); |
| 1053 | else if (DiagName.is(tok::string_literal) && !DiagName.hasUDSuffix()) { |
| 1054 | StringLiteralParser Literal(DiagName, PP); |
| 1055 | if (Literal.hadError) |
| 1056 | return; |
| 1057 | PP.getDiagnostics().dump(Literal.GetString()); |
| 1058 | } else { |
| 1059 | PP.Diag(DiagName, diag::warn_pragma_debug_missing_argument) |
| 1060 | << II->getName(); |
| 1061 | } |
Daniel Dunbar | f2cf329 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 1062 | } else if (II->isStr("llvm_fatal_error")) { |
| 1063 | llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error"); |
| 1064 | } else if (II->isStr("llvm_unreachable")) { |
| 1065 | llvm_unreachable("#pragma clang __debug llvm_unreachable"); |
Richard Smith | 3ffa61d | 2015-04-30 23:10:40 +0000 | [diff] [blame] | 1066 | } else if (II->isStr("macro")) { |
| 1067 | Token MacroName; |
| 1068 | PP.LexUnexpandedToken(MacroName); |
| 1069 | auto *MacroII = MacroName.getIdentifierInfo(); |
| 1070 | if (MacroII) |
| 1071 | PP.dumpMacroInfo(MacroII); |
| 1072 | else |
Richard Smith | ba3a4f9 | 2016-01-12 21:59:26 +0000 | [diff] [blame] | 1073 | PP.Diag(MacroName, diag::warn_pragma_debug_missing_argument) |
| 1074 | << II->getName(); |
Richard Smith | 77e53cbe | 2019-04-18 00:57:01 +0000 | [diff] [blame] | 1075 | } else if (II->isStr("module_map")) { |
| 1076 | llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> |
| 1077 | ModuleName; |
| 1078 | if (LexModuleName(PP, Tok, ModuleName)) |
| 1079 | return; |
| 1080 | ModuleMap &MM = PP.getHeaderSearchInfo().getModuleMap(); |
| 1081 | Module *M = nullptr; |
| 1082 | for (auto IIAndLoc : ModuleName) { |
| 1083 | M = MM.lookupModuleQualified(IIAndLoc.first->getName(), M); |
| 1084 | if (!M) { |
| 1085 | PP.Diag(IIAndLoc.second, diag::warn_pragma_debug_unknown_module) |
| 1086 | << IIAndLoc.first; |
| 1087 | return; |
| 1088 | } |
| 1089 | } |
| 1090 | M->dump(); |
Daniel Dunbar | f2cf329 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 1091 | } else if (II->isStr("overflow_stack")) { |
| 1092 | DebugOverflowStack(); |
Daniel Dunbar | 211a787 | 2010-08-18 23:09:23 +0000 | [diff] [blame] | 1093 | } else if (II->isStr("handle_crash")) { |
| 1094 | llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent(); |
| 1095 | if (CRC) |
| 1096 | CRC->HandleCrash(); |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 1097 | } else if (II->isStr("captured")) { |
| 1098 | HandleCaptured(PP); |
Daniel Dunbar | f2cf329 | 2010-08-17 22:32:48 +0000 | [diff] [blame] | 1099 | } else { |
| 1100 | PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command) |
| 1101 | << II->getName(); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1102 | } |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 1103 | |
| 1104 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
| 1105 | if (Callbacks) |
| 1106 | Callbacks->PragmaDebug(Tok.getLocation(), II->getName()); |
| 1107 | } |
| 1108 | |
| 1109 | void HandleCaptured(Preprocessor &PP) { |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 1110 | Token Tok; |
| 1111 | PP.LexUnexpandedToken(Tok); |
| 1112 | |
| 1113 | if (Tok.isNot(tok::eod)) { |
| 1114 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) |
| 1115 | << "pragma clang __debug captured"; |
| 1116 | return; |
| 1117 | } |
| 1118 | |
| 1119 | SourceLocation NameLoc = Tok.getLocation(); |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 1120 | MutableArrayRef<Token> Toks( |
| 1121 | PP.getPreprocessorAllocator().Allocate<Token>(1), 1); |
| 1122 | Toks[0].startToken(); |
| 1123 | Toks[0].setKind(tok::annot_pragma_captured); |
| 1124 | Toks[0].setLocation(NameLoc); |
Tareq A. Siraj | 0de0dd4 | 2013-04-16 18:41:26 +0000 | [diff] [blame] | 1125 | |
Ilya Biryukov | 929af67 | 2019-05-17 09:32:05 +0000 | [diff] [blame] | 1126 | PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true, |
| 1127 | /*IsReinject=*/false); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Francois Pichet | 2e11f5d | 2011-05-25 16:15:03 +0000 | [diff] [blame] | 1130 | // Disable MSVC warning about runtime stack overflow. |
| 1131 | #ifdef _MSC_VER |
| 1132 | #pragma warning(disable : 4717) |
| 1133 | #endif |
Matthias Braun | 285f88d | 2017-04-24 18:41:00 +0000 | [diff] [blame] | 1134 | static void DebugOverflowStack(void (*P)() = nullptr) { |
| 1135 | void (*volatile Self)(void(*P)()) = DebugOverflowStack; |
| 1136 | Self(reinterpret_cast<void(*)()>(Self)); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1137 | } |
Francois Pichet | 2e11f5d | 2011-05-25 16:15:03 +0000 | [diff] [blame] | 1138 | #ifdef _MSC_VER |
| 1139 | #pragma warning(default : 4717) |
| 1140 | #endif |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1141 | }; |
| 1142 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1143 | /// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"' |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1144 | struct PragmaDiagnosticHandler : public PragmaHandler { |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1145 | private: |
| 1146 | const char *Namespace; |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1147 | |
Chris Lattner | fb42a18 | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 1148 | public: |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 1149 | explicit PragmaDiagnosticHandler(const char *NS) |
| 1150 | : PragmaHandler("diagnostic"), Namespace(NS) {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1151 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1152 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1153 | Token &DiagToken) override { |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1154 | SourceLocation DiagLoc = DiagToken.getLocation(); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1155 | Token Tok; |
| 1156 | PP.LexUnexpandedToken(Tok); |
| 1157 | if (Tok.isNot(tok::identifier)) { |
Douglas Gregor | 3cc2648 | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1158 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1159 | return; |
| 1160 | } |
| 1161 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1162 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1163 | |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 1164 | if (II->isStr("pop")) { |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1165 | if (!PP.getDiagnostics().popMappings(DiagLoc)) |
Douglas Gregor | 3cc2648 | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1166 | PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop); |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1167 | else if (Callbacks) |
| 1168 | Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace); |
Douglas Gregor | 3cc2648 | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1169 | return; |
| 1170 | } else if (II->isStr("push")) { |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 1171 | PP.getDiagnostics().pushMappings(DiagLoc); |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1172 | if (Callbacks) |
| 1173 | Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace); |
Chris Lattner | fb42a18 | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 1174 | return; |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | diag::Severity SV = llvm::StringSwitch<diag::Severity>(II->getName()) |
| 1178 | .Case("ignored", diag::Severity::Ignored) |
| 1179 | .Case("warning", diag::Severity::Warning) |
| 1180 | .Case("error", diag::Severity::Error) |
| 1181 | .Case("fatal", diag::Severity::Fatal) |
| 1182 | .Default(diag::Severity()); |
| 1183 | |
| 1184 | if (SV == diag::Severity()) { |
Douglas Gregor | 3cc2648 | 2010-08-30 15:15:34 +0000 | [diff] [blame] | 1185 | PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1186 | return; |
| 1187 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1189 | PP.LexUnexpandedToken(Tok); |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1190 | SourceLocation StringLoc = Tok.getLocation(); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1191 | |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1192 | std::string WarningName; |
Andy Gibbs | a8df57a | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 1193 | if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic", |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 1194 | /*AllowMacroExpansion=*/false)) |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1195 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1197 | if (Tok.isNot(tok::eod)) { |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1198 | PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); |
| 1199 | return; |
| 1200 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1201 | |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1202 | if (WarningName.size() < 3 || WarningName[0] != '-' || |
Richard Smith | 3be1cb2 | 2014-08-07 00:24:21 +0000 | [diff] [blame] | 1203 | (WarningName[1] != 'W' && WarningName[1] != 'R')) { |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1204 | PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1205 | return; |
| 1206 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | |
Sunil Srivastava | 5239de7 | 2016-02-13 01:44:05 +0000 | [diff] [blame] | 1208 | diag::Flavor Flavor = WarningName[1] == 'W' ? diag::Flavor::WarningOrError |
| 1209 | : diag::Flavor::Remark; |
Benjamin Kramer | 2193e23 | 2016-02-13 13:42:41 +0000 | [diff] [blame] | 1210 | StringRef Group = StringRef(WarningName).substr(2); |
Sunil Srivastava | 5239de7 | 2016-02-13 01:44:05 +0000 | [diff] [blame] | 1211 | bool unknownDiag = false; |
| 1212 | if (Group == "everything") { |
| 1213 | // Special handling for pragma clang diagnostic ... "-Weverything". |
| 1214 | // There is no formal group named "everything", so there has to be a |
| 1215 | // special case for it. |
| 1216 | PP.getDiagnostics().setSeverityForAll(Flavor, SV, DiagLoc); |
| 1217 | } else |
| 1218 | unknownDiag = PP.getDiagnostics().setSeverityForGroup(Flavor, Group, SV, |
| 1219 | DiagLoc); |
| 1220 | if (unknownDiag) |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1221 | PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning) |
| 1222 | << WarningName; |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1223 | else if (Callbacks) |
Alp Toker | 46df1c0 | 2014-06-12 10:15:20 +0000 | [diff] [blame] | 1224 | Callbacks->PragmaDiagnostic(DiagLoc, Namespace, SV, WarningName); |
Chris Lattner | 504af11 | 2009-04-19 23:16:58 +0000 | [diff] [blame] | 1225 | } |
| 1226 | }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 1228 | /// "\#pragma hdrstop [<header-name-string>]" |
| 1229 | struct PragmaHdrstopHandler : public PragmaHandler { |
| 1230 | PragmaHdrstopHandler() : PragmaHandler("hdrstop") {} |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1231 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 1232 | Token &DepToken) override { |
| 1233 | PP.HandlePragmaHdrstop(DepToken); |
| 1234 | } |
| 1235 | }; |
| 1236 | |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1237 | /// "\#pragma warning(...)". MSVC's diagnostics do not map cleanly to clang's |
| 1238 | /// diagnostics, so we don't really implement this pragma. We parse it and |
| 1239 | /// ignore it to avoid -Wunknown-pragma warnings. |
| 1240 | struct PragmaWarningHandler : public PragmaHandler { |
| 1241 | PragmaWarningHandler() : PragmaHandler("warning") {} |
| 1242 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1243 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1244 | Token &Tok) override { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1245 | // Parse things like: |
| 1246 | // warning(push, 1) |
| 1247 | // warning(pop) |
John Thompson | 4762b23 | 2013-11-16 00:16:03 +0000 | [diff] [blame] | 1248 | // warning(disable : 1 2 3 ; error : 4 5 6 ; suppress : 7 8 9) |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1249 | SourceLocation DiagLoc = Tok.getLocation(); |
| 1250 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
| 1251 | |
| 1252 | PP.Lex(Tok); |
| 1253 | if (Tok.isNot(tok::l_paren)) { |
| 1254 | PP.Diag(Tok, diag::warn_pragma_warning_expected) << "("; |
| 1255 | return; |
| 1256 | } |
| 1257 | |
| 1258 | PP.Lex(Tok); |
| 1259 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1260 | |
Alexander Musman | 6b080fc | 2015-05-25 11:21:20 +0000 | [diff] [blame] | 1261 | if (II && II->isStr("push")) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1262 | // #pragma warning( push[ ,n ] ) |
Reid Kleckner | 4d18510 | 2013-10-02 15:19:23 +0000 | [diff] [blame] | 1263 | int Level = -1; |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1264 | PP.Lex(Tok); |
| 1265 | if (Tok.is(tok::comma)) { |
| 1266 | PP.Lex(Tok); |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1267 | uint64_t Value; |
| 1268 | if (Tok.is(tok::numeric_constant) && |
| 1269 | PP.parseSimpleIntegerLiteral(Tok, Value)) |
| 1270 | Level = int(Value); |
Reid Kleckner | 4d18510 | 2013-10-02 15:19:23 +0000 | [diff] [blame] | 1271 | if (Level < 0 || Level > 4) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1272 | PP.Diag(Tok, diag::warn_pragma_warning_push_level); |
| 1273 | return; |
| 1274 | } |
| 1275 | } |
| 1276 | if (Callbacks) |
| 1277 | Callbacks->PragmaWarningPush(DiagLoc, Level); |
Alexander Musman | 6b080fc | 2015-05-25 11:21:20 +0000 | [diff] [blame] | 1278 | } else if (II && II->isStr("pop")) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1279 | // #pragma warning( pop ) |
| 1280 | PP.Lex(Tok); |
| 1281 | if (Callbacks) |
| 1282 | Callbacks->PragmaWarningPop(DiagLoc); |
| 1283 | } else { |
| 1284 | // #pragma warning( warning-specifier : warning-number-list |
| 1285 | // [; warning-specifier : warning-number-list...] ) |
| 1286 | while (true) { |
| 1287 | II = Tok.getIdentifierInfo(); |
Alexander Musman | 6b080fc | 2015-05-25 11:21:20 +0000 | [diff] [blame] | 1288 | if (!II && !Tok.is(tok::numeric_constant)) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1289 | PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid); |
| 1290 | return; |
| 1291 | } |
| 1292 | |
| 1293 | // Figure out which warning specifier this is. |
Alexander Musman | 6b080fc | 2015-05-25 11:21:20 +0000 | [diff] [blame] | 1294 | bool SpecifierValid; |
| 1295 | StringRef Specifier; |
| 1296 | llvm::SmallString<1> SpecifierBuf; |
| 1297 | if (II) { |
| 1298 | Specifier = II->getName(); |
| 1299 | SpecifierValid = llvm::StringSwitch<bool>(Specifier) |
| 1300 | .Cases("default", "disable", "error", "once", |
| 1301 | "suppress", true) |
| 1302 | .Default(false); |
| 1303 | // If we read a correct specifier, snatch next token (that should be |
| 1304 | // ":", checked later). |
| 1305 | if (SpecifierValid) |
| 1306 | PP.Lex(Tok); |
| 1307 | } else { |
| 1308 | // Token is a numeric constant. It should be either 1, 2, 3 or 4. |
| 1309 | uint64_t Value; |
| 1310 | Specifier = PP.getSpelling(Tok, SpecifierBuf); |
| 1311 | if (PP.parseSimpleIntegerLiteral(Tok, Value)) { |
| 1312 | SpecifierValid = (Value >= 1) && (Value <= 4); |
| 1313 | } else |
| 1314 | SpecifierValid = false; |
| 1315 | // Next token already snatched by parseSimpleIntegerLiteral. |
| 1316 | } |
| 1317 | |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1318 | if (!SpecifierValid) { |
| 1319 | PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid); |
| 1320 | return; |
| 1321 | } |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1322 | if (Tok.isNot(tok::colon)) { |
| 1323 | PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":"; |
| 1324 | return; |
| 1325 | } |
| 1326 | |
| 1327 | // Collect the warning ids. |
| 1328 | SmallVector<int, 4> Ids; |
| 1329 | PP.Lex(Tok); |
| 1330 | while (Tok.is(tok::numeric_constant)) { |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1331 | uint64_t Value; |
| 1332 | if (!PP.parseSimpleIntegerLiteral(Tok, Value) || Value == 0 || |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1333 | Value > std::numeric_limits<int>::max()) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1334 | PP.Diag(Tok, diag::warn_pragma_warning_expected_number); |
| 1335 | return; |
| 1336 | } |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1337 | Ids.push_back(int(Value)); |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1338 | } |
| 1339 | if (Callbacks) |
| 1340 | Callbacks->PragmaWarning(DiagLoc, Specifier, Ids); |
| 1341 | |
| 1342 | // Parse the next specifier if there is a semicolon. |
| 1343 | if (Tok.isNot(tok::semi)) |
| 1344 | break; |
| 1345 | PP.Lex(Tok); |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | if (Tok.isNot(tok::r_paren)) { |
| 1350 | PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")"; |
| 1351 | return; |
| 1352 | } |
| 1353 | |
| 1354 | PP.Lex(Tok); |
| 1355 | if (Tok.isNot(tok::eod)) |
| 1356 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning"; |
| 1357 | } |
| 1358 | }; |
| 1359 | |
Reid Kleckner | 0f56b22 | 2019-03-14 18:12:17 +0000 | [diff] [blame] | 1360 | /// "\#pragma execution_character_set(...)". MSVC supports this pragma only |
| 1361 | /// for "UTF-8". We parse it and ignore it if UTF-8 is provided and warn |
| 1362 | /// otherwise to avoid -Wunknown-pragma warnings. |
| 1363 | struct PragmaExecCharsetHandler : public PragmaHandler { |
| 1364 | PragmaExecCharsetHandler() : PragmaHandler("execution_character_set") {} |
| 1365 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1366 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Reid Kleckner | 0f56b22 | 2019-03-14 18:12:17 +0000 | [diff] [blame] | 1367 | Token &Tok) override { |
| 1368 | // Parse things like: |
| 1369 | // execution_character_set(push, "UTF-8") |
| 1370 | // execution_character_set(pop) |
| 1371 | SourceLocation DiagLoc = Tok.getLocation(); |
| 1372 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
| 1373 | |
| 1374 | PP.Lex(Tok); |
| 1375 | if (Tok.isNot(tok::l_paren)) { |
| 1376 | PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << "("; |
| 1377 | return; |
| 1378 | } |
| 1379 | |
| 1380 | PP.Lex(Tok); |
| 1381 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1382 | |
| 1383 | if (II && II->isStr("push")) { |
| 1384 | // #pragma execution_character_set( push[ , string ] ) |
| 1385 | PP.Lex(Tok); |
| 1386 | if (Tok.is(tok::comma)) { |
| 1387 | PP.Lex(Tok); |
| 1388 | |
| 1389 | std::string ExecCharset; |
| 1390 | if (!PP.FinishLexStringLiteral(Tok, ExecCharset, |
| 1391 | "pragma execution_character_set", |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 1392 | /*AllowMacroExpansion=*/false)) |
Reid Kleckner | 0f56b22 | 2019-03-14 18:12:17 +0000 | [diff] [blame] | 1393 | return; |
| 1394 | |
| 1395 | // MSVC supports either of these, but nothing else. |
| 1396 | if (ExecCharset != "UTF-8" && ExecCharset != "utf-8") { |
| 1397 | PP.Diag(Tok, diag::warn_pragma_exec_charset_push_invalid) << ExecCharset; |
| 1398 | return; |
| 1399 | } |
| 1400 | } |
| 1401 | if (Callbacks) |
| 1402 | Callbacks->PragmaExecCharsetPush(DiagLoc, "UTF-8"); |
| 1403 | } else if (II && II->isStr("pop")) { |
| 1404 | // #pragma execution_character_set( pop ) |
| 1405 | PP.Lex(Tok); |
| 1406 | if (Callbacks) |
| 1407 | Callbacks->PragmaExecCharsetPop(DiagLoc); |
| 1408 | } else { |
| 1409 | PP.Diag(Tok, diag::warn_pragma_exec_charset_spec_invalid); |
| 1410 | return; |
| 1411 | } |
| 1412 | |
| 1413 | if (Tok.isNot(tok::r_paren)) { |
| 1414 | PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << ")"; |
| 1415 | return; |
| 1416 | } |
| 1417 | |
| 1418 | PP.Lex(Tok); |
| 1419 | if (Tok.isNot(tok::eod)) |
| 1420 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma execution_character_set"; |
| 1421 | } |
| 1422 | }; |
| 1423 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1424 | /// PragmaIncludeAliasHandler - "\#pragma include_alias("...")". |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 1425 | struct PragmaIncludeAliasHandler : public PragmaHandler { |
| 1426 | PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {} |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 1427 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1428 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1429 | Token &IncludeAliasTok) override { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1430 | PP.HandlePragmaIncludeAlias(IncludeAliasTok); |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 1431 | } |
| 1432 | }; |
| 1433 | |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1434 | /// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message |
| 1435 | /// extension. The syntax is: |
| 1436 | /// \code |
| 1437 | /// #pragma message(string) |
| 1438 | /// \endcode |
| 1439 | /// OR, in GCC mode: |
| 1440 | /// \code |
| 1441 | /// #pragma message string |
| 1442 | /// \endcode |
| 1443 | /// string is a string, which is fully macro expanded, and permits string |
| 1444 | /// concatenation, embedded escape characters, etc... See MSDN for more details. |
| 1445 | /// Also handles \#pragma GCC warning and \#pragma GCC error which take the same |
| 1446 | /// form as \#pragma message. |
Chris Lattner | 30c924b | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1447 | struct PragmaMessageHandler : public PragmaHandler { |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1448 | private: |
| 1449 | const PPCallbacks::PragmaMessageKind Kind; |
| 1450 | const StringRef Namespace; |
| 1451 | |
| 1452 | static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind, |
| 1453 | bool PragmaNameOnly = false) { |
| 1454 | switch (Kind) { |
| 1455 | case PPCallbacks::PMK_Message: |
| 1456 | return PragmaNameOnly ? "message" : "pragma message"; |
| 1457 | case PPCallbacks::PMK_Warning: |
| 1458 | return PragmaNameOnly ? "warning" : "pragma warning"; |
| 1459 | case PPCallbacks::PMK_Error: |
| 1460 | return PragmaNameOnly ? "error" : "pragma error"; |
| 1461 | } |
| 1462 | llvm_unreachable("Unknown PragmaMessageKind!"); |
| 1463 | } |
| 1464 | |
| 1465 | public: |
| 1466 | PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind, |
| 1467 | StringRef Namespace = StringRef()) |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 1468 | : PragmaHandler(PragmaKind(Kind, true)), Kind(Kind), |
| 1469 | Namespace(Namespace) {} |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1470 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1471 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1472 | Token &Tok) override { |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1473 | SourceLocation MessageLoc = Tok.getLocation(); |
| 1474 | PP.Lex(Tok); |
| 1475 | bool ExpectClosingParen = false; |
| 1476 | switch (Tok.getKind()) { |
| 1477 | case tok::l_paren: |
| 1478 | // We have a MSVC style pragma message. |
| 1479 | ExpectClosingParen = true; |
| 1480 | // Read the string. |
| 1481 | PP.Lex(Tok); |
| 1482 | break; |
| 1483 | case tok::string_literal: |
| 1484 | // We have a GCC style pragma message, and we just read the string. |
| 1485 | break; |
| 1486 | default: |
| 1487 | PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind; |
| 1488 | return; |
| 1489 | } |
| 1490 | |
| 1491 | std::string MessageString; |
| 1492 | if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind), |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 1493 | /*AllowMacroExpansion=*/true)) |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1494 | return; |
| 1495 | |
| 1496 | if (ExpectClosingParen) { |
| 1497 | if (Tok.isNot(tok::r_paren)) { |
| 1498 | PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind; |
| 1499 | return; |
| 1500 | } |
| 1501 | PP.Lex(Tok); // eat the r_paren. |
| 1502 | } |
| 1503 | |
| 1504 | if (Tok.isNot(tok::eod)) { |
| 1505 | PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind; |
| 1506 | return; |
| 1507 | } |
| 1508 | |
| 1509 | // Output the message. |
| 1510 | PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error) |
| 1511 | ? diag::err_pragma_message |
| 1512 | : diag::warn_pragma_message) << MessageString; |
| 1513 | |
| 1514 | // If the pragma is lexically sound, notify any interested PPCallbacks. |
| 1515 | if (PPCallbacks *Callbacks = PP.getPPCallbacks()) |
| 1516 | Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString); |
Chris Lattner | 30c924b | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1517 | } |
| 1518 | }; |
| 1519 | |
Richard Smith | c51c38b | 2017-04-29 00:34:47 +0000 | [diff] [blame] | 1520 | /// Handle the clang \#pragma module import extension. The syntax is: |
| 1521 | /// \code |
| 1522 | /// #pragma clang module import some.module.name |
| 1523 | /// \endcode |
| 1524 | struct PragmaModuleImportHandler : public PragmaHandler { |
| 1525 | PragmaModuleImportHandler() : PragmaHandler("import") {} |
| 1526 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1527 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 1528 | Token &Tok) override { |
| 1529 | SourceLocation ImportLoc = Tok.getLocation(); |
| 1530 | |
| 1531 | // Read the module name. |
| 1532 | llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> |
| 1533 | ModuleName; |
| 1534 | if (LexModuleName(PP, Tok, ModuleName)) |
| 1535 | return; |
| 1536 | |
| 1537 | if (Tok.isNot(tok::eod)) |
| 1538 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 1539 | |
| 1540 | // If we have a non-empty module path, load the named module. |
| 1541 | Module *Imported = |
| 1542 | PP.getModuleLoader().loadModule(ImportLoc, ModuleName, Module::Hidden, |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 1543 | /*IsInclusionDirective=*/false); |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 1544 | if (!Imported) |
| 1545 | return; |
| 1546 | |
| 1547 | PP.makeModuleVisible(Imported, ImportLoc); |
| 1548 | PP.EnterAnnotationToken(SourceRange(ImportLoc, ModuleName.back().second), |
| 1549 | tok::annot_module_include, Imported); |
| 1550 | if (auto *CB = PP.getPPCallbacks()) |
| 1551 | CB->moduleImport(ImportLoc, ModuleName, Imported); |
| 1552 | } |
| 1553 | }; |
| 1554 | |
| 1555 | /// Handle the clang \#pragma module begin extension. The syntax is: |
| 1556 | /// \code |
| 1557 | /// #pragma clang module begin some.module.name |
| 1558 | /// ... |
| 1559 | /// #pragma clang module end |
| 1560 | /// \endcode |
| 1561 | struct PragmaModuleBeginHandler : public PragmaHandler { |
| 1562 | PragmaModuleBeginHandler() : PragmaHandler("begin") {} |
| 1563 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1564 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 1565 | Token &Tok) override { |
| 1566 | SourceLocation BeginLoc = Tok.getLocation(); |
| 1567 | |
| 1568 | // Read the module name. |
| 1569 | llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> |
| 1570 | ModuleName; |
| 1571 | if (LexModuleName(PP, Tok, ModuleName)) |
| 1572 | return; |
| 1573 | |
| 1574 | if (Tok.isNot(tok::eod)) |
| 1575 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 1576 | |
| 1577 | // We can only enter submodules of the current module. |
| 1578 | StringRef Current = PP.getLangOpts().CurrentModule; |
| 1579 | if (ModuleName.front().first->getName() != Current) { |
| 1580 | PP.Diag(ModuleName.front().second, diag::err_pp_module_begin_wrong_module) |
| 1581 | << ModuleName.front().first << (ModuleName.size() > 1) |
| 1582 | << Current.empty() << Current; |
| 1583 | return; |
| 1584 | } |
| 1585 | |
| 1586 | // Find the module we're entering. We require that a module map for it |
| 1587 | // be loaded or implicitly loadable. |
David Blaikie | 89e58dd | 2019-05-07 21:38:51 +0000 | [diff] [blame] | 1588 | auto &HSI = PP.getHeaderSearchInfo(); |
| 1589 | Module *M = HSI.lookupModule(Current); |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 1590 | if (!M) { |
| 1591 | PP.Diag(ModuleName.front().second, |
| 1592 | diag::err_pp_module_begin_no_module_map) << Current; |
| 1593 | return; |
| 1594 | } |
| 1595 | for (unsigned I = 1; I != ModuleName.size(); ++I) { |
David Blaikie | 89e58dd | 2019-05-07 21:38:51 +0000 | [diff] [blame] | 1596 | auto *NewM = M->findOrInferSubmodule(ModuleName[I].first->getName()); |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 1597 | if (!NewM) { |
| 1598 | PP.Diag(ModuleName[I].second, diag::err_pp_module_begin_no_submodule) |
| 1599 | << M->getFullModuleName() << ModuleName[I].first; |
| 1600 | return; |
| 1601 | } |
| 1602 | M = NewM; |
| 1603 | } |
| 1604 | |
Richard Smith | 51d09c5 | 2017-05-30 05:22:59 +0000 | [diff] [blame] | 1605 | // If the module isn't available, it doesn't make sense to enter it. |
Richard Smith | 27e5aa0 | 2017-06-05 18:57:56 +0000 | [diff] [blame] | 1606 | if (Preprocessor::checkModuleIsAvailable( |
| 1607 | PP.getLangOpts(), PP.getTargetInfo(), PP.getDiagnostics(), M)) { |
Richard Smith | 51d09c5 | 2017-05-30 05:22:59 +0000 | [diff] [blame] | 1608 | PP.Diag(BeginLoc, diag::note_pp_module_begin_here) |
| 1609 | << M->getTopLevelModuleName(); |
| 1610 | return; |
| 1611 | } |
| 1612 | |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 1613 | // Enter the scope of the submodule. |
| 1614 | PP.EnterSubmodule(M, BeginLoc, /*ForPragma*/true); |
| 1615 | PP.EnterAnnotationToken(SourceRange(BeginLoc, ModuleName.back().second), |
| 1616 | tok::annot_module_begin, M); |
| 1617 | } |
| 1618 | }; |
| 1619 | |
| 1620 | /// Handle the clang \#pragma module end extension. |
| 1621 | struct PragmaModuleEndHandler : public PragmaHandler { |
| 1622 | PragmaModuleEndHandler() : PragmaHandler("end") {} |
| 1623 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1624 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 1625 | Token &Tok) override { |
| 1626 | SourceLocation Loc = Tok.getLocation(); |
| 1627 | |
| 1628 | PP.LexUnexpandedToken(Tok); |
| 1629 | if (Tok.isNot(tok::eod)) |
| 1630 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 1631 | |
| 1632 | Module *M = PP.LeaveSubmodule(/*ForPragma*/true); |
| 1633 | if (M) |
| 1634 | PP.EnterAnnotationToken(SourceRange(Loc), tok::annot_module_end, M); |
| 1635 | else |
| 1636 | PP.Diag(Loc, diag::err_pp_module_end_without_module_begin); |
Richard Smith | c51c38b | 2017-04-29 00:34:47 +0000 | [diff] [blame] | 1637 | } |
| 1638 | }; |
| 1639 | |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1640 | /// Handle the clang \#pragma module build extension. |
| 1641 | struct PragmaModuleBuildHandler : public PragmaHandler { |
| 1642 | PragmaModuleBuildHandler() : PragmaHandler("build") {} |
| 1643 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1644 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1645 | Token &Tok) override { |
| 1646 | PP.HandlePragmaModuleBuild(Tok); |
| 1647 | } |
| 1648 | }; |
| 1649 | |
| 1650 | /// Handle the clang \#pragma module load extension. |
| 1651 | struct PragmaModuleLoadHandler : public PragmaHandler { |
| 1652 | PragmaModuleLoadHandler() : PragmaHandler("load") {} |
| 1653 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1654 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1655 | Token &Tok) override { |
| 1656 | SourceLocation Loc = Tok.getLocation(); |
| 1657 | |
| 1658 | // Read the module name. |
| 1659 | llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8> |
| 1660 | ModuleName; |
| 1661 | if (LexModuleName(PP, Tok, ModuleName)) |
| 1662 | return; |
| 1663 | |
| 1664 | if (Tok.isNot(tok::eod)) |
| 1665 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 1666 | |
| 1667 | // Load the module, don't make it visible. |
| 1668 | PP.getModuleLoader().loadModule(Loc, ModuleName, Module::Hidden, |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 1669 | /*IsInclusionDirective=*/false); |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1670 | } |
| 1671 | }; |
| 1672 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1673 | /// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1674 | /// macro on the top of the stack. |
| 1675 | struct PragmaPushMacroHandler : public PragmaHandler { |
| 1676 | PragmaPushMacroHandler() : PragmaHandler("push_macro") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1677 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1678 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1679 | Token &PushMacroTok) override { |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1680 | PP.HandlePragmaPushMacro(PushMacroTok); |
| 1681 | } |
| 1682 | }; |
| 1683 | |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1684 | /// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1685 | /// macro to the value on the top of the stack. |
| 1686 | struct PragmaPopMacroHandler : public PragmaHandler { |
| 1687 | PragmaPopMacroHandler() : PragmaHandler("pop_macro") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1688 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1689 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1690 | Token &PopMacroTok) override { |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1691 | PP.HandlePragmaPopMacro(PopMacroTok); |
| 1692 | } |
| 1693 | }; |
| 1694 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1695 | /// PragmaARCCFCodeAuditedHandler - |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1696 | /// \#pragma clang arc_cf_code_audited begin/end |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1697 | struct PragmaARCCFCodeAuditedHandler : public PragmaHandler { |
| 1698 | PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1699 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1700 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1701 | Token &NameTok) override { |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1702 | SourceLocation Loc = NameTok.getLocation(); |
| 1703 | bool IsBegin; |
| 1704 | |
| 1705 | Token Tok; |
| 1706 | |
| 1707 | // Lex the 'begin' or 'end'. |
| 1708 | PP.LexUnexpandedToken(Tok); |
| 1709 | const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo(); |
| 1710 | if (BeginEnd && BeginEnd->isStr("begin")) { |
| 1711 | IsBegin = true; |
| 1712 | } else if (BeginEnd && BeginEnd->isStr("end")) { |
| 1713 | IsBegin = false; |
| 1714 | } else { |
| 1715 | PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax); |
| 1716 | return; |
| 1717 | } |
| 1718 | |
| 1719 | // Verify that this is followed by EOD. |
| 1720 | PP.LexUnexpandedToken(Tok); |
| 1721 | if (Tok.isNot(tok::eod)) |
| 1722 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 1723 | |
| 1724 | // The start location of the active audit. |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame^] | 1725 | SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedInfo().second; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1726 | |
| 1727 | // The start location we want after processing this. |
| 1728 | SourceLocation NewLoc; |
| 1729 | |
| 1730 | if (IsBegin) { |
| 1731 | // Complain about attempts to re-enter an audit. |
| 1732 | if (BeginLoc.isValid()) { |
| 1733 | PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited); |
| 1734 | PP.Diag(BeginLoc, diag::note_pragma_entered_here); |
| 1735 | } |
| 1736 | NewLoc = Loc; |
| 1737 | } else { |
| 1738 | // Complain about attempts to leave an audit that doesn't exist. |
| 1739 | if (!BeginLoc.isValid()) { |
| 1740 | PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited); |
| 1741 | return; |
| 1742 | } |
| 1743 | NewLoc = SourceLocation(); |
| 1744 | } |
| 1745 | |
Erich Keane | 6a24e80 | 2019-09-13 17:39:31 +0000 | [diff] [blame^] | 1746 | PP.setPragmaARCCFCodeAuditedInfo(NameTok.getIdentifierInfo(), NewLoc); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1747 | } |
| 1748 | }; |
| 1749 | |
Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 1750 | /// PragmaAssumeNonNullHandler - |
| 1751 | /// \#pragma clang assume_nonnull begin/end |
| 1752 | struct PragmaAssumeNonNullHandler : public PragmaHandler { |
| 1753 | PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {} |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 1754 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1755 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 1756 | Token &NameTok) override { |
| 1757 | SourceLocation Loc = NameTok.getLocation(); |
| 1758 | bool IsBegin; |
| 1759 | |
| 1760 | Token Tok; |
| 1761 | |
| 1762 | // Lex the 'begin' or 'end'. |
| 1763 | PP.LexUnexpandedToken(Tok); |
| 1764 | const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo(); |
| 1765 | if (BeginEnd && BeginEnd->isStr("begin")) { |
| 1766 | IsBegin = true; |
| 1767 | } else if (BeginEnd && BeginEnd->isStr("end")) { |
| 1768 | IsBegin = false; |
| 1769 | } else { |
| 1770 | PP.Diag(Tok.getLocation(), diag::err_pp_assume_nonnull_syntax); |
| 1771 | return; |
| 1772 | } |
| 1773 | |
| 1774 | // Verify that this is followed by EOD. |
| 1775 | PP.LexUnexpandedToken(Tok); |
| 1776 | if (Tok.isNot(tok::eod)) |
| 1777 | PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma"; |
| 1778 | |
| 1779 | // The start location of the active audit. |
| 1780 | SourceLocation BeginLoc = PP.getPragmaAssumeNonNullLoc(); |
| 1781 | |
| 1782 | // The start location we want after processing this. |
| 1783 | SourceLocation NewLoc; |
Eli Friedman | 16fee08 | 2017-09-27 23:29:37 +0000 | [diff] [blame] | 1784 | PPCallbacks *Callbacks = PP.getPPCallbacks(); |
Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 1785 | |
| 1786 | if (IsBegin) { |
| 1787 | // Complain about attempts to re-enter an audit. |
| 1788 | if (BeginLoc.isValid()) { |
| 1789 | PP.Diag(Loc, diag::err_pp_double_begin_of_assume_nonnull); |
| 1790 | PP.Diag(BeginLoc, diag::note_pragma_entered_here); |
| 1791 | } |
| 1792 | NewLoc = Loc; |
Eli Friedman | 16fee08 | 2017-09-27 23:29:37 +0000 | [diff] [blame] | 1793 | if (Callbacks) |
| 1794 | Callbacks->PragmaAssumeNonNullBegin(NewLoc); |
Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 1795 | } else { |
| 1796 | // Complain about attempts to leave an audit that doesn't exist. |
| 1797 | if (!BeginLoc.isValid()) { |
| 1798 | PP.Diag(Loc, diag::err_pp_unmatched_end_of_assume_nonnull); |
| 1799 | return; |
| 1800 | } |
| 1801 | NewLoc = SourceLocation(); |
Eli Friedman | 16fee08 | 2017-09-27 23:29:37 +0000 | [diff] [blame] | 1802 | if (Callbacks) |
| 1803 | Callbacks->PragmaAssumeNonNullEnd(NewLoc); |
Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | PP.setPragmaAssumeNonNullLoc(NewLoc); |
| 1807 | } |
| 1808 | }; |
| 1809 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1810 | /// Handle "\#pragma region [...]" |
David Majnemer | 7aa8c2f | 2013-06-30 08:18:16 +0000 | [diff] [blame] | 1811 | /// |
| 1812 | /// The syntax is |
| 1813 | /// \code |
| 1814 | /// #pragma region [optional name] |
| 1815 | /// #pragma endregion [optional comment] |
| 1816 | /// \endcode |
| 1817 | /// |
| 1818 | /// \note This is |
| 1819 | /// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a> |
| 1820 | /// pragma, just skipped by compiler. |
| 1821 | struct PragmaRegionHandler : public PragmaHandler { |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 1822 | PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) {} |
Aaron Ballman | 406ea51 | 2012-11-30 19:52:30 +0000 | [diff] [blame] | 1823 | |
Joel E. Denny | ddde0ec | 2019-05-21 23:51:38 +0000 | [diff] [blame] | 1824 | void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, |
Craig Topper | 9140dd2 | 2014-03-11 06:50:42 +0000 | [diff] [blame] | 1825 | Token &NameTok) override { |
David Majnemer | 7aa8c2f | 2013-06-30 08:18:16 +0000 | [diff] [blame] | 1826 | // #pragma region: endregion matches can be verified |
| 1827 | // __pragma(region): no sense, but ignored by msvc |
| 1828 | // _Pragma is not valid for MSVC, but there isn't any point |
| 1829 | // to handle a _Pragma differently. |
| 1830 | } |
| 1831 | }; |
Aaron Ballman | 406ea51 | 2012-11-30 19:52:30 +0000 | [diff] [blame] | 1832 | |
Eugene Zelenko | cb96ac6 | 2017-12-08 22:39:26 +0000 | [diff] [blame] | 1833 | } // namespace |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 1834 | |
| 1835 | /// RegisterBuiltinPragmas - Install the standard preprocessor pragmas: |
James Dennett | 18a6d79 | 2012-06-17 03:26:26 +0000 | [diff] [blame] | 1836 | /// \#pragma GCC poison/system_header/dependency and \#pragma once. |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 1837 | void Preprocessor::RegisterBuiltinPragmas() { |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1838 | AddPragmaHandler(new PragmaOnceHandler()); |
| 1839 | AddPragmaHandler(new PragmaMarkHandler()); |
Chris Lattner | c0a585d | 2010-08-17 15:55:45 +0000 | [diff] [blame] | 1840 | AddPragmaHandler(new PragmaPushMacroHandler()); |
| 1841 | AddPragmaHandler(new PragmaPopMacroHandler()); |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1842 | AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1843 | |
Chris Lattner | b61448d | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1844 | // #pragma GCC ... |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1845 | AddPragmaHandler("GCC", new PragmaPoisonHandler()); |
| 1846 | AddPragmaHandler("GCC", new PragmaSystemHeaderHandler()); |
| 1847 | AddPragmaHandler("GCC", new PragmaDependencyHandler()); |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1848 | AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC")); |
Andy Gibbs | 9c2ccd6 | 2013-04-17 16:16:16 +0000 | [diff] [blame] | 1849 | AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning, |
| 1850 | "GCC")); |
| 1851 | AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error, |
| 1852 | "GCC")); |
Chris Lattner | b61448d | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1853 | // #pragma clang ... |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1854 | AddPragmaHandler("clang", new PragmaPoisonHandler()); |
| 1855 | AddPragmaHandler("clang", new PragmaSystemHeaderHandler()); |
Daniel Dunbar | b8068c3 | 2010-07-28 15:40:33 +0000 | [diff] [blame] | 1856 | AddPragmaHandler("clang", new PragmaDebugHandler()); |
Argyrios Kyrtzidis | 36745fd | 2010-07-13 09:07:17 +0000 | [diff] [blame] | 1857 | AddPragmaHandler("clang", new PragmaDependencyHandler()); |
Douglas Gregor | 3bde9b1 | 2011-06-22 19:41:48 +0000 | [diff] [blame] | 1858 | AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang")); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 1859 | AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler()); |
Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 1860 | AddPragmaHandler("clang", new PragmaAssumeNonNullHandler()); |
Chris Lattner | b61448d | 2009-05-12 18:21:11 +0000 | [diff] [blame] | 1861 | |
Richard Smith | c51c38b | 2017-04-29 00:34:47 +0000 | [diff] [blame] | 1862 | // #pragma clang module ... |
| 1863 | auto *ModuleHandler = new PragmaNamespace("module"); |
| 1864 | AddPragmaHandler("clang", ModuleHandler); |
| 1865 | ModuleHandler->AddPragma(new PragmaModuleImportHandler()); |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 1866 | ModuleHandler->AddPragma(new PragmaModuleBeginHandler()); |
| 1867 | ModuleHandler->AddPragma(new PragmaModuleEndHandler()); |
Richard Smith | 5d2ed48 | 2017-06-09 19:22:32 +0000 | [diff] [blame] | 1868 | ModuleHandler->AddPragma(new PragmaModuleBuildHandler()); |
| 1869 | ModuleHandler->AddPragma(new PragmaModuleLoadHandler()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1870 | |
Matt Davis | 1edb905 | 2018-01-27 00:25:29 +0000 | [diff] [blame] | 1871 | // Add region pragmas. |
| 1872 | AddPragmaHandler(new PragmaRegionHandler("region")); |
| 1873 | AddPragmaHandler(new PragmaRegionHandler("endregion")); |
Richard Smith | c51c38b | 2017-04-29 00:34:47 +0000 | [diff] [blame] | 1874 | |
Chris Lattner | 2ff698d | 2009-01-16 08:21:25 +0000 | [diff] [blame] | 1875 | // MS extensions. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1876 | if (LangOpts.MicrosoftExt) { |
Reid Kleckner | 881dff3 | 2013-09-13 22:00:30 +0000 | [diff] [blame] | 1877 | AddPragmaHandler(new PragmaWarningHandler()); |
Reid Kleckner | 0f56b22 | 2019-03-14 18:12:17 +0000 | [diff] [blame] | 1878 | AddPragmaHandler(new PragmaExecCharsetHandler()); |
Aaron Ballman | 611306e | 2012-03-02 22:51:54 +0000 | [diff] [blame] | 1879 | AddPragmaHandler(new PragmaIncludeAliasHandler()); |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 1880 | AddPragmaHandler(new PragmaHdrstopHandler()); |
Chris Lattner | 30c924b | 2010-06-26 17:11:39 +0000 | [diff] [blame] | 1881 | } |
John Brawn | 8e62db3 | 2016-04-04 14:22:58 +0000 | [diff] [blame] | 1882 | |
| 1883 | // Pragmas added by plugins |
| 1884 | for (PragmaHandlerRegistry::iterator it = PragmaHandlerRegistry::begin(), |
| 1885 | ie = PragmaHandlerRegistry::end(); |
| 1886 | it != ie; ++it) { |
| 1887 | AddPragmaHandler(it->instantiate().release()); |
| 1888 | } |
Chris Lattner | b694ba7 | 2006-07-02 22:41:36 +0000 | [diff] [blame] | 1889 | } |
Lubos Lunak | 576a041 | 2014-05-01 12:54:03 +0000 | [diff] [blame] | 1890 | |
| 1891 | /// Ignore all pragmas, useful for modes such as -Eonly which would otherwise |
| 1892 | /// warn about those pragmas being unknown. |
| 1893 | void Preprocessor::IgnorePragmas() { |
| 1894 | AddPragmaHandler(new EmptyPragmaHandler()); |
| 1895 | // Also ignore all pragmas in all namespaces created |
| 1896 | // in Preprocessor::RegisterBuiltinPragmas(). |
| 1897 | AddPragmaHandler("GCC", new EmptyPragmaHandler()); |
| 1898 | AddPragmaHandler("clang", new EmptyPragmaHandler()); |
Lubos Lunak | 576a041 | 2014-05-01 12:54:03 +0000 | [diff] [blame] | 1899 | } |