2017-12-09 06:39:26 +08:00
|
|
|
//===- Pragma.cpp - Pragma registration and handling ----------------------===//
|
2006-06-25 05:31:03 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2006-06-25 05:31:03 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2006-07-03 06:41:36 +08:00
|
|
|
// This file implements the PragmaHandler/PragmaTable interfaces and implements
|
|
|
|
// pragma related methods of the Preprocessor class.
|
2006-06-25 05:31:03 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Lex/Pragma.h"
|
2017-12-09 06:39:26 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2016-09-08 05:53:17 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2017-12-09 06:39:26 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "clang/Basic/LangOptions.h"
|
|
|
|
#include "clang/Basic/Module.h"
|
2016-09-08 05:53:17 +08:00
|
|
|
#include "clang/Basic/SourceLocation.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2016-09-08 05:53:17 +08:00
|
|
|
#include "clang/Basic/TokenKinds.h"
|
2006-10-22 15:28:56 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/LexDiagnostic.h"
|
2017-12-09 06:39:26 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2017-06-20 07:09:36 +08:00
|
|
|
#include "clang/Lex/LiteralSupport.h"
|
2010-08-17 23:55:45 +08:00
|
|
|
#include "clang/Lex/MacroInfo.h"
|
2017-12-09 06:39:26 +08:00
|
|
|
#include "clang/Lex/ModuleLoader.h"
|
2016-09-08 05:53:17 +08:00
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2016-09-08 05:53:17 +08:00
|
|
|
#include "clang/Lex/PreprocessorLexer.h"
|
|
|
|
#include "clang/Lex/Token.h"
|
|
|
|
#include "clang/Lex/TokenLexer.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2017-12-09 06:39:26 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-09-08 05:53:17 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2018-04-10 18:34:13 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2018-04-11 02:53:28 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2018-04-10 18:34:13 +08:00
|
|
|
#include "llvm/Support/CrashRecoveryContext.h"
|
2018-04-11 02:53:28 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2010-08-18 06:32:48 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
Add support for retrieving the Doxygen comment associated with a given
declaration in the AST.
The new ASTContext::getCommentForDecl function searches for a comment
that is attached to the given declaration, and returns that comment,
which may be composed of several comment blocks.
Comments are always available in an AST. However, to avoid harming
performance, we don't actually parse the comments. Rather, we keep the
source ranges of all of the comments within a large, sorted vector,
then lazily extract comments via a binary search in that vector only
when needed (which never occurs in a "normal" compile).
Comments are written to a precompiled header/AST file as a blob of
source ranges. That blob is only lazily loaded when one requests a
comment for a declaration (this never occurs in a "normal" compile).
The indexer testbed now supports comment extraction. When the
-point-at location points to a declaration with a Doxygen-style
comment, the indexer testbed prints the associated comment
block(s). See test/Index/comments.c for an example.
Some notes:
- We don't actually attempt to parse the comment blocks themselves,
beyond identifying them as Doxygen comment blocks to associate them
with a declaration.
- We won't find comment blocks that aren't adjacent to the
declaration, because we start our search based on the location of
the declaration.
- We don't go through the necessary hops to find, for example,
whether some redeclaration of a declaration has comments when our
current declaration does not. Similarly, we don't attempt to
associate a \param Foo marker in a function body comment with the
parameter named Foo (although that is certainly possible).
- Verification of my "no performance impact" claims is still "to be
done".
llvm-svn: 74704
2009-07-03 01:08:52 +08:00
|
|
|
#include <algorithm>
|
2016-09-08 05:53:17 +08:00
|
|
|
#include <cassert>
|
2017-12-09 06:39:26 +08:00
|
|
|
#include <cstddef>
|
2016-09-08 05:53:17 +08:00
|
|
|
#include <cstdint>
|
|
|
|
#include <limits>
|
|
|
|
#include <string>
|
2017-12-09 06:39:26 +08:00
|
|
|
#include <utility>
|
2016-09-08 05:53:17 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2006-06-25 05:31:03 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
// Out-of-line destructor to provide a home for the class.
|
2017-12-09 06:39:26 +08:00
|
|
|
PragmaHandler::~PragmaHandler() = default;
|
2006-06-25 05:31:03 +08:00
|
|
|
|
2010-06-12 04:10:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// EmptyPragmaHandler Implementation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-10-13 04:47:58 +08:00
|
|
|
EmptyPragmaHandler::EmptyPragmaHandler(StringRef Name) : PragmaHandler(Name) {}
|
2010-06-12 04:10:12 +08:00
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
|
2019-05-22 07:51:38 +08:00
|
|
|
PragmaIntroducer Introducer,
|
2010-09-10 06:45:38 +08:00
|
|
|
Token &FirstToken) {}
|
2010-06-12 04:10:12 +08:00
|
|
|
|
2006-07-03 13:34:41 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PragmaNamespace Implementation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
PragmaNamespace::~PragmaNamespace() {
|
2014-02-20 07:44:52 +08:00
|
|
|
llvm::DeleteContainerSeconds(Handlers);
|
2006-07-03 13:34:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// FindHandler - Check to see if there is already a handler for the
|
|
|
|
/// specified name. If not, return the handler for the null identifier if it
|
|
|
|
/// exists, otherwise return null. If IgnoreNull is true (the default) then
|
|
|
|
/// the null handler isn't returned on failure to match.
|
2011-07-23 18:55:15 +08:00
|
|
|
PragmaHandler *PragmaNamespace::FindHandler(StringRef Name,
|
2006-07-03 13:34:41 +08:00
|
|
|
bool IgnoreNull) const {
|
2010-07-13 17:07:17 +08:00
|
|
|
if (PragmaHandler *Handler = Handlers.lookup(Name))
|
|
|
|
return Handler;
|
2014-05-18 07:10:59 +08:00
|
|
|
return IgnoreNull ? nullptr : Handlers.lookup(StringRef());
|
2010-07-13 17:07:17 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-07-13 17:07:17 +08:00
|
|
|
void PragmaNamespace::AddPragma(PragmaHandler *Handler) {
|
|
|
|
assert(!Handlers.lookup(Handler->getName()) &&
|
|
|
|
"A handler with this name is already registered in this namespace");
|
2014-11-19 11:06:06 +08:00
|
|
|
Handlers[Handler->getName()] = Handler;
|
2006-07-03 13:34:41 +08:00
|
|
|
}
|
|
|
|
|
2008-10-05 03:17:46 +08:00
|
|
|
void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
|
2010-07-13 17:07:17 +08:00
|
|
|
assert(Handlers.lookup(Handler->getName()) &&
|
|
|
|
"Handler not registered in this namespace");
|
|
|
|
Handlers.erase(Handler->getName());
|
2008-10-05 03:17:46 +08:00
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
void PragmaNamespace::HandlePragma(Preprocessor &PP,
|
2019-05-22 07:51:38 +08:00
|
|
|
PragmaIntroducer Introducer, Token &Tok) {
|
2006-06-25 05:31:03 +08:00
|
|
|
// Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
|
|
|
|
// expand it, the user can have a STDC #define, that should not affect this.
|
|
|
|
PP.LexUnexpandedToken(Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-25 05:31:03 +08:00
|
|
|
// Get the handler for this token. If there is no handler, ignore the pragma.
|
2010-07-13 17:07:17 +08:00
|
|
|
PragmaHandler *Handler
|
|
|
|
= FindHandler(Tok.getIdentifierInfo() ? Tok.getIdentifierInfo()->getName()
|
2011-07-23 18:55:15 +08:00
|
|
|
: StringRef(),
|
2010-07-13 17:07:17 +08:00
|
|
|
/*IgnoreNull=*/false);
|
2014-05-18 07:10:59 +08:00
|
|
|
if (!Handler) {
|
2009-04-20 05:10:26 +08:00
|
|
|
PP.Diag(Tok, diag::warn_pragma_ignored);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-06-25 05:31:03 +08:00
|
|
|
// Otherwise, pass it down.
|
2010-09-10 06:45:38 +08:00
|
|
|
Handler->HandlePragma(PP, Introducer, Tok);
|
2006-06-25 05:31:03 +08:00
|
|
|
}
|
2006-07-03 06:41:36 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessor Pragma Directive Handling.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-06-17 11:26:26 +08:00
|
|
|
/// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the
|
2006-07-03 06:41:36 +08:00
|
|
|
/// rest of the pragma, passing it to the registered pragma handlers.
|
2019-05-22 07:51:38 +08:00
|
|
|
void Preprocessor::HandlePragmaDirective(PragmaIntroducer Introducer) {
|
2013-07-21 04:09:11 +08:00
|
|
|
if (Callbacks)
|
2019-05-22 07:51:38 +08:00
|
|
|
Callbacks->PragmaDirective(Introducer.Loc, Introducer.Kind);
|
2013-07-21 04:09:11 +08:00
|
|
|
|
2012-06-09 02:06:21 +08:00
|
|
|
if (!PragmasEnabled)
|
|
|
|
return;
|
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
++NumPragma;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
// Invoke the first level of pragma handlers which reads the namespace id.
|
2007-07-21 00:59:19 +08:00
|
|
|
Token Tok;
|
2013-07-21 04:09:11 +08:00
|
|
|
PragmaHandlers->HandlePragma(*this, Introducer, Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
// If the pragma handler didn't read the rest of the line, consume it now.
|
2018-07-31 03:24:48 +08:00
|
|
|
if ((CurTokenLexer && CurTokenLexer->isParsingPreprocessorDirective())
|
2011-02-22 21:49:00 +08:00
|
|
|
|| (CurPPLexer && CurPPLexer->ParsingPreprocessorDirective))
|
2006-07-03 06:41:36 +08:00
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
|
|
|
|
/// return the first token after the directive. The _Pragma token has just
|
|
|
|
/// been read into 'Tok'.
|
2007-07-21 00:59:19 +08:00
|
|
|
void Preprocessor::Handle_Pragma(Token &Tok) {
|
2019-04-12 05:18:22 +08:00
|
|
|
// C11 6.10.3.4/3:
|
|
|
|
// all pragma unary operator expressions within [a completely
|
|
|
|
// macro-replaced preprocessing token sequence] are [...] processed [after
|
|
|
|
// rescanning is complete]
|
|
|
|
//
|
|
|
|
// This means that we execute _Pragma operators in two cases:
|
|
|
|
//
|
|
|
|
// 1) on token sequences that would otherwise be produced as the output of
|
|
|
|
// phase 4 of preprocessing, and
|
|
|
|
// 2) on token sequences formed as the macro-replaced token sequence of a
|
|
|
|
// macro argument
|
2012-04-04 00:47:40 +08:00
|
|
|
//
|
2019-04-12 05:18:22 +08:00
|
|
|
// Case #2 appears to be a wording bug: only _Pragmas that would survive to
|
|
|
|
// the end of phase 4 should actually be executed. Discussion on the WG14
|
|
|
|
// mailing list suggests that a _Pragma operator is notionally checked early,
|
|
|
|
// but only pragmas that survive to the end of phase 4 should be executed.
|
|
|
|
//
|
|
|
|
// In Case #2, we check the syntax now, but then put the tokens back into the
|
|
|
|
// token stream for later consumption.
|
|
|
|
|
|
|
|
struct TokenCollector {
|
|
|
|
Preprocessor &Self;
|
|
|
|
bool Collect;
|
|
|
|
SmallVector<Token, 3> Tokens;
|
|
|
|
Token &Tok;
|
|
|
|
|
|
|
|
void lex() {
|
|
|
|
if (Collect)
|
|
|
|
Tokens.push_back(Tok);
|
|
|
|
Self.Lex(Tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
void revert() {
|
|
|
|
assert(Collect && "did not collect tokens");
|
|
|
|
assert(!Tokens.empty() && "collected unexpected number of tokens");
|
|
|
|
|
|
|
|
// Push the ( "string" ) tokens into the token stream.
|
|
|
|
auto Toks = llvm::make_unique<Token[]>(Tokens.size());
|
|
|
|
std::copy(Tokens.begin() + 1, Tokens.end(), Toks.get());
|
|
|
|
Toks[Tokens.size() - 1] = Tok;
|
|
|
|
Self.EnterTokenStream(std::move(Toks), Tokens.size(),
|
2019-05-17 17:32:05 +08:00
|
|
|
/*DisableMacroExpansion*/ true,
|
|
|
|
/*IsReinject*/ true);
|
2019-04-12 05:18:22 +08:00
|
|
|
|
|
|
|
// ... and return the _Pragma token unchanged.
|
|
|
|
Tok = *Tokens.begin();
|
|
|
|
}
|
|
|
|
};
|
2012-04-04 00:47:40 +08:00
|
|
|
|
2019-04-12 05:18:22 +08:00
|
|
|
TokenCollector Toks = {*this, InMacroArgPreExpansion, {}, Tok};
|
2012-04-04 00:47:40 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
// Remember the pragma token location.
|
|
|
|
SourceLocation PragmaLoc = Tok.getLocation();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
// Read the '('.
|
2019-04-12 05:18:22 +08:00
|
|
|
Toks.lex();
|
2008-11-18 15:59:24 +08:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
|
|
|
Diag(PragmaLoc, diag::err__Pragma_malformed);
|
2019-04-12 05:18:22 +08:00
|
|
|
return;
|
2008-11-18 15:59:24 +08:00
|
|
|
}
|
2006-07-03 06:41:36 +08:00
|
|
|
|
|
|
|
// Read the '"..."'.
|
2019-04-12 05:18:22 +08:00
|
|
|
Toks.lex();
|
2013-03-10 07:30:15 +08:00
|
|
|
if (!tok::isStringLiteral(Tok.getKind())) {
|
2008-11-18 15:59:24 +08:00
|
|
|
Diag(PragmaLoc, diag::err__Pragma_malformed);
|
Improved error recovery for _Pragma
Summary:
Currently, if the argument to _Pragma is not a parenthesised string
literal, the bad token will be consumed, as well as the ')', if present.
If additional bad tokens are passed to the _Pragma, this results in
extra error messages which may distract from the true problem.
The proposed patch causes all tokens to be consumed until the closing
')' or a new line, whichever is reached first.
Reviewers: hfinkel, rsmith
Subscribers: hubert.reinterpretcast, fraggamuffin, rnk, cfe-commits
Differential Revision: http://reviews.llvm.org/D8308
Patch by Rachel Craik!
llvm-svn: 243692
2015-07-31 05:30:00 +08:00
|
|
|
// Skip bad tokens, and the ')', if present.
|
2014-08-15 03:47:06 +08:00
|
|
|
if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eof))
|
2012-03-06 11:21:47 +08:00
|
|
|
Lex(Tok);
|
Improved error recovery for _Pragma
Summary:
Currently, if the argument to _Pragma is not a parenthesised string
literal, the bad token will be consumed, as well as the ')', if present.
If additional bad tokens are passed to the _Pragma, this results in
extra error messages which may distract from the true problem.
The proposed patch causes all tokens to be consumed until the closing
')' or a new line, whichever is reached first.
Reviewers: hfinkel, rsmith
Subscribers: hubert.reinterpretcast, fraggamuffin, rnk, cfe-commits
Differential Revision: http://reviews.llvm.org/D8308
Patch by Rachel Craik!
llvm-svn: 243692
2015-07-31 05:30:00 +08:00
|
|
|
while (Tok.isNot(tok::r_paren) &&
|
|
|
|
!Tok.isAtStartOfLine() &&
|
|
|
|
Tok.isNot(tok::eof))
|
|
|
|
Lex(Tok);
|
2012-03-06 11:21:47 +08:00
|
|
|
if (Tok.is(tok::r_paren))
|
|
|
|
Lex(Tok);
|
2019-04-12 05:18:22 +08:00
|
|
|
return;
|
2012-03-06 11:21:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.hasUDSuffix()) {
|
|
|
|
Diag(Tok, diag::err_invalid_string_udl);
|
|
|
|
// Skip this token, and the ')', if present.
|
|
|
|
Lex(Tok);
|
|
|
|
if (Tok.is(tok::r_paren))
|
|
|
|
Lex(Tok);
|
2019-04-12 05:18:22 +08:00
|
|
|
return;
|
2008-11-18 15:59:24 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
// Remember the string.
|
2012-04-04 00:47:40 +08:00
|
|
|
Token StrTok = Tok;
|
2006-07-03 06:41:36 +08:00
|
|
|
|
|
|
|
// Read the ')'.
|
2019-04-12 05:18:22 +08:00
|
|
|
Toks.lex();
|
2008-11-18 15:59:24 +08:00
|
|
|
if (Tok.isNot(tok::r_paren)) {
|
|
|
|
Diag(PragmaLoc, diag::err__Pragma_malformed);
|
2019-04-12 05:18:22 +08:00
|
|
|
return;
|
2008-11-18 15:59:24 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2019-04-12 05:18:22 +08:00
|
|
|
// If we're expanding a macro argument, put the tokens back.
|
|
|
|
if (InMacroArgPreExpansion) {
|
|
|
|
Toks.revert();
|
2012-04-04 00:47:40 +08:00
|
|
|
return;
|
2019-04-12 05:18:22 +08:00
|
|
|
}
|
2012-04-04 00:47:40 +08:00
|
|
|
|
2009-02-16 04:52:18 +08:00
|
|
|
SourceLocation RParenLoc = Tok.getLocation();
|
2012-04-04 00:47:40 +08:00
|
|
|
std::string StrVal = getSpelling(StrTok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-03-10 07:30:15 +08:00
|
|
|
// The _Pragma is lexically sound. Destringize according to C11 6.10.9.1:
|
|
|
|
// "The string literal is destringized by deleting any encoding prefix,
|
2009-01-17 02:59:23 +08:00
|
|
|
// deleting the leading and trailing double-quotes, replacing each escape
|
|
|
|
// sequence \" by a double-quote, and replacing each escape sequence \\ by a
|
|
|
|
// single backslash."
|
2013-03-10 07:30:15 +08:00
|
|
|
if (StrVal[0] == 'L' || StrVal[0] == 'U' ||
|
|
|
|
(StrVal[0] == 'u' && StrVal[1] != '8'))
|
2006-07-03 06:41:36 +08:00
|
|
|
StrVal.erase(StrVal.begin());
|
2013-03-10 07:30:15 +08:00
|
|
|
else if (StrVal[0] == 'u')
|
|
|
|
StrVal.erase(StrVal.begin(), StrVal.begin() + 2);
|
|
|
|
|
|
|
|
if (StrVal[0] == 'R') {
|
|
|
|
// FIXME: C++11 does not specify how to handle raw-string-literals here.
|
|
|
|
// We strip off the 'R', the quotes, the d-char-sequences, and the parens.
|
|
|
|
assert(StrVal[1] == '"' && StrVal[StrVal.size() - 1] == '"' &&
|
|
|
|
"Invalid raw string token!");
|
|
|
|
|
|
|
|
// Measure the length of the d-char-sequence.
|
|
|
|
unsigned NumDChars = 0;
|
|
|
|
while (StrVal[2 + NumDChars] != '(') {
|
|
|
|
assert(NumDChars < (StrVal.size() - 5) / 2 &&
|
|
|
|
"Invalid raw string token!");
|
|
|
|
++NumDChars;
|
|
|
|
}
|
|
|
|
assert(StrVal[StrVal.size() - 2 - NumDChars] == ')');
|
|
|
|
|
|
|
|
// Remove 'R " d-char-sequence' and 'd-char-sequence "'. We'll replace the
|
|
|
|
// parens below.
|
|
|
|
StrVal.erase(0, 2 + NumDChars);
|
|
|
|
StrVal.erase(StrVal.size() - 1 - NumDChars);
|
|
|
|
} else {
|
|
|
|
assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
|
|
|
|
"Invalid string token!");
|
|
|
|
|
|
|
|
// Remove escaped quotes and escapes.
|
2013-05-04 18:37:20 +08:00
|
|
|
unsigned ResultPos = 1;
|
2016-10-26 21:06:13 +08:00
|
|
|
for (size_t i = 1, e = StrVal.size() - 1; i != e; ++i) {
|
2013-09-26 00:42:48 +08:00
|
|
|
// Skip escapes. \\ -> '\' and \" -> '"'.
|
|
|
|
if (StrVal[i] == '\\' && i + 1 < e &&
|
|
|
|
(StrVal[i + 1] == '\\' || StrVal[i + 1] == '"'))
|
|
|
|
++i;
|
|
|
|
StrVal[ResultPos++] = StrVal[i];
|
2013-03-10 07:30:15 +08:00
|
|
|
}
|
2013-09-26 00:42:48 +08:00
|
|
|
StrVal.erase(StrVal.begin() + ResultPos, StrVal.end() - 1);
|
2013-03-10 07:30:15 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
// Remove the front quote, replacing it with a space, so that the pragma
|
|
|
|
// contents appear to have a space before them.
|
|
|
|
StrVal[0] = ' ';
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-08 16:08:45 +08:00
|
|
|
// Replace the terminating quote with a \n.
|
2006-07-03 06:41:36 +08:00
|
|
|
StrVal[StrVal.size()-1] = '\n';
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-22 21:49:06 +08:00
|
|
|
// Plop the string (including the newline and trailing null) into a buffer
|
|
|
|
// where we can lex it.
|
|
|
|
Token TmpTok;
|
|
|
|
TmpTok.startToken();
|
2012-09-25 05:07:17 +08:00
|
|
|
CreateString(StrVal, TmpTok);
|
2011-02-22 21:49:06 +08:00
|
|
|
SourceLocation TokLoc = TmpTok.getLocation();
|
|
|
|
|
|
|
|
// Make and enter a lexer object so that we lex and expand the tokens just
|
|
|
|
// like any others.
|
|
|
|
Lexer *TL = Lexer::Create_PragmaLexer(TokLoc, PragmaLoc, RParenLoc,
|
|
|
|
StrVal.size(), *this);
|
|
|
|
|
2014-05-18 07:10:59 +08:00
|
|
|
EnterSourceFileWithLexer(TL, nullptr);
|
2011-02-22 21:49:06 +08:00
|
|
|
|
|
|
|
// With everything set up, lex this as a #pragma directive.
|
2019-05-22 07:51:38 +08:00
|
|
|
HandlePragmaDirective({PIK__Pragma, PragmaLoc});
|
2010-08-29 06:34:47 +08:00
|
|
|
|
|
|
|
// Finally, return whatever came after the pragma directive.
|
|
|
|
return Lex(Tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HandleMicrosoft__pragma - Like Handle_Pragma except the pragma text
|
|
|
|
/// is not enclosed within a string literal.
|
|
|
|
void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
|
|
|
|
// Remember the pragma token location.
|
|
|
|
SourceLocation PragmaLoc = Tok.getLocation();
|
|
|
|
|
|
|
|
// Read the '('.
|
|
|
|
Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
|
|
|
Diag(PragmaLoc, diag::err__Pragma_malformed);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-22 21:49:06 +08:00
|
|
|
// Get the tokens enclosed within the __pragma(), as well as the final ')'.
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<Token, 32> PragmaToks;
|
2010-08-29 06:34:47 +08:00
|
|
|
int NumParens = 0;
|
|
|
|
Lex(Tok);
|
|
|
|
while (Tok.isNot(tok::eof)) {
|
2011-02-22 21:49:06 +08:00
|
|
|
PragmaToks.push_back(Tok);
|
2010-08-29 06:34:47 +08:00
|
|
|
if (Tok.is(tok::l_paren))
|
|
|
|
NumParens++;
|
|
|
|
else if (Tok.is(tok::r_paren) && NumParens-- == 0)
|
|
|
|
break;
|
|
|
|
Lex(Tok);
|
|
|
|
}
|
|
|
|
|
2010-08-29 09:09:54 +08:00
|
|
|
if (Tok.is(tok::eof)) {
|
|
|
|
Diag(PragmaLoc, diag::err_unterminated___pragma);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-22 21:49:06 +08:00
|
|
|
PragmaToks.front().setFlag(Token::LeadingSpace);
|
2010-08-29 06:34:47 +08:00
|
|
|
|
2011-02-28 10:37:51 +08:00
|
|
|
// Replace the ')' with an EOD to mark the end of the pragma.
|
|
|
|
PragmaToks.back().setKind(tok::eod);
|
2010-08-29 06:34:47 +08:00
|
|
|
|
2011-02-22 21:49:06 +08:00
|
|
|
Token *TokArray = new Token[PragmaToks.size()];
|
|
|
|
std::copy(PragmaToks.begin(), PragmaToks.end(), TokArray);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-22 21:49:06 +08:00
|
|
|
// Push the tokens onto the stack.
|
2019-05-17 17:32:05 +08:00
|
|
|
EnterTokenStream(TokArray, PragmaToks.size(), true, true,
|
|
|
|
/*IsReinject*/ false);
|
2006-07-03 07:00:20 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
// With everything set up, lex this as a #pragma directive.
|
2019-05-22 07:51:38 +08:00
|
|
|
HandlePragmaDirective({PIK___pragma, PragmaLoc});
|
2006-07-03 06:41:36 +08:00
|
|
|
|
2011-02-22 21:49:06 +08:00
|
|
|
// Finally, return whatever came after the pragma directive.
|
|
|
|
return Lex(Tok);
|
|
|
|
}
|
2006-07-03 06:41:36 +08:00
|
|
|
|
2012-06-17 11:26:26 +08:00
|
|
|
/// HandlePragmaOnce - Handle \#pragma once. OnceTok is the 'once'.
|
2007-07-21 00:59:19 +08:00
|
|
|
void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
|
2016-07-26 01:17:06 +08:00
|
|
|
// Don't honor the 'once' when handling the primary source file, unless
|
2016-10-27 22:17:10 +08:00
|
|
|
// this is a prefix to a TU, which indicates we're generating a PCH file, or
|
|
|
|
// when the main file is a header (e.g. when -xc-header is provided on the
|
|
|
|
// commandline).
|
|
|
|
if (isInPrimaryFile() && TUKind != TU_Prefix && !getLangOpts().IsHeaderFile) {
|
2006-07-03 06:41:36 +08:00
|
|
|
Diag(OnceTok, diag::pp_pragma_once_in_main_file);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
// Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
|
|
|
|
// Mark the file as a once-only file now.
|
2009-01-17 14:22:33 +08:00
|
|
|
HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
|
2006-07-03 06:41:36 +08:00
|
|
|
}
|
|
|
|
|
2007-12-20 03:38:36 +08:00
|
|
|
void Preprocessor::HandlePragmaMark() {
|
2008-11-20 06:21:33 +08:00
|
|
|
assert(CurPPLexer && "No current lexer?");
|
2018-12-04 22:34:09 +08:00
|
|
|
CurLexer->ReadToEndOfLine();
|
2007-12-20 03:38:36 +08:00
|
|
|
}
|
|
|
|
|
2012-06-17 11:26:26 +08:00
|
|
|
/// HandlePragmaPoison - Handle \#pragma GCC poison. PoisonTok is the 'poison'.
|
2016-10-26 19:46:10 +08:00
|
|
|
void Preprocessor::HandlePragmaPoison() {
|
2007-07-21 00:59:19 +08:00
|
|
|
Token Tok;
|
2006-07-20 12:31:52 +08:00
|
|
|
|
2016-09-08 05:53:17 +08:00
|
|
|
while (true) {
|
2006-07-03 06:41:36 +08:00
|
|
|
// Read the next token to poison. While doing this, pretend that we are
|
|
|
|
// skipping while reading the identifier to poison.
|
|
|
|
// This avoids errors on code like:
|
|
|
|
// #pragma GCC poison X
|
|
|
|
// #pragma GCC poison X
|
2008-11-18 09:12:54 +08:00
|
|
|
if (CurPPLexer) CurPPLexer->LexingRawMode = true;
|
2006-07-03 06:41:36 +08:00
|
|
|
LexUnexpandedToken(Tok);
|
2008-11-18 09:12:54 +08:00
|
|
|
if (CurPPLexer) CurPPLexer->LexingRawMode = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
// If we reached the end of line, we're done.
|
2011-02-28 10:37:51 +08:00
|
|
|
if (Tok.is(tok::eod)) return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
// Can only poison identifiers.
|
2010-12-22 16:23:18 +08:00
|
|
|
if (Tok.isNot(tok::raw_identifier)) {
|
2006-07-03 06:41:36 +08:00
|
|
|
Diag(Tok, diag::err_pp_invalid_poison);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-08 16:28:12 +08:00
|
|
|
// Look up the identifier info for the token. We disabled identifier lookup
|
|
|
|
// by saying we're skipping contents, so we need to do this manually.
|
|
|
|
IdentifierInfo *II = LookUpIdentifierInfo(Tok);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
// Already poisoned.
|
|
|
|
if (II->isPoisoned()) continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
// If this is a macro identifier, emit a warning.
|
2015-04-30 07:20:19 +08:00
|
|
|
if (isMacroDefined(II))
|
2006-07-03 06:41:36 +08:00
|
|
|
Diag(Tok, diag::pp_poisoning_existing_macro);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
// Finally, poison it!
|
|
|
|
II->setIsPoisoned();
|
Make the loading of information attached to an IdentifierInfo from an
AST file more lazy, so that we don't eagerly load that information for
all known identifiers each time a new AST file is loaded. The eager
reloading made some sense in the context of precompiled headers, since
very few identifiers were defined before PCH load time. With modules,
however, a huge amount of code can get parsed before we see an
@import, so laziness becomes important here.
The approach taken to make this information lazy is fairly simple:
when we load a new AST file, we mark all of the existing identifiers
as being out-of-date. Whenever we want to access information that may
come from an AST (e.g., whether the identifier has a macro definition,
or what top-level declarations have that name), we check the
out-of-date bit and, if it's set, ask the AST reader to update the
IdentifierInfo from the AST files. The update is a merge, and we now
take care to merge declarations before/after imports with declarations
from multiple imports.
The results of this optimization are fairly dramatic. On a small
application that brings in 14 non-trivial modules, this takes modules
from being > 3x slower than a "perfect" PCH file down to 30% slower
for a full rebuild. A partial rebuild (where the PCH file or modules
can be re-used) is down to 7% slower. Making the PCH file just a
little imperfect (e.g., adding two smallish modules used by a bunch of
.m files that aren't in the PCH file) tips the scales in favor of the
modules approach, with 24% faster partial rebuilds.
This is just a first step; the lazy scheme could possibly be improved
by adding versioning, so we don't search into modules we already
searched. Moreover, we'll need similar lazy schemes for all of the
other lookup data structures, such as DeclContexts.
llvm-svn: 143100
2011-10-27 17:33:13 +08:00
|
|
|
if (II->isFromAST())
|
|
|
|
II->setChangedSinceDeserialization();
|
2006-07-03 06:41:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-17 11:26:26 +08:00
|
|
|
/// HandlePragmaSystemHeader - Implement \#pragma GCC system_header. We know
|
2006-07-03 06:41:36 +08:00
|
|
|
/// that the whole directive has been parsed.
|
2007-07-21 00:59:19 +08:00
|
|
|
void Preprocessor::HandlePragmaSystemHeader(Token &SysHeaderTok) {
|
2006-07-03 06:41:36 +08:00
|
|
|
if (isInPrimaryFile()) {
|
|
|
|
Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
// Get the current file lexer we're looking at. Ignore _Pragma 'files' etc.
|
2008-11-20 09:45:11 +08:00
|
|
|
PreprocessorLexer *TheLexer = getCurrentFileLexer();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
// Mark the file as a system header.
|
2009-01-17 14:22:33 +08:00
|
|
|
HeaderInfo.MarkFileSystemHeader(TheLexer->getFileEntry());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-15 13:02:34 +08:00
|
|
|
PresumedLoc PLoc = SourceMgr.getPresumedLoc(SysHeaderTok.getLocation());
|
2010-11-12 15:15:47 +08:00
|
|
|
if (PLoc.isInvalid())
|
|
|
|
return;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-06-21 23:13:30 +08:00
|
|
|
unsigned FilenameID = SourceMgr.getLineTableFilenameID(PLoc.getFilename());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-05-23 06:10:16 +08:00
|
|
|
// Notify the client, if desired, that we are in a new source file.
|
|
|
|
if (Callbacks)
|
|
|
|
Callbacks->FileChanged(SysHeaderTok.getLocation(),
|
|
|
|
PPCallbacks::SystemHeaderPragma, SrcMgr::C_System);
|
|
|
|
|
2009-06-15 13:02:34 +08:00
|
|
|
// Emit a line marker. This will change any source locations from this point
|
|
|
|
// forward to realize they are in a system header.
|
|
|
|
// Create a line note with this information.
|
2017-05-23 05:42:58 +08:00
|
|
|
SourceMgr.AddLineNote(SysHeaderTok.getLocation(), PLoc.getLine() + 1,
|
2013-04-18 03:09:18 +08:00
|
|
|
FilenameID, /*IsEntry=*/false, /*IsExit=*/false,
|
2017-05-23 05:42:58 +08:00
|
|
|
SrcMgr::C_System);
|
2006-07-03 06:41:36 +08:00
|
|
|
}
|
|
|
|
|
2012-06-17 11:26:26 +08:00
|
|
|
/// HandlePragmaDependency - Handle \#pragma GCC dependency "foo" blah.
|
2007-07-21 00:59:19 +08:00
|
|
|
void Preprocessor::HandlePragmaDependency(Token &DependencyTok) {
|
|
|
|
Token FilenameTok;
|
2019-03-19 09:51:19 +08:00
|
|
|
if (LexHeaderName(FilenameTok, /*AllowConcatenation*/false))
|
|
|
|
return;
|
2006-07-03 06:41:36 +08:00
|
|
|
|
2019-03-19 09:51:19 +08:00
|
|
|
// If the next token wasn't a header-name, diagnose the error.
|
2019-03-20 06:09:55 +08:00
|
|
|
if (FilenameTok.isNot(tok::header_name)) {
|
2019-03-19 09:51:19 +08:00
|
|
|
Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
|
2006-07-03 06:41:36 +08:00
|
|
|
return;
|
2019-03-19 09:51:19 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-30 13:58:32 +08:00
|
|
|
// Reserve a buffer to get the spelling.
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> FilenameBuffer;
|
2010-03-17 06:30:13 +08:00
|
|
|
bool Invalid = false;
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Filename = getSpelling(FilenameTok, FilenameBuffer, &Invalid);
|
2010-03-17 06:30:13 +08:00
|
|
|
if (Invalid)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-01-10 09:35:12 +08:00
|
|
|
bool isAngled =
|
|
|
|
GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
|
2006-10-30 13:58:32 +08:00
|
|
|
// If GetIncludeFilenameSpelling set the start ptr to null, there was an
|
|
|
|
// error.
|
2010-01-10 09:35:12 +08:00
|
|
|
if (Filename.empty())
|
2006-10-30 13:58:32 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
// Search include directories for this file.
|
2006-07-03 06:41:36 +08:00
|
|
|
const DirectoryLookup *CurDir;
|
2014-10-20 08:15:49 +08:00
|
|
|
const FileEntry *File =
|
|
|
|
LookupFile(FilenameTok.getLocation(), Filename, isAngled, nullptr,
|
2019-02-06 06:34:55 +08:00
|
|
|
nullptr, CurDir, nullptr, nullptr, nullptr, nullptr, nullptr);
|
2014-05-18 07:10:59 +08:00
|
|
|
if (!File) {
|
2011-08-31 07:07:51 +08:00
|
|
|
if (!SuppressIncludeNotFoundError)
|
|
|
|
Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
|
2008-11-18 16:02:48 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
const FileEntry *CurFile = getCurrentFileLexer()->getFileEntry();
|
2006-07-03 06:41:36 +08:00
|
|
|
|
|
|
|
// If this file is older than the file it depends on, emit a diagnostic.
|
|
|
|
if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
|
|
|
|
// Lex tokens at the end of the message and include them in the message.
|
|
|
|
std::string Message;
|
|
|
|
Lex(DependencyTok);
|
2011-02-28 10:37:51 +08:00
|
|
|
while (DependencyTok.isNot(tok::eod)) {
|
2006-07-03 06:41:36 +08:00
|
|
|
Message += getSpelling(DependencyTok) + " ";
|
|
|
|
Lex(DependencyTok);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-09-06 07:16:09 +08:00
|
|
|
// Remove the trailing ' ' if present.
|
|
|
|
if (!Message.empty())
|
|
|
|
Message.erase(Message.end()-1);
|
2008-11-18 16:02:48 +08:00
|
|
|
Diag(FilenameTok, diag::pp_out_of_date_dependency) << Message;
|
2006-07-03 06:41:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-07 05:02:12 +08:00
|
|
|
/// ParsePragmaPushOrPopMacro - Handle parsing of pragma push_macro/pop_macro.
|
2010-08-17 23:55:45 +08:00
|
|
|
/// Return the IdentifierInfo* associated with the macro to push or pop.
|
|
|
|
IdentifierInfo *Preprocessor::ParsePragmaPushOrPopMacro(Token &Tok) {
|
|
|
|
// Remember the pragma token location.
|
|
|
|
Token PragmaTok = Tok;
|
|
|
|
|
|
|
|
// Read the '('.
|
|
|
|
Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
|
|
|
Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
|
|
|
|
<< getSpelling(PragmaTok);
|
2014-05-18 07:10:59 +08:00
|
|
|
return nullptr;
|
2010-08-17 23:55:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the macro name string.
|
|
|
|
Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::string_literal)) {
|
|
|
|
Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
|
|
|
|
<< getSpelling(PragmaTok);
|
2014-05-18 07:10:59 +08:00
|
|
|
return nullptr;
|
2010-08-17 23:55:45 +08:00
|
|
|
}
|
|
|
|
|
2012-03-06 11:21:47 +08:00
|
|
|
if (Tok.hasUDSuffix()) {
|
|
|
|
Diag(Tok, diag::err_invalid_string_udl);
|
2014-05-18 07:10:59 +08:00
|
|
|
return nullptr;
|
2012-03-06 11:21:47 +08:00
|
|
|
}
|
|
|
|
|
2010-08-17 23:55:45 +08:00
|
|
|
// Remember the macro string.
|
|
|
|
std::string StrVal = getSpelling(Tok);
|
|
|
|
|
|
|
|
// Read the ')'.
|
|
|
|
Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::r_paren)) {
|
|
|
|
Diag(PragmaTok.getLocation(), diag::err_pragma_push_pop_macro_malformed)
|
|
|
|
<< getSpelling(PragmaTok);
|
2014-05-18 07:10:59 +08:00
|
|
|
return nullptr;
|
2010-08-17 23:55:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
|
|
|
|
"Invalid string token!");
|
|
|
|
|
|
|
|
// Create a Token from the string.
|
|
|
|
Token MacroTok;
|
|
|
|
MacroTok.startToken();
|
2010-12-22 16:23:18 +08:00
|
|
|
MacroTok.setKind(tok::raw_identifier);
|
2012-09-25 05:07:17 +08:00
|
|
|
CreateString(StringRef(&StrVal[1], StrVal.size() - 2), MacroTok);
|
2010-08-17 23:55:45 +08:00
|
|
|
|
|
|
|
// Get the IdentifierInfo of MacroToPushTok.
|
|
|
|
return LookUpIdentifierInfo(MacroTok);
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Handle \#pragma push_macro.
|
2012-06-17 11:26:26 +08:00
|
|
|
///
|
2010-08-17 23:55:45 +08:00
|
|
|
/// The syntax is:
|
2012-06-17 11:26:26 +08:00
|
|
|
/// \code
|
2012-12-01 04:04:39 +08:00
|
|
|
/// #pragma push_macro("macro")
|
2012-06-17 11:26:26 +08:00
|
|
|
/// \endcode
|
2010-08-17 23:55:45 +08:00
|
|
|
void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) {
|
|
|
|
// Parse the pragma directive and get the macro IdentifierInfo*.
|
|
|
|
IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PushMacroTok);
|
|
|
|
if (!IdentInfo) return;
|
|
|
|
|
|
|
|
// Get the MacroInfo associated with IdentInfo.
|
|
|
|
MacroInfo *MI = getMacroInfo(IdentInfo);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-08-17 23:55:45 +08:00
|
|
|
if (MI) {
|
|
|
|
// Allow the original MacroInfo to be redefined later.
|
|
|
|
MI->setIsAllowRedefinitionsWithoutWarning(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the cloned MacroInfo so we can retrieve it later.
|
2013-02-20 08:54:57 +08:00
|
|
|
PragmaPushMacroInfo[IdentInfo].push_back(MI);
|
2010-08-17 23:55:45 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Handle \#pragma pop_macro.
|
2012-06-17 11:26:26 +08:00
|
|
|
///
|
2010-08-17 23:55:45 +08:00
|
|
|
/// The syntax is:
|
2012-06-17 11:26:26 +08:00
|
|
|
/// \code
|
2010-08-17 23:55:45 +08:00
|
|
|
/// #pragma pop_macro("macro")
|
2012-06-17 11:26:26 +08:00
|
|
|
/// \endcode
|
2010-08-17 23:55:45 +08:00
|
|
|
void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) {
|
|
|
|
SourceLocation MessageLoc = PopMacroTok.getLocation();
|
|
|
|
|
|
|
|
// Parse the pragma directive and get the macro IdentifierInfo*.
|
|
|
|
IdentifierInfo *IdentInfo = ParsePragmaPushOrPopMacro(PopMacroTok);
|
|
|
|
if (!IdentInfo) return;
|
|
|
|
|
|
|
|
// Find the vector<MacroInfo*> associated with the macro.
|
2017-12-09 06:39:26 +08:00
|
|
|
llvm::DenseMap<IdentifierInfo *, std::vector<MacroInfo *>>::iterator iter =
|
2010-08-17 23:55:45 +08:00
|
|
|
PragmaPushMacroInfo.find(IdentInfo);
|
|
|
|
if (iter != PragmaPushMacroInfo.end()) {
|
Keep history of macro definitions and #undefs
Summary:
Summary: Keep history of macro definitions and #undefs with corresponding source locations, so that we can later find out all macros active in a specified source location. We don't save the history in PCH (no need currently). Memory overhead is about sizeof(void*)*3*<number of macro definitions and #undefs>+<in-memory size of all #undef'd macros>
I've run a test on a file composed of 109 .h files from boost 1.49 on x86-64 linux.
Stats before this patch:
*** Preprocessor Stats:
73222 directives found:
19171 #define.
4345 #undef.
#include/#include_next/#import:
5233 source files entered.
27 max include stack depth
19210 #if/#ifndef/#ifdef.
2384 #else/#elif.
6891 #endif.
408 #pragma.
14466 #if/#ifndef#ifdef regions skipped
80023/451669/1270 obj/fn/builtin macros expanded, 85724 on the fast path.
127145 token paste (##) operations performed, 11008 on the fast path.
Preprocessor Memory: 5874615B total
BumpPtr: 4399104
Macro Expanded Tokens: 417768
Predefines Buffer: 8135
Macros: 1048576
#pragma push_macro Info: 0
Poison Reasons: 1024
Comment Handlers: 8
Stats with this patch:
...
Preprocessor Memory: 7541687B total
BumpPtr: 6066176
Macro Expanded Tokens: 417768
Predefines Buffer: 8135
Macros: 1048576
#pragma push_macro Info: 0
Poison Reasons: 1024
Comment Handlers: 8
In my test increase in memory usage is about 1.7Mb, which is ~28% of initial preprocessor's memory usage and about 0.8% of clang's total VMM allocation.
As for CPU overhead, it should only be noticeable when iterating over all macros, and should mostly consist of couple extra dereferences and one comparison per macro + skipping of #undef'd macros. It's less trivial to measure, though, as the preprocessor consumes a very small fraction of compilation time.
Reviewers: doug.gregor, klimek, rsmith, djasper
Reviewed By: doug.gregor
CC: cfe-commits, chandlerc
Differential Revision: http://llvm-reviews.chandlerc.com/D28
llvm-svn: 162810
2012-08-29 08:20:03 +08:00
|
|
|
// Forget the MacroInfo currently associated with IdentInfo.
|
2015-04-30 07:20:19 +08:00
|
|
|
if (MacroInfo *MI = getMacroInfo(IdentInfo)) {
|
2013-03-27 01:17:01 +08:00
|
|
|
if (MI->isWarnIfUnused())
|
|
|
|
WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
|
|
|
|
appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc));
|
2010-12-16 02:44:22 +08:00
|
|
|
}
|
2010-08-17 23:55:45 +08:00
|
|
|
|
|
|
|
// Get the MacroInfo we want to reinstall.
|
|
|
|
MacroInfo *MacroToReInstall = iter->second.back();
|
|
|
|
|
2015-04-24 04:40:50 +08:00
|
|
|
if (MacroToReInstall)
|
2012-08-30 00:56:24 +08:00
|
|
|
// Reinstall the previously pushed macro.
|
2015-04-24 04:40:50 +08:00
|
|
|
appendDefMacroDirective(IdentInfo, MacroToReInstall, MessageLoc);
|
2010-08-17 23:55:45 +08:00
|
|
|
|
|
|
|
// Pop PragmaPushMacroInfo stack.
|
|
|
|
iter->second.pop_back();
|
2016-09-08 05:53:17 +08:00
|
|
|
if (iter->second.empty())
|
2010-08-17 23:55:45 +08:00
|
|
|
PragmaPushMacroInfo.erase(iter);
|
|
|
|
} else {
|
|
|
|
Diag(MessageLoc, diag::warn_pragma_pop_macro_no_push)
|
|
|
|
<< IdentInfo->getName();
|
|
|
|
}
|
|
|
|
}
|
2006-07-03 06:41:36 +08:00
|
|
|
|
2012-03-03 06:51:54 +08:00
|
|
|
void Preprocessor::HandlePragmaIncludeAlias(Token &Tok) {
|
2018-07-31 03:24:48 +08:00
|
|
|
// We will either get a quoted filename or a bracketed filename, and we
|
2012-03-03 06:51:54 +08:00
|
|
|
// have to track which we got. The first filename is the source name,
|
|
|
|
// and the second name is the mapped filename. If the first is quoted,
|
|
|
|
// the second must be as well (cannot mix and match quotes and brackets).
|
|
|
|
|
|
|
|
// Get the open paren
|
|
|
|
Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
|
|
|
Diag(Tok, diag::warn_pragma_include_alias_expected) << "(";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect either a quoted string literal, or a bracketed name
|
|
|
|
Token SourceFilenameTok;
|
2019-03-19 09:51:19 +08:00
|
|
|
if (LexHeaderName(SourceFilenameTok))
|
2012-03-03 06:51:54 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
StringRef SourceFileName;
|
|
|
|
SmallString<128> FileNameBuffer;
|
2019-03-20 06:09:55 +08:00
|
|
|
if (SourceFilenameTok.is(tok::header_name)) {
|
2012-03-03 06:51:54 +08:00
|
|
|
SourceFileName = getSpelling(SourceFilenameTok, FileNameBuffer);
|
|
|
|
} else {
|
|
|
|
Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FileNameBuffer.clear();
|
|
|
|
|
|
|
|
// Now we expect a comma, followed by another include name
|
|
|
|
Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::comma)) {
|
|
|
|
Diag(Tok, diag::warn_pragma_include_alias_expected) << ",";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token ReplaceFilenameTok;
|
2019-03-19 09:51:19 +08:00
|
|
|
if (LexHeaderName(ReplaceFilenameTok))
|
2012-03-03 06:51:54 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
StringRef ReplaceFileName;
|
2019-03-20 06:09:55 +08:00
|
|
|
if (ReplaceFilenameTok.is(tok::header_name)) {
|
2012-03-03 06:51:54 +08:00
|
|
|
ReplaceFileName = getSpelling(ReplaceFilenameTok, FileNameBuffer);
|
|
|
|
} else {
|
|
|
|
Diag(Tok, diag::warn_pragma_include_alias_expected_filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we expect the closing paren
|
|
|
|
Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::r_paren)) {
|
|
|
|
Diag(Tok, diag::warn_pragma_include_alias_expected) << ")";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have the source and target filenames, we need to make sure
|
|
|
|
// they're both of the same type (angled vs non-angled)
|
|
|
|
StringRef OriginalSource = SourceFileName;
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
bool SourceIsAngled =
|
|
|
|
GetIncludeFilenameSpelling(SourceFilenameTok.getLocation(),
|
2012-03-03 06:51:54 +08:00
|
|
|
SourceFileName);
|
|
|
|
bool ReplaceIsAngled =
|
|
|
|
GetIncludeFilenameSpelling(ReplaceFilenameTok.getLocation(),
|
|
|
|
ReplaceFileName);
|
|
|
|
if (!SourceFileName.empty() && !ReplaceFileName.empty() &&
|
|
|
|
(SourceIsAngled != ReplaceIsAngled)) {
|
|
|
|
unsigned int DiagID;
|
|
|
|
if (SourceIsAngled)
|
|
|
|
DiagID = diag::warn_pragma_include_alias_mismatch_angle;
|
|
|
|
else
|
|
|
|
DiagID = diag::warn_pragma_include_alias_mismatch_quote;
|
|
|
|
|
|
|
|
Diag(SourceFilenameTok.getLocation(), DiagID)
|
2018-07-31 03:24:48 +08:00
|
|
|
<< SourceFileName
|
2012-03-03 06:51:54 +08:00
|
|
|
<< ReplaceFileName;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we can let the include handler know about this mapping
|
|
|
|
getHeaderSearchInfo().AddIncludeAlias(OriginalSource, ReplaceFileName);
|
|
|
|
}
|
|
|
|
|
2017-06-20 07:09:36 +08:00
|
|
|
// Lex a component of a module name: either an identifier or a string literal;
|
|
|
|
// for components that can be expressed both ways, the two forms are equivalent.
|
|
|
|
static bool LexModuleNameComponent(
|
|
|
|
Preprocessor &PP, Token &Tok,
|
|
|
|
std::pair<IdentifierInfo *, SourceLocation> &ModuleNameComponent,
|
|
|
|
bool First) {
|
|
|
|
PP.LexUnexpandedToken(Tok);
|
|
|
|
if (Tok.is(tok::string_literal) && !Tok.hasUDSuffix()) {
|
|
|
|
StringLiteralParser Literal(Tok, PP);
|
|
|
|
if (Literal.hadError)
|
|
|
|
return true;
|
|
|
|
ModuleNameComponent = std::make_pair(
|
|
|
|
PP.getIdentifierInfo(Literal.GetString()), Tok.getLocation());
|
|
|
|
} else if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) {
|
|
|
|
ModuleNameComponent =
|
|
|
|
std::make_pair(Tok.getIdentifierInfo(), Tok.getLocation());
|
|
|
|
} else {
|
|
|
|
PP.Diag(Tok.getLocation(), diag::err_pp_expected_module_name) << First;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool LexModuleName(
|
|
|
|
Preprocessor &PP, Token &Tok,
|
|
|
|
llvm::SmallVectorImpl<std::pair<IdentifierInfo *, SourceLocation>>
|
|
|
|
&ModuleName) {
|
|
|
|
while (true) {
|
|
|
|
std::pair<IdentifierInfo*, SourceLocation> NameComponent;
|
|
|
|
if (LexModuleNameComponent(PP, Tok, NameComponent, ModuleName.empty()))
|
|
|
|
return true;
|
|
|
|
ModuleName.push_back(NameComponent);
|
|
|
|
|
|
|
|
PP.LexUnexpandedToken(Tok);
|
|
|
|
if (Tok.isNot(tok::period))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-10 03:22:32 +08:00
|
|
|
void Preprocessor::HandlePragmaModuleBuild(Token &Tok) {
|
|
|
|
SourceLocation Loc = Tok.getLocation();
|
|
|
|
|
2017-06-20 07:09:36 +08:00
|
|
|
std::pair<IdentifierInfo *, SourceLocation> ModuleNameLoc;
|
|
|
|
if (LexModuleNameComponent(*this, Tok, ModuleNameLoc, true))
|
2017-06-10 03:22:32 +08:00
|
|
|
return;
|
2017-06-20 07:09:36 +08:00
|
|
|
IdentifierInfo *ModuleName = ModuleNameLoc.first;
|
2017-06-10 03:22:32 +08:00
|
|
|
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
if (Tok.isNot(tok::eod)) {
|
|
|
|
Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
|
|
|
|
DiscardUntilEndOfDirective();
|
|
|
|
}
|
|
|
|
|
|
|
|
CurLexer->LexingRawMode = true;
|
|
|
|
|
|
|
|
auto TryConsumeIdentifier = [&](StringRef Ident) -> bool {
|
|
|
|
if (Tok.getKind() != tok::raw_identifier ||
|
|
|
|
Tok.getRawIdentifier() != Ident)
|
|
|
|
return false;
|
|
|
|
CurLexer->Lex(Tok);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Scan forward looking for the end of the module.
|
|
|
|
const char *Start = CurLexer->getBufferLocation();
|
|
|
|
const char *End = nullptr;
|
|
|
|
unsigned NestingLevel = 1;
|
|
|
|
while (true) {
|
|
|
|
End = CurLexer->getBufferLocation();
|
|
|
|
CurLexer->Lex(Tok);
|
|
|
|
|
|
|
|
if (Tok.is(tok::eof)) {
|
|
|
|
Diag(Loc, diag::err_pp_module_build_missing_end);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine()) {
|
|
|
|
// Token was part of module; keep going.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We hit something directive-shaped; check to see if this is the end
|
|
|
|
// of the module build.
|
|
|
|
CurLexer->ParsingPreprocessorDirective = true;
|
|
|
|
CurLexer->Lex(Tok);
|
|
|
|
if (TryConsumeIdentifier("pragma") && TryConsumeIdentifier("clang") &&
|
|
|
|
TryConsumeIdentifier("module")) {
|
|
|
|
if (TryConsumeIdentifier("build"))
|
|
|
|
// #pragma clang module build -> entering a nested module build.
|
|
|
|
++NestingLevel;
|
|
|
|
else if (TryConsumeIdentifier("endbuild")) {
|
|
|
|
// #pragma clang module endbuild -> leaving a module build.
|
|
|
|
if (--NestingLevel == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// We should either be looking at the EOD or more of the current directive
|
|
|
|
// preceding the EOD. Either way we can ignore this token and keep going.
|
|
|
|
assert(Tok.getKind() != tok::eof && "missing EOD before EOF");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CurLexer->LexingRawMode = false;
|
|
|
|
|
|
|
|
// Load the extracted text as a preprocessed module.
|
|
|
|
assert(CurLexer->getBuffer().begin() <= Start &&
|
|
|
|
Start <= CurLexer->getBuffer().end() &&
|
|
|
|
CurLexer->getBuffer().begin() <= End &&
|
|
|
|
End <= CurLexer->getBuffer().end() &&
|
|
|
|
"module source range not contained within same file buffer");
|
|
|
|
TheModuleLoader.loadModuleFromSource(Loc, ModuleName->getName(),
|
|
|
|
StringRef(Start, End - Start));
|
|
|
|
}
|
|
|
|
|
2018-09-12 01:10:44 +08:00
|
|
|
void Preprocessor::HandlePragmaHdrstop(Token &Tok) {
|
|
|
|
Lex(Tok);
|
|
|
|
if (Tok.is(tok::l_paren)) {
|
|
|
|
Diag(Tok.getLocation(), diag::warn_pp_hdrstop_filename_ignored);
|
|
|
|
|
|
|
|
std::string FileName;
|
|
|
|
if (!LexStringLiteral(Tok, FileName, "pragma hdrstop", false))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::r_paren)) {
|
|
|
|
Diag(Tok, diag::err_expected) << tok::r_paren;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Lex(Tok);
|
|
|
|
}
|
|
|
|
if (Tok.isNot(tok::eod))
|
|
|
|
Diag(Tok.getLocation(), diag::ext_pp_extra_tokens_at_eol)
|
|
|
|
<< "pragma hdrstop";
|
|
|
|
|
|
|
|
if (creatingPCHWithPragmaHdrStop() &&
|
|
|
|
SourceMgr.isInMainFile(Tok.getLocation())) {
|
|
|
|
assert(CurLexer && "no lexer for #pragma hdrstop processing");
|
|
|
|
Token &Result = Tok;
|
|
|
|
Result.startToken();
|
|
|
|
CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
|
|
|
|
CurLexer->cutOffLexing();
|
|
|
|
}
|
|
|
|
if (usingPCHWithPragmaHdrStop())
|
|
|
|
SkippingUntilPragmaHdrStop = false;
|
|
|
|
}
|
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
|
|
|
|
/// If 'Namespace' is non-null, then it is a token required to exist on the
|
|
|
|
/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
|
2011-07-23 18:55:15 +08:00
|
|
|
void Preprocessor::AddPragmaHandler(StringRef Namespace,
|
2006-07-03 06:41:36 +08:00
|
|
|
PragmaHandler *Handler) {
|
2014-09-12 13:19:24 +08:00
|
|
|
PragmaNamespace *InsertNS = PragmaHandlers.get();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
// If this is specified to be in a namespace, step down into it.
|
2010-07-13 17:07:17 +08:00
|
|
|
if (!Namespace.empty()) {
|
2006-07-03 06:41:36 +08:00
|
|
|
// If there is already a pragma handler with the name of this namespace,
|
|
|
|
// we either have an error (directive with the same name as a namespace) or
|
|
|
|
// we already have the namespace to insert into.
|
2010-07-13 17:07:17 +08:00
|
|
|
if (PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace)) {
|
2006-07-03 06:41:36 +08:00
|
|
|
InsertNS = Existing->getIfNamespace();
|
2014-05-18 07:10:59 +08:00
|
|
|
assert(InsertNS != nullptr && "Cannot have a pragma namespace and pragma"
|
2006-07-03 06:41:36 +08:00
|
|
|
" handler with the same name!");
|
|
|
|
} else {
|
|
|
|
// Otherwise, this namespace doesn't exist yet, create and insert the
|
|
|
|
// handler for it.
|
2010-07-13 17:07:17 +08:00
|
|
|
InsertNS = new PragmaNamespace(Namespace);
|
2006-07-03 06:41:36 +08:00
|
|
|
PragmaHandlers->AddPragma(InsertNS);
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
// Check to make sure we don't already have a pragma for this identifier.
|
|
|
|
assert(!InsertNS->FindHandler(Handler->getName()) &&
|
|
|
|
"Pragma handler already exists for this identifier!");
|
|
|
|
InsertNS->AddPragma(Handler);
|
|
|
|
}
|
|
|
|
|
2008-10-05 03:17:46 +08:00
|
|
|
/// RemovePragmaHandler - Remove the specific pragma handler from the
|
|
|
|
/// preprocessor. If \arg Namespace is non-null, then it should be the
|
|
|
|
/// namespace that \arg Handler was added to. It is an error to remove
|
|
|
|
/// a handler that has not been registered.
|
2011-07-23 18:55:15 +08:00
|
|
|
void Preprocessor::RemovePragmaHandler(StringRef Namespace,
|
2008-10-05 03:17:46 +08:00
|
|
|
PragmaHandler *Handler) {
|
2014-09-12 13:19:24 +08:00
|
|
|
PragmaNamespace *NS = PragmaHandlers.get();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-05 03:17:46 +08:00
|
|
|
// If this is specified to be in a namespace, step down into it.
|
2010-07-13 17:07:17 +08:00
|
|
|
if (!Namespace.empty()) {
|
|
|
|
PragmaHandler *Existing = PragmaHandlers->FindHandler(Namespace);
|
2008-10-05 03:17:46 +08:00
|
|
|
assert(Existing && "Namespace containing handler does not exist!");
|
|
|
|
|
|
|
|
NS = Existing->getIfNamespace();
|
|
|
|
assert(NS && "Invalid namespace, registered as a regular pragma handler!");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS->RemovePragmaHandler(Handler);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-09-12 13:19:24 +08:00
|
|
|
// If this is a non-default namespace and it is now empty, remove it.
|
|
|
|
if (NS != PragmaHandlers.get() && NS->IsEmpty()) {
|
2008-10-05 03:17:46 +08:00
|
|
|
PragmaHandlers->RemovePragmaHandler(NS);
|
2012-01-06 08:22:09 +08:00
|
|
|
delete NS;
|
|
|
|
}
|
2008-10-05 03:17:46 +08:00
|
|
|
}
|
|
|
|
|
2011-02-14 09:42:24 +08:00
|
|
|
bool Preprocessor::LexOnOffSwitch(tok::OnOffSwitch &Result) {
|
|
|
|
Token Tok;
|
|
|
|
LexUnexpandedToken(Tok);
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::identifier)) {
|
|
|
|
Diag(Tok, diag::ext_on_off_switch_syntax);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
IdentifierInfo *II = Tok.getIdentifierInfo();
|
|
|
|
if (II->isStr("ON"))
|
|
|
|
Result = tok::OOS_ON;
|
|
|
|
else if (II->isStr("OFF"))
|
|
|
|
Result = tok::OOS_OFF;
|
|
|
|
else if (II->isStr("DEFAULT"))
|
|
|
|
Result = tok::OOS_DEFAULT;
|
|
|
|
else {
|
|
|
|
Diag(Tok, diag::ext_on_off_switch_syntax);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-28 10:37:51 +08:00
|
|
|
// Verify that this is followed by EOD.
|
2011-02-14 09:42:24 +08:00
|
|
|
LexUnexpandedToken(Tok);
|
2011-02-28 10:37:51 +08:00
|
|
|
if (Tok.isNot(tok::eod))
|
|
|
|
Diag(Tok, diag::ext_pragma_syntax_eod);
|
2011-02-14 09:42:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
namespace {
|
2016-09-08 05:53:17 +08:00
|
|
|
|
2012-06-17 11:26:26 +08:00
|
|
|
/// PragmaOnceHandler - "\#pragma once" marks the file as atomically included.
|
2006-07-03 06:41:36 +08:00
|
|
|
struct PragmaOnceHandler : public PragmaHandler {
|
2010-07-13 17:07:17 +08:00
|
|
|
PragmaOnceHandler() : PragmaHandler("once") {}
|
2017-12-09 06:39:26 +08:00
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2014-03-11 14:50:42 +08:00
|
|
|
Token &OnceTok) override {
|
2009-04-14 13:07:49 +08:00
|
|
|
PP.CheckEndOfDirective("pragma once");
|
2006-07-03 06:41:36 +08:00
|
|
|
PP.HandlePragmaOnce(OnceTok);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-17 11:26:26 +08:00
|
|
|
/// PragmaMarkHandler - "\#pragma mark ..." is ignored by the compiler, and the
|
2007-12-20 03:38:36 +08:00
|
|
|
/// rest of the line is not lexed.
|
|
|
|
struct PragmaMarkHandler : public PragmaHandler {
|
2010-07-13 17:07:17 +08:00
|
|
|
PragmaMarkHandler() : PragmaHandler("mark") {}
|
2016-09-08 05:53:17 +08:00
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2014-03-11 14:50:42 +08:00
|
|
|
Token &MarkTok) override {
|
2007-12-20 03:38:36 +08:00
|
|
|
PP.HandlePragmaMark();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-17 11:26:26 +08:00
|
|
|
/// PragmaPoisonHandler - "\#pragma poison x" marks x as not usable.
|
2006-07-03 06:41:36 +08:00
|
|
|
struct PragmaPoisonHandler : public PragmaHandler {
|
2010-07-13 17:07:17 +08:00
|
|
|
PragmaPoisonHandler() : PragmaHandler("poison") {}
|
2016-09-08 05:53:17 +08:00
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2014-03-11 14:50:42 +08:00
|
|
|
Token &PoisonTok) override {
|
2016-10-26 19:46:10 +08:00
|
|
|
PP.HandlePragmaPoison();
|
2006-07-03 06:41:36 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-17 11:26:26 +08:00
|
|
|
/// PragmaSystemHeaderHandler - "\#pragma system_header" marks the current file
|
2007-12-20 03:38:36 +08:00
|
|
|
/// as a system header, which silences warnings in it.
|
2006-07-03 06:41:36 +08:00
|
|
|
struct PragmaSystemHeaderHandler : public PragmaHandler {
|
2010-07-13 17:07:17 +08:00
|
|
|
PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
|
2016-09-08 05:53:17 +08:00
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2014-03-11 14:50:42 +08:00
|
|
|
Token &SHToken) override {
|
2006-07-03 06:41:36 +08:00
|
|
|
PP.HandlePragmaSystemHeader(SHToken);
|
2009-04-14 13:07:49 +08:00
|
|
|
PP.CheckEndOfDirective("pragma");
|
2006-07-03 06:41:36 +08:00
|
|
|
}
|
|
|
|
};
|
2016-09-08 05:53:17 +08:00
|
|
|
|
2006-07-03 06:41:36 +08:00
|
|
|
struct PragmaDependencyHandler : public PragmaHandler {
|
2010-07-13 17:07:17 +08:00
|
|
|
PragmaDependencyHandler() : PragmaHandler("dependency") {}
|
2016-09-08 05:53:17 +08:00
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2014-03-11 14:50:42 +08:00
|
|
|
Token &DepToken) override {
|
2006-07-03 06:41:36 +08:00
|
|
|
PP.HandlePragmaDependency(DepToken);
|
|
|
|
}
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-07-28 23:40:33 +08:00
|
|
|
struct PragmaDebugHandler : public PragmaHandler {
|
|
|
|
PragmaDebugHandler() : PragmaHandler("__debug") {}
|
2016-09-08 05:53:17 +08:00
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2019-04-18 08:57:01 +08:00
|
|
|
Token &DebugToken) override {
|
2010-07-28 23:40:33 +08:00
|
|
|
Token Tok;
|
|
|
|
PP.LexUnexpandedToken(Tok);
|
|
|
|
if (Tok.isNot(tok::identifier)) {
|
2010-08-30 23:15:34 +08:00
|
|
|
PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
|
2010-07-28 23:40:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
IdentifierInfo *II = Tok.getIdentifierInfo();
|
|
|
|
|
2010-08-18 06:32:48 +08:00
|
|
|
if (II->isStr("assert")) {
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("This is an assertion!");
|
2010-07-28 23:40:33 +08:00
|
|
|
} else if (II->isStr("crash")) {
|
2012-08-22 02:56:49 +08:00
|
|
|
LLVM_BUILTIN_TRAP;
|
2012-06-30 06:03:56 +08:00
|
|
|
} else if (II->isStr("parser_crash")) {
|
|
|
|
Token Crasher;
|
2015-03-09 03:28:24 +08:00
|
|
|
Crasher.startToken();
|
2012-06-30 06:03:56 +08:00
|
|
|
Crasher.setKind(tok::annot_pragma_parser_crash);
|
2015-03-09 03:28:24 +08:00
|
|
|
Crasher.setAnnotationRange(SourceRange(Tok.getLocation()));
|
2019-05-17 17:32:05 +08:00
|
|
|
PP.EnterToken(Crasher, /*IsReinject*/false);
|
2016-01-13 05:59:26 +08:00
|
|
|
} else if (II->isStr("dump")) {
|
|
|
|
Token Identifier;
|
|
|
|
PP.LexUnexpandedToken(Identifier);
|
|
|
|
if (auto *DumpII = Identifier.getIdentifierInfo()) {
|
|
|
|
Token DumpAnnot;
|
|
|
|
DumpAnnot.startToken();
|
|
|
|
DumpAnnot.setKind(tok::annot_pragma_dump);
|
|
|
|
DumpAnnot.setAnnotationRange(
|
|
|
|
SourceRange(Tok.getLocation(), Identifier.getLocation()));
|
|
|
|
DumpAnnot.setAnnotationValue(DumpII);
|
|
|
|
PP.DiscardUntilEndOfDirective();
|
2019-05-17 17:32:05 +08:00
|
|
|
PP.EnterToken(DumpAnnot, /*IsReinject*/false);
|
2016-01-13 05:59:26 +08:00
|
|
|
} else {
|
|
|
|
PP.Diag(Identifier, diag::warn_pragma_debug_missing_argument)
|
|
|
|
<< II->getName();
|
|
|
|
}
|
2018-02-09 09:15:13 +08:00
|
|
|
} else if (II->isStr("diag_mapping")) {
|
|
|
|
Token DiagName;
|
|
|
|
PP.LexUnexpandedToken(DiagName);
|
|
|
|
if (DiagName.is(tok::eod))
|
|
|
|
PP.getDiagnostics().dump();
|
|
|
|
else if (DiagName.is(tok::string_literal) && !DiagName.hasUDSuffix()) {
|
|
|
|
StringLiteralParser Literal(DiagName, PP);
|
|
|
|
if (Literal.hadError)
|
|
|
|
return;
|
|
|
|
PP.getDiagnostics().dump(Literal.GetString());
|
|
|
|
} else {
|
|
|
|
PP.Diag(DiagName, diag::warn_pragma_debug_missing_argument)
|
|
|
|
<< II->getName();
|
|
|
|
}
|
2010-08-18 06:32:48 +08:00
|
|
|
} else if (II->isStr("llvm_fatal_error")) {
|
|
|
|
llvm::report_fatal_error("#pragma clang __debug llvm_fatal_error");
|
|
|
|
} else if (II->isStr("llvm_unreachable")) {
|
|
|
|
llvm_unreachable("#pragma clang __debug llvm_unreachable");
|
2015-05-01 07:10:40 +08:00
|
|
|
} else if (II->isStr("macro")) {
|
|
|
|
Token MacroName;
|
|
|
|
PP.LexUnexpandedToken(MacroName);
|
|
|
|
auto *MacroII = MacroName.getIdentifierInfo();
|
|
|
|
if (MacroII)
|
|
|
|
PP.dumpMacroInfo(MacroII);
|
|
|
|
else
|
2016-01-13 05:59:26 +08:00
|
|
|
PP.Diag(MacroName, diag::warn_pragma_debug_missing_argument)
|
|
|
|
<< II->getName();
|
2019-04-18 08:57:01 +08:00
|
|
|
} else if (II->isStr("module_map")) {
|
|
|
|
llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
|
|
|
|
ModuleName;
|
|
|
|
if (LexModuleName(PP, Tok, ModuleName))
|
|
|
|
return;
|
|
|
|
ModuleMap &MM = PP.getHeaderSearchInfo().getModuleMap();
|
|
|
|
Module *M = nullptr;
|
|
|
|
for (auto IIAndLoc : ModuleName) {
|
|
|
|
M = MM.lookupModuleQualified(IIAndLoc.first->getName(), M);
|
|
|
|
if (!M) {
|
|
|
|
PP.Diag(IIAndLoc.second, diag::warn_pragma_debug_unknown_module)
|
|
|
|
<< IIAndLoc.first;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
M->dump();
|
2010-08-18 06:32:48 +08:00
|
|
|
} else if (II->isStr("overflow_stack")) {
|
|
|
|
DebugOverflowStack();
|
2010-08-19 07:09:23 +08:00
|
|
|
} else if (II->isStr("handle_crash")) {
|
|
|
|
llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
|
|
|
|
if (CRC)
|
|
|
|
CRC->HandleCrash();
|
2013-04-17 02:41:26 +08:00
|
|
|
} else if (II->isStr("captured")) {
|
|
|
|
HandleCaptured(PP);
|
2010-08-18 06:32:48 +08:00
|
|
|
} else {
|
|
|
|
PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
|
|
|
|
<< II->getName();
|
2010-07-28 23:40:33 +08:00
|
|
|
}
|
2013-04-17 02:41:26 +08:00
|
|
|
|
|
|
|
PPCallbacks *Callbacks = PP.getPPCallbacks();
|
|
|
|
if (Callbacks)
|
|
|
|
Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandleCaptured(Preprocessor &PP) {
|
|
|
|
Token Tok;
|
|
|
|
PP.LexUnexpandedToken(Tok);
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::eod)) {
|
|
|
|
PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
|
|
|
|
<< "pragma clang __debug captured";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceLocation NameLoc = Tok.getLocation();
|
2016-02-10 02:52:09 +08:00
|
|
|
MutableArrayRef<Token> Toks(
|
|
|
|
PP.getPreprocessorAllocator().Allocate<Token>(1), 1);
|
|
|
|
Toks[0].startToken();
|
|
|
|
Toks[0].setKind(tok::annot_pragma_captured);
|
|
|
|
Toks[0].setLocation(NameLoc);
|
2013-04-17 02:41:26 +08:00
|
|
|
|
2019-05-17 17:32:05 +08:00
|
|
|
PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
|
|
|
|
/*IsReinject=*/false);
|
2010-07-28 23:40:33 +08:00
|
|
|
}
|
|
|
|
|
2011-05-26 00:15:03 +08:00
|
|
|
// Disable MSVC warning about runtime stack overflow.
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(disable : 4717)
|
|
|
|
#endif
|
2017-04-25 02:41:00 +08:00
|
|
|
static void DebugOverflowStack(void (*P)() = nullptr) {
|
|
|
|
void (*volatile Self)(void(*P)()) = DebugOverflowStack;
|
|
|
|
Self(reinterpret_cast<void(*)()>(Self));
|
2010-07-28 23:40:33 +08:00
|
|
|
}
|
2011-05-26 00:15:03 +08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(default : 4717)
|
|
|
|
#endif
|
2010-07-28 23:40:33 +08:00
|
|
|
};
|
|
|
|
|
2012-06-17 11:26:26 +08:00
|
|
|
/// PragmaDiagnosticHandler - e.g. '\#pragma GCC diagnostic ignored "-Wformat"'
|
2009-04-20 07:16:58 +08:00
|
|
|
struct PragmaDiagnosticHandler : public PragmaHandler {
|
2011-06-23 03:41:48 +08:00
|
|
|
private:
|
|
|
|
const char *Namespace;
|
2016-09-08 05:53:17 +08:00
|
|
|
|
2009-07-13 05:18:45 +08:00
|
|
|
public:
|
2017-12-09 06:39:26 +08:00
|
|
|
explicit PragmaDiagnosticHandler(const char *NS)
|
|
|
|
: PragmaHandler("diagnostic"), Namespace(NS) {}
|
2016-09-08 05:53:17 +08:00
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2014-03-11 14:50:42 +08:00
|
|
|
Token &DiagToken) override {
|
2010-12-16 02:44:22 +08:00
|
|
|
SourceLocation DiagLoc = DiagToken.getLocation();
|
2009-04-20 07:16:58 +08:00
|
|
|
Token Tok;
|
|
|
|
PP.LexUnexpandedToken(Tok);
|
|
|
|
if (Tok.isNot(tok::identifier)) {
|
2010-08-30 23:15:34 +08:00
|
|
|
PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
|
2009-04-20 07:16:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
IdentifierInfo *II = Tok.getIdentifierInfo();
|
2011-06-23 03:41:48 +08:00
|
|
|
PPCallbacks *Callbacks = PP.getPPCallbacks();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-06-12 18:15:20 +08:00
|
|
|
if (II->isStr("pop")) {
|
2010-12-16 02:44:22 +08:00
|
|
|
if (!PP.getDiagnostics().popMappings(DiagLoc))
|
2010-08-30 23:15:34 +08:00
|
|
|
PP.Diag(Tok, diag::warn_pragma_diagnostic_cannot_pop);
|
2011-06-23 03:41:48 +08:00
|
|
|
else if (Callbacks)
|
|
|
|
Callbacks->PragmaDiagnosticPop(DiagLoc, Namespace);
|
2010-08-30 23:15:34 +08:00
|
|
|
return;
|
|
|
|
} else if (II->isStr("push")) {
|
2010-12-16 02:44:22 +08:00
|
|
|
PP.getDiagnostics().pushMappings(DiagLoc);
|
2011-06-23 03:41:48 +08:00
|
|
|
if (Callbacks)
|
|
|
|
Callbacks->PragmaDiagnosticPush(DiagLoc, Namespace);
|
2009-07-13 05:18:45 +08:00
|
|
|
return;
|
2014-06-12 18:15:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
diag::Severity SV = llvm::StringSwitch<diag::Severity>(II->getName())
|
|
|
|
.Case("ignored", diag::Severity::Ignored)
|
|
|
|
.Case("warning", diag::Severity::Warning)
|
|
|
|
.Case("error", diag::Severity::Error)
|
|
|
|
.Case("fatal", diag::Severity::Fatal)
|
|
|
|
.Default(diag::Severity());
|
|
|
|
|
|
|
|
if (SV == diag::Severity()) {
|
2010-08-30 23:15:34 +08:00
|
|
|
PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid);
|
2009-04-20 07:16:58 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-20 07:16:58 +08:00
|
|
|
PP.LexUnexpandedToken(Tok);
|
2012-11-18 03:15:38 +08:00
|
|
|
SourceLocation StringLoc = Tok.getLocation();
|
2009-04-20 07:16:58 +08:00
|
|
|
|
2012-11-18 03:15:38 +08:00
|
|
|
std::string WarningName;
|
2012-11-18 03:16:52 +08:00
|
|
|
if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
|
2019-07-16 12:46:31 +08:00
|
|
|
/*AllowMacroExpansion=*/false))
|
2009-04-20 07:16:58 +08:00
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-28 10:37:51 +08:00
|
|
|
if (Tok.isNot(tok::eod)) {
|
2009-04-20 07:16:58 +08:00
|
|
|
PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-20 07:16:58 +08:00
|
|
|
if (WarningName.size() < 3 || WarningName[0] != '-' ||
|
Use -Rblah, not -Wblah, to control remark diagnostics. This was always the
intent when we added remark support, but was never implemented in the general
case, because the first -R flags didn't need it. (-Rpass= had special handling
to accomodate its argument.)
-Rno-foo, -Reverything, and -Rno-everything can be used to turn off a remark,
or to turn on or off all remarks. Per discussion on cfe-commits, -Weverything
does not affect remarks, and -Reverything does not affect warnings or errors.
The only "real" -R flag we have right now is -Rmodule-build; that flag is
effectively renamed from -Wmodule-build to -Rmodule-build by this change.
-Wpass and -Wno-pass (and their friends) are also renamed to -Rpass and
-Rno-pass by this change; it's not completely clear whether we intended to have
a -Rpass (with no =pattern), but that is unchanged by this commit, other than
the flag name. The default pattern is effectively one which matches no passes.
In future, we may want to make the default pattern be .*, so that -Reverything
works for -Rpass properly.
llvm-svn: 215046
2014-08-07 08:24:21 +08:00
|
|
|
(WarningName[1] != 'W' && WarningName[1] != 'R')) {
|
2012-11-18 03:15:38 +08:00
|
|
|
PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option);
|
2009-04-20 07:16:58 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2016-02-13 09:44:05 +08:00
|
|
|
diag::Flavor Flavor = WarningName[1] == 'W' ? diag::Flavor::WarningOrError
|
|
|
|
: diag::Flavor::Remark;
|
2016-02-13 21:42:41 +08:00
|
|
|
StringRef Group = StringRef(WarningName).substr(2);
|
2016-02-13 09:44:05 +08:00
|
|
|
bool unknownDiag = false;
|
|
|
|
if (Group == "everything") {
|
|
|
|
// Special handling for pragma clang diagnostic ... "-Weverything".
|
|
|
|
// There is no formal group named "everything", so there has to be a
|
|
|
|
// special case for it.
|
|
|
|
PP.getDiagnostics().setSeverityForAll(Flavor, SV, DiagLoc);
|
|
|
|
} else
|
|
|
|
unknownDiag = PP.getDiagnostics().setSeverityForGroup(Flavor, Group, SV,
|
|
|
|
DiagLoc);
|
|
|
|
if (unknownDiag)
|
2012-11-18 03:15:38 +08:00
|
|
|
PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning)
|
|
|
|
<< WarningName;
|
2011-06-23 03:41:48 +08:00
|
|
|
else if (Callbacks)
|
2014-06-12 18:15:20 +08:00
|
|
|
Callbacks->PragmaDiagnostic(DiagLoc, Namespace, SV, WarningName);
|
2009-04-20 07:16:58 +08:00
|
|
|
}
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2018-09-12 01:10:44 +08:00
|
|
|
/// "\#pragma hdrstop [<header-name-string>]"
|
|
|
|
struct PragmaHdrstopHandler : public PragmaHandler {
|
|
|
|
PragmaHdrstopHandler() : PragmaHandler("hdrstop") {}
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2018-09-12 01:10:44 +08:00
|
|
|
Token &DepToken) override {
|
|
|
|
PP.HandlePragmaHdrstop(DepToken);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-14 06:00:30 +08:00
|
|
|
/// "\#pragma warning(...)". MSVC's diagnostics do not map cleanly to clang's
|
|
|
|
/// diagnostics, so we don't really implement this pragma. We parse it and
|
|
|
|
/// ignore it to avoid -Wunknown-pragma warnings.
|
|
|
|
struct PragmaWarningHandler : public PragmaHandler {
|
|
|
|
PragmaWarningHandler() : PragmaHandler("warning") {}
|
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2014-03-11 14:50:42 +08:00
|
|
|
Token &Tok) override {
|
2013-09-14 06:00:30 +08:00
|
|
|
// Parse things like:
|
|
|
|
// warning(push, 1)
|
|
|
|
// warning(pop)
|
2013-11-16 08:16:03 +08:00
|
|
|
// warning(disable : 1 2 3 ; error : 4 5 6 ; suppress : 7 8 9)
|
2013-09-14 06:00:30 +08:00
|
|
|
SourceLocation DiagLoc = Tok.getLocation();
|
|
|
|
PPCallbacks *Callbacks = PP.getPPCallbacks();
|
|
|
|
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
|
|
|
PP.Diag(Tok, diag::warn_pragma_warning_expected) << "(";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PP.Lex(Tok);
|
|
|
|
IdentifierInfo *II = Tok.getIdentifierInfo();
|
|
|
|
|
2015-05-25 19:21:20 +08:00
|
|
|
if (II && II->isStr("push")) {
|
2013-09-14 06:00:30 +08:00
|
|
|
// #pragma warning( push[ ,n ] )
|
2013-10-02 23:19:23 +08:00
|
|
|
int Level = -1;
|
2013-09-14 06:00:30 +08:00
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.is(tok::comma)) {
|
|
|
|
PP.Lex(Tok);
|
2014-02-13 07:50:26 +08:00
|
|
|
uint64_t Value;
|
|
|
|
if (Tok.is(tok::numeric_constant) &&
|
|
|
|
PP.parseSimpleIntegerLiteral(Tok, Value))
|
|
|
|
Level = int(Value);
|
2013-10-02 23:19:23 +08:00
|
|
|
if (Level < 0 || Level > 4) {
|
2013-09-14 06:00:30 +08:00
|
|
|
PP.Diag(Tok, diag::warn_pragma_warning_push_level);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Callbacks)
|
|
|
|
Callbacks->PragmaWarningPush(DiagLoc, Level);
|
2015-05-25 19:21:20 +08:00
|
|
|
} else if (II && II->isStr("pop")) {
|
2013-09-14 06:00:30 +08:00
|
|
|
// #pragma warning( pop )
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Callbacks)
|
|
|
|
Callbacks->PragmaWarningPop(DiagLoc);
|
|
|
|
} else {
|
|
|
|
// #pragma warning( warning-specifier : warning-number-list
|
|
|
|
// [; warning-specifier : warning-number-list...] )
|
|
|
|
while (true) {
|
|
|
|
II = Tok.getIdentifierInfo();
|
2015-05-25 19:21:20 +08:00
|
|
|
if (!II && !Tok.is(tok::numeric_constant)) {
|
2013-09-14 06:00:30 +08:00
|
|
|
PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out which warning specifier this is.
|
2015-05-25 19:21:20 +08:00
|
|
|
bool SpecifierValid;
|
|
|
|
StringRef Specifier;
|
|
|
|
llvm::SmallString<1> SpecifierBuf;
|
|
|
|
if (II) {
|
|
|
|
Specifier = II->getName();
|
|
|
|
SpecifierValid = llvm::StringSwitch<bool>(Specifier)
|
|
|
|
.Cases("default", "disable", "error", "once",
|
|
|
|
"suppress", true)
|
|
|
|
.Default(false);
|
|
|
|
// If we read a correct specifier, snatch next token (that should be
|
|
|
|
// ":", checked later).
|
|
|
|
if (SpecifierValid)
|
|
|
|
PP.Lex(Tok);
|
|
|
|
} else {
|
|
|
|
// Token is a numeric constant. It should be either 1, 2, 3 or 4.
|
|
|
|
uint64_t Value;
|
|
|
|
Specifier = PP.getSpelling(Tok, SpecifierBuf);
|
|
|
|
if (PP.parseSimpleIntegerLiteral(Tok, Value)) {
|
|
|
|
SpecifierValid = (Value >= 1) && (Value <= 4);
|
|
|
|
} else
|
|
|
|
SpecifierValid = false;
|
|
|
|
// Next token already snatched by parseSimpleIntegerLiteral.
|
|
|
|
}
|
|
|
|
|
2013-09-14 06:00:30 +08:00
|
|
|
if (!SpecifierValid) {
|
|
|
|
PP.Diag(Tok, diag::warn_pragma_warning_spec_invalid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Tok.isNot(tok::colon)) {
|
|
|
|
PP.Diag(Tok, diag::warn_pragma_warning_expected) << ":";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect the warning ids.
|
|
|
|
SmallVector<int, 4> Ids;
|
|
|
|
PP.Lex(Tok);
|
|
|
|
while (Tok.is(tok::numeric_constant)) {
|
2014-02-13 07:50:26 +08:00
|
|
|
uint64_t Value;
|
|
|
|
if (!PP.parseSimpleIntegerLiteral(Tok, Value) || Value == 0 ||
|
2016-09-08 05:53:17 +08:00
|
|
|
Value > std::numeric_limits<int>::max()) {
|
2013-09-14 06:00:30 +08:00
|
|
|
PP.Diag(Tok, diag::warn_pragma_warning_expected_number);
|
|
|
|
return;
|
|
|
|
}
|
2014-02-13 07:50:26 +08:00
|
|
|
Ids.push_back(int(Value));
|
2013-09-14 06:00:30 +08:00
|
|
|
}
|
|
|
|
if (Callbacks)
|
|
|
|
Callbacks->PragmaWarning(DiagLoc, Specifier, Ids);
|
|
|
|
|
|
|
|
// Parse the next specifier if there is a semicolon.
|
|
|
|
if (Tok.isNot(tok::semi))
|
|
|
|
break;
|
|
|
|
PP.Lex(Tok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::r_paren)) {
|
|
|
|
PP.Diag(Tok, diag::warn_pragma_warning_expected) << ")";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::eod))
|
|
|
|
PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma warning";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-15 02:12:17 +08:00
|
|
|
/// "\#pragma execution_character_set(...)". MSVC supports this pragma only
|
|
|
|
/// for "UTF-8". We parse it and ignore it if UTF-8 is provided and warn
|
|
|
|
/// otherwise to avoid -Wunknown-pragma warnings.
|
|
|
|
struct PragmaExecCharsetHandler : public PragmaHandler {
|
|
|
|
PragmaExecCharsetHandler() : PragmaHandler("execution_character_set") {}
|
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2019-03-15 02:12:17 +08:00
|
|
|
Token &Tok) override {
|
|
|
|
// Parse things like:
|
|
|
|
// execution_character_set(push, "UTF-8")
|
|
|
|
// execution_character_set(pop)
|
|
|
|
SourceLocation DiagLoc = Tok.getLocation();
|
|
|
|
PPCallbacks *Callbacks = PP.getPPCallbacks();
|
|
|
|
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
|
|
|
PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << "(";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PP.Lex(Tok);
|
|
|
|
IdentifierInfo *II = Tok.getIdentifierInfo();
|
|
|
|
|
|
|
|
if (II && II->isStr("push")) {
|
|
|
|
// #pragma execution_character_set( push[ , string ] )
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.is(tok::comma)) {
|
|
|
|
PP.Lex(Tok);
|
|
|
|
|
|
|
|
std::string ExecCharset;
|
|
|
|
if (!PP.FinishLexStringLiteral(Tok, ExecCharset,
|
|
|
|
"pragma execution_character_set",
|
2019-07-16 12:46:31 +08:00
|
|
|
/*AllowMacroExpansion=*/false))
|
2019-03-15 02:12:17 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// MSVC supports either of these, but nothing else.
|
|
|
|
if (ExecCharset != "UTF-8" && ExecCharset != "utf-8") {
|
|
|
|
PP.Diag(Tok, diag::warn_pragma_exec_charset_push_invalid) << ExecCharset;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Callbacks)
|
|
|
|
Callbacks->PragmaExecCharsetPush(DiagLoc, "UTF-8");
|
|
|
|
} else if (II && II->isStr("pop")) {
|
|
|
|
// #pragma execution_character_set( pop )
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Callbacks)
|
|
|
|
Callbacks->PragmaExecCharsetPop(DiagLoc);
|
|
|
|
} else {
|
|
|
|
PP.Diag(Tok, diag::warn_pragma_exec_charset_spec_invalid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::r_paren)) {
|
|
|
|
PP.Diag(Tok, diag::warn_pragma_exec_charset_expected) << ")";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.isNot(tok::eod))
|
|
|
|
PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma execution_character_set";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-17 11:26:26 +08:00
|
|
|
/// PragmaIncludeAliasHandler - "\#pragma include_alias("...")".
|
2012-03-03 06:51:54 +08:00
|
|
|
struct PragmaIncludeAliasHandler : public PragmaHandler {
|
|
|
|
PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
|
2017-12-09 06:39:26 +08:00
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2014-03-11 14:50:42 +08:00
|
|
|
Token &IncludeAliasTok) override {
|
2013-09-14 06:00:30 +08:00
|
|
|
PP.HandlePragmaIncludeAlias(IncludeAliasTok);
|
2012-03-03 06:51:54 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-18 00:16:16 +08:00
|
|
|
/// PragmaMessageHandler - Handle the microsoft and gcc \#pragma message
|
|
|
|
/// extension. The syntax is:
|
|
|
|
/// \code
|
|
|
|
/// #pragma message(string)
|
|
|
|
/// \endcode
|
|
|
|
/// OR, in GCC mode:
|
|
|
|
/// \code
|
|
|
|
/// #pragma message string
|
|
|
|
/// \endcode
|
|
|
|
/// string is a string, which is fully macro expanded, and permits string
|
|
|
|
/// concatenation, embedded escape characters, etc... See MSDN for more details.
|
|
|
|
/// Also handles \#pragma GCC warning and \#pragma GCC error which take the same
|
|
|
|
/// form as \#pragma message.
|
2010-06-27 01:11:39 +08:00
|
|
|
struct PragmaMessageHandler : public PragmaHandler {
|
2013-04-18 00:16:16 +08:00
|
|
|
private:
|
|
|
|
const PPCallbacks::PragmaMessageKind Kind;
|
|
|
|
const StringRef Namespace;
|
|
|
|
|
|
|
|
static const char* PragmaKind(PPCallbacks::PragmaMessageKind Kind,
|
|
|
|
bool PragmaNameOnly = false) {
|
|
|
|
switch (Kind) {
|
|
|
|
case PPCallbacks::PMK_Message:
|
|
|
|
return PragmaNameOnly ? "message" : "pragma message";
|
|
|
|
case PPCallbacks::PMK_Warning:
|
|
|
|
return PragmaNameOnly ? "warning" : "pragma warning";
|
|
|
|
case PPCallbacks::PMK_Error:
|
|
|
|
return PragmaNameOnly ? "error" : "pragma error";
|
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown PragmaMessageKind!");
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
PragmaMessageHandler(PPCallbacks::PragmaMessageKind Kind,
|
|
|
|
StringRef Namespace = StringRef())
|
2017-12-09 06:39:26 +08:00
|
|
|
: PragmaHandler(PragmaKind(Kind, true)), Kind(Kind),
|
|
|
|
Namespace(Namespace) {}
|
2013-04-18 00:16:16 +08:00
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2014-03-11 14:50:42 +08:00
|
|
|
Token &Tok) override {
|
2013-04-18 00:16:16 +08:00
|
|
|
SourceLocation MessageLoc = Tok.getLocation();
|
|
|
|
PP.Lex(Tok);
|
|
|
|
bool ExpectClosingParen = false;
|
|
|
|
switch (Tok.getKind()) {
|
|
|
|
case tok::l_paren:
|
|
|
|
// We have a MSVC style pragma message.
|
|
|
|
ExpectClosingParen = true;
|
|
|
|
// Read the string.
|
|
|
|
PP.Lex(Tok);
|
|
|
|
break;
|
|
|
|
case tok::string_literal:
|
|
|
|
// We have a GCC style pragma message, and we just read the string.
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PP.Diag(MessageLoc, diag::err_pragma_message_malformed) << Kind;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string MessageString;
|
|
|
|
if (!PP.FinishLexStringLiteral(Tok, MessageString, PragmaKind(Kind),
|
2019-07-16 12:46:31 +08:00
|
|
|
/*AllowMacroExpansion=*/true))
|
2013-04-18 00:16:16 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (ExpectClosingParen) {
|
|
|
|
if (Tok.isNot(tok::r_paren)) {
|
|
|
|
PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PP.Lex(Tok); // eat the r_paren.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::eod)) {
|
|
|
|
PP.Diag(Tok.getLocation(), diag::err_pragma_message_malformed) << Kind;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output the message.
|
|
|
|
PP.Diag(MessageLoc, (Kind == PPCallbacks::PMK_Error)
|
|
|
|
? diag::err_pragma_message
|
|
|
|
: diag::warn_pragma_message) << MessageString;
|
|
|
|
|
|
|
|
// If the pragma is lexically sound, notify any interested PPCallbacks.
|
|
|
|
if (PPCallbacks *Callbacks = PP.getPPCallbacks())
|
|
|
|
Callbacks->PragmaMessage(MessageLoc, Namespace, Kind, MessageString);
|
2010-06-27 01:11:39 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-29 08:34:47 +08:00
|
|
|
/// Handle the clang \#pragma module import extension. The syntax is:
|
|
|
|
/// \code
|
|
|
|
/// #pragma clang module import some.module.name
|
|
|
|
/// \endcode
|
|
|
|
struct PragmaModuleImportHandler : public PragmaHandler {
|
|
|
|
PragmaModuleImportHandler() : PragmaHandler("import") {}
|
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2017-05-04 08:29:54 +08:00
|
|
|
Token &Tok) override {
|
|
|
|
SourceLocation ImportLoc = Tok.getLocation();
|
|
|
|
|
|
|
|
// Read the module name.
|
|
|
|
llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
|
|
|
|
ModuleName;
|
|
|
|
if (LexModuleName(PP, Tok, ModuleName))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::eod))
|
|
|
|
PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
|
|
|
|
|
|
|
|
// If we have a non-empty module path, load the named module.
|
|
|
|
Module *Imported =
|
|
|
|
PP.getModuleLoader().loadModule(ImportLoc, ModuleName, Module::Hidden,
|
2019-07-16 12:46:31 +08:00
|
|
|
/*IsInclusionDirective=*/false);
|
2017-05-04 08:29:54 +08:00
|
|
|
if (!Imported)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PP.makeModuleVisible(Imported, ImportLoc);
|
|
|
|
PP.EnterAnnotationToken(SourceRange(ImportLoc, ModuleName.back().second),
|
|
|
|
tok::annot_module_include, Imported);
|
|
|
|
if (auto *CB = PP.getPPCallbacks())
|
|
|
|
CB->moduleImport(ImportLoc, ModuleName, Imported);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Handle the clang \#pragma module begin extension. The syntax is:
|
|
|
|
/// \code
|
|
|
|
/// #pragma clang module begin some.module.name
|
|
|
|
/// ...
|
|
|
|
/// #pragma clang module end
|
|
|
|
/// \endcode
|
|
|
|
struct PragmaModuleBeginHandler : public PragmaHandler {
|
|
|
|
PragmaModuleBeginHandler() : PragmaHandler("begin") {}
|
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2017-05-04 08:29:54 +08:00
|
|
|
Token &Tok) override {
|
|
|
|
SourceLocation BeginLoc = Tok.getLocation();
|
|
|
|
|
|
|
|
// Read the module name.
|
|
|
|
llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
|
|
|
|
ModuleName;
|
|
|
|
if (LexModuleName(PP, Tok, ModuleName))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::eod))
|
|
|
|
PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
|
|
|
|
|
|
|
|
// We can only enter submodules of the current module.
|
|
|
|
StringRef Current = PP.getLangOpts().CurrentModule;
|
|
|
|
if (ModuleName.front().first->getName() != Current) {
|
|
|
|
PP.Diag(ModuleName.front().second, diag::err_pp_module_begin_wrong_module)
|
|
|
|
<< ModuleName.front().first << (ModuleName.size() > 1)
|
|
|
|
<< Current.empty() << Current;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the module we're entering. We require that a module map for it
|
|
|
|
// be loaded or implicitly loadable.
|
2019-05-08 05:38:51 +08:00
|
|
|
auto &HSI = PP.getHeaderSearchInfo();
|
|
|
|
Module *M = HSI.lookupModule(Current);
|
2017-05-04 08:29:54 +08:00
|
|
|
if (!M) {
|
|
|
|
PP.Diag(ModuleName.front().second,
|
|
|
|
diag::err_pp_module_begin_no_module_map) << Current;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (unsigned I = 1; I != ModuleName.size(); ++I) {
|
2019-05-08 05:38:51 +08:00
|
|
|
auto *NewM = M->findOrInferSubmodule(ModuleName[I].first->getName());
|
2017-05-04 08:29:54 +08:00
|
|
|
if (!NewM) {
|
|
|
|
PP.Diag(ModuleName[I].second, diag::err_pp_module_begin_no_submodule)
|
|
|
|
<< M->getFullModuleName() << ModuleName[I].first;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
M = NewM;
|
|
|
|
}
|
|
|
|
|
2017-05-30 13:22:59 +08:00
|
|
|
// If the module isn't available, it doesn't make sense to enter it.
|
2017-06-06 02:57:56 +08:00
|
|
|
if (Preprocessor::checkModuleIsAvailable(
|
|
|
|
PP.getLangOpts(), PP.getTargetInfo(), PP.getDiagnostics(), M)) {
|
2017-05-30 13:22:59 +08:00
|
|
|
PP.Diag(BeginLoc, diag::note_pp_module_begin_here)
|
|
|
|
<< M->getTopLevelModuleName();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-04 08:29:54 +08:00
|
|
|
// Enter the scope of the submodule.
|
|
|
|
PP.EnterSubmodule(M, BeginLoc, /*ForPragma*/true);
|
|
|
|
PP.EnterAnnotationToken(SourceRange(BeginLoc, ModuleName.back().second),
|
|
|
|
tok::annot_module_begin, M);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Handle the clang \#pragma module end extension.
|
|
|
|
struct PragmaModuleEndHandler : public PragmaHandler {
|
|
|
|
PragmaModuleEndHandler() : PragmaHandler("end") {}
|
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2017-05-04 08:29:54 +08:00
|
|
|
Token &Tok) override {
|
|
|
|
SourceLocation Loc = Tok.getLocation();
|
|
|
|
|
|
|
|
PP.LexUnexpandedToken(Tok);
|
|
|
|
if (Tok.isNot(tok::eod))
|
|
|
|
PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
|
|
|
|
|
|
|
|
Module *M = PP.LeaveSubmodule(/*ForPragma*/true);
|
|
|
|
if (M)
|
|
|
|
PP.EnterAnnotationToken(SourceRange(Loc), tok::annot_module_end, M);
|
|
|
|
else
|
|
|
|
PP.Diag(Loc, diag::err_pp_module_end_without_module_begin);
|
2017-04-29 08:34:47 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-10 03:22:32 +08:00
|
|
|
/// Handle the clang \#pragma module build extension.
|
|
|
|
struct PragmaModuleBuildHandler : public PragmaHandler {
|
|
|
|
PragmaModuleBuildHandler() : PragmaHandler("build") {}
|
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2017-06-10 03:22:32 +08:00
|
|
|
Token &Tok) override {
|
|
|
|
PP.HandlePragmaModuleBuild(Tok);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Handle the clang \#pragma module load extension.
|
|
|
|
struct PragmaModuleLoadHandler : public PragmaHandler {
|
|
|
|
PragmaModuleLoadHandler() : PragmaHandler("load") {}
|
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2017-06-10 03:22:32 +08:00
|
|
|
Token &Tok) override {
|
|
|
|
SourceLocation Loc = Tok.getLocation();
|
|
|
|
|
|
|
|
// Read the module name.
|
|
|
|
llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 8>
|
|
|
|
ModuleName;
|
|
|
|
if (LexModuleName(PP, Tok, ModuleName))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::eod))
|
|
|
|
PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
|
|
|
|
|
|
|
|
// Load the module, don't make it visible.
|
|
|
|
PP.getModuleLoader().loadModule(Loc, ModuleName, Module::Hidden,
|
2019-07-16 12:46:31 +08:00
|
|
|
/*IsInclusionDirective=*/false);
|
2017-06-10 03:22:32 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-17 11:26:26 +08:00
|
|
|
/// PragmaPushMacroHandler - "\#pragma push_macro" saves the value of the
|
2010-08-17 23:55:45 +08:00
|
|
|
/// macro on the top of the stack.
|
|
|
|
struct PragmaPushMacroHandler : public PragmaHandler {
|
|
|
|
PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
|
2016-09-08 05:53:17 +08:00
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2014-03-11 14:50:42 +08:00
|
|
|
Token &PushMacroTok) override {
|
2010-08-17 23:55:45 +08:00
|
|
|
PP.HandlePragmaPushMacro(PushMacroTok);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-17 11:26:26 +08:00
|
|
|
/// PragmaPopMacroHandler - "\#pragma pop_macro" sets the value of the
|
2010-08-17 23:55:45 +08:00
|
|
|
/// macro to the value on the top of the stack.
|
|
|
|
struct PragmaPopMacroHandler : public PragmaHandler {
|
|
|
|
PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
|
2016-09-08 05:53:17 +08:00
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2014-03-11 14:50:42 +08:00
|
|
|
Token &PopMacroTok) override {
|
2010-08-17 23:55:45 +08:00
|
|
|
PP.HandlePragmaPopMacro(PopMacroTok);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
/// PragmaARCCFCodeAuditedHandler -
|
2012-06-17 11:26:26 +08:00
|
|
|
/// \#pragma clang arc_cf_code_audited begin/end
|
2011-09-30 13:12:12 +08:00
|
|
|
struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
|
|
|
|
PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
|
2016-09-08 05:53:17 +08:00
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2014-03-11 14:50:42 +08:00
|
|
|
Token &NameTok) override {
|
2011-09-30 13:12:12 +08:00
|
|
|
SourceLocation Loc = NameTok.getLocation();
|
|
|
|
bool IsBegin;
|
|
|
|
|
|
|
|
Token Tok;
|
|
|
|
|
|
|
|
// Lex the 'begin' or 'end'.
|
|
|
|
PP.LexUnexpandedToken(Tok);
|
|
|
|
const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
|
|
|
|
if (BeginEnd && BeginEnd->isStr("begin")) {
|
|
|
|
IsBegin = true;
|
|
|
|
} else if (BeginEnd && BeginEnd->isStr("end")) {
|
|
|
|
IsBegin = false;
|
|
|
|
} else {
|
|
|
|
PP.Diag(Tok.getLocation(), diag::err_pp_arc_cf_code_audited_syntax);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that this is followed by EOD.
|
|
|
|
PP.LexUnexpandedToken(Tok);
|
|
|
|
if (Tok.isNot(tok::eod))
|
|
|
|
PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
|
|
|
|
|
|
|
|
// The start location of the active audit.
|
|
|
|
SourceLocation BeginLoc = PP.getPragmaARCCFCodeAuditedLoc();
|
|
|
|
|
|
|
|
// The start location we want after processing this.
|
|
|
|
SourceLocation NewLoc;
|
|
|
|
|
|
|
|
if (IsBegin) {
|
|
|
|
// Complain about attempts to re-enter an audit.
|
|
|
|
if (BeginLoc.isValid()) {
|
|
|
|
PP.Diag(Loc, diag::err_pp_double_begin_of_arc_cf_code_audited);
|
|
|
|
PP.Diag(BeginLoc, diag::note_pragma_entered_here);
|
|
|
|
}
|
|
|
|
NewLoc = Loc;
|
|
|
|
} else {
|
|
|
|
// Complain about attempts to leave an audit that doesn't exist.
|
|
|
|
if (!BeginLoc.isValid()) {
|
|
|
|
PP.Diag(Loc, diag::err_pp_unmatched_end_of_arc_cf_code_audited);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NewLoc = SourceLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
PP.setPragmaARCCFCodeAuditedLoc(NewLoc);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-20 02:25:57 +08:00
|
|
|
/// PragmaAssumeNonNullHandler -
|
|
|
|
/// \#pragma clang assume_nonnull begin/end
|
|
|
|
struct PragmaAssumeNonNullHandler : public PragmaHandler {
|
|
|
|
PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {}
|
2016-09-08 05:53:17 +08:00
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2015-06-20 02:25:57 +08:00
|
|
|
Token &NameTok) override {
|
|
|
|
SourceLocation Loc = NameTok.getLocation();
|
|
|
|
bool IsBegin;
|
|
|
|
|
|
|
|
Token Tok;
|
|
|
|
|
|
|
|
// Lex the 'begin' or 'end'.
|
|
|
|
PP.LexUnexpandedToken(Tok);
|
|
|
|
const IdentifierInfo *BeginEnd = Tok.getIdentifierInfo();
|
|
|
|
if (BeginEnd && BeginEnd->isStr("begin")) {
|
|
|
|
IsBegin = true;
|
|
|
|
} else if (BeginEnd && BeginEnd->isStr("end")) {
|
|
|
|
IsBegin = false;
|
|
|
|
} else {
|
|
|
|
PP.Diag(Tok.getLocation(), diag::err_pp_assume_nonnull_syntax);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that this is followed by EOD.
|
|
|
|
PP.LexUnexpandedToken(Tok);
|
|
|
|
if (Tok.isNot(tok::eod))
|
|
|
|
PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol) << "pragma";
|
|
|
|
|
|
|
|
// The start location of the active audit.
|
|
|
|
SourceLocation BeginLoc = PP.getPragmaAssumeNonNullLoc();
|
|
|
|
|
|
|
|
// The start location we want after processing this.
|
|
|
|
SourceLocation NewLoc;
|
2017-09-28 07:29:37 +08:00
|
|
|
PPCallbacks *Callbacks = PP.getPPCallbacks();
|
2015-06-20 02:25:57 +08:00
|
|
|
|
|
|
|
if (IsBegin) {
|
|
|
|
// Complain about attempts to re-enter an audit.
|
|
|
|
if (BeginLoc.isValid()) {
|
|
|
|
PP.Diag(Loc, diag::err_pp_double_begin_of_assume_nonnull);
|
|
|
|
PP.Diag(BeginLoc, diag::note_pragma_entered_here);
|
|
|
|
}
|
|
|
|
NewLoc = Loc;
|
2017-09-28 07:29:37 +08:00
|
|
|
if (Callbacks)
|
|
|
|
Callbacks->PragmaAssumeNonNullBegin(NewLoc);
|
2015-06-20 02:25:57 +08:00
|
|
|
} else {
|
|
|
|
// Complain about attempts to leave an audit that doesn't exist.
|
|
|
|
if (!BeginLoc.isValid()) {
|
|
|
|
PP.Diag(Loc, diag::err_pp_unmatched_end_of_assume_nonnull);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NewLoc = SourceLocation();
|
2017-09-28 07:29:37 +08:00
|
|
|
if (Callbacks)
|
|
|
|
Callbacks->PragmaAssumeNonNullEnd(NewLoc);
|
2015-06-20 02:25:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PP.setPragmaAssumeNonNullLoc(NewLoc);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Handle "\#pragma region [...]"
|
2013-06-30 16:18:16 +08:00
|
|
|
///
|
|
|
|
/// The syntax is
|
|
|
|
/// \code
|
|
|
|
/// #pragma region [optional name]
|
|
|
|
/// #pragma endregion [optional comment]
|
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// \note This is
|
|
|
|
/// <a href="http://msdn.microsoft.com/en-us/library/b6xkz944(v=vs.80).aspx">editor-only</a>
|
|
|
|
/// pragma, just skipped by compiler.
|
|
|
|
struct PragmaRegionHandler : public PragmaHandler {
|
2017-12-09 06:39:26 +08:00
|
|
|
PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) {}
|
2013-06-30 16:18:16 +08:00
|
|
|
|
2019-05-22 07:51:38 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
|
2014-03-11 14:50:42 +08:00
|
|
|
Token &NameTok) override {
|
2013-06-30 16:18:16 +08:00
|
|
|
// #pragma region: endregion matches can be verified
|
|
|
|
// __pragma(region): no sense, but ignored by msvc
|
|
|
|
// _Pragma is not valid for MSVC, but there isn't any point
|
|
|
|
// to handle a _Pragma differently.
|
|
|
|
}
|
|
|
|
};
|
2012-12-01 03:52:30 +08:00
|
|
|
|
2017-12-09 06:39:26 +08:00
|
|
|
} // namespace
|
2006-07-03 06:41:36 +08:00
|
|
|
|
|
|
|
/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
|
2012-06-17 11:26:26 +08:00
|
|
|
/// \#pragma GCC poison/system_header/dependency and \#pragma once.
|
2006-07-03 06:41:36 +08:00
|
|
|
void Preprocessor::RegisterBuiltinPragmas() {
|
2010-07-13 17:07:17 +08:00
|
|
|
AddPragmaHandler(new PragmaOnceHandler());
|
|
|
|
AddPragmaHandler(new PragmaMarkHandler());
|
2010-08-17 23:55:45 +08:00
|
|
|
AddPragmaHandler(new PragmaPushMacroHandler());
|
|
|
|
AddPragmaHandler(new PragmaPopMacroHandler());
|
2013-04-18 00:16:16 +08:00
|
|
|
AddPragmaHandler(new PragmaMessageHandler(PPCallbacks::PMK_Message));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-13 02:21:11 +08:00
|
|
|
// #pragma GCC ...
|
2010-07-13 17:07:17 +08:00
|
|
|
AddPragmaHandler("GCC", new PragmaPoisonHandler());
|
|
|
|
AddPragmaHandler("GCC", new PragmaSystemHeaderHandler());
|
|
|
|
AddPragmaHandler("GCC", new PragmaDependencyHandler());
|
2011-06-23 03:41:48 +08:00
|
|
|
AddPragmaHandler("GCC", new PragmaDiagnosticHandler("GCC"));
|
2013-04-18 00:16:16 +08:00
|
|
|
AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Warning,
|
|
|
|
"GCC"));
|
|
|
|
AddPragmaHandler("GCC", new PragmaMessageHandler(PPCallbacks::PMK_Error,
|
|
|
|
"GCC"));
|
2009-05-13 02:21:11 +08:00
|
|
|
// #pragma clang ...
|
2010-07-13 17:07:17 +08:00
|
|
|
AddPragmaHandler("clang", new PragmaPoisonHandler());
|
|
|
|
AddPragmaHandler("clang", new PragmaSystemHeaderHandler());
|
2010-07-28 23:40:33 +08:00
|
|
|
AddPragmaHandler("clang", new PragmaDebugHandler());
|
2010-07-13 17:07:17 +08:00
|
|
|
AddPragmaHandler("clang", new PragmaDependencyHandler());
|
2011-06-23 03:41:48 +08:00
|
|
|
AddPragmaHandler("clang", new PragmaDiagnosticHandler("clang"));
|
2011-09-30 13:12:12 +08:00
|
|
|
AddPragmaHandler("clang", new PragmaARCCFCodeAuditedHandler());
|
2015-06-20 02:25:57 +08:00
|
|
|
AddPragmaHandler("clang", new PragmaAssumeNonNullHandler());
|
2010-07-13 17:07:17 +08:00
|
|
|
|
2017-04-29 08:34:47 +08:00
|
|
|
// #pragma clang module ...
|
|
|
|
auto *ModuleHandler = new PragmaNamespace("module");
|
|
|
|
AddPragmaHandler("clang", ModuleHandler);
|
|
|
|
ModuleHandler->AddPragma(new PragmaModuleImportHandler());
|
2017-05-04 08:29:54 +08:00
|
|
|
ModuleHandler->AddPragma(new PragmaModuleBeginHandler());
|
|
|
|
ModuleHandler->AddPragma(new PragmaModuleEndHandler());
|
2017-06-10 03:22:32 +08:00
|
|
|
ModuleHandler->AddPragma(new PragmaModuleBuildHandler());
|
|
|
|
ModuleHandler->AddPragma(new PragmaModuleLoadHandler());
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2018-01-27 08:25:29 +08:00
|
|
|
// Add region pragmas.
|
|
|
|
AddPragmaHandler(new PragmaRegionHandler("region"));
|
|
|
|
AddPragmaHandler(new PragmaRegionHandler("endregion"));
|
2017-04-29 08:34:47 +08:00
|
|
|
|
2009-01-16 16:21:25 +08:00
|
|
|
// MS extensions.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (LangOpts.MicrosoftExt) {
|
2013-09-14 06:00:30 +08:00
|
|
|
AddPragmaHandler(new PragmaWarningHandler());
|
2019-03-15 02:12:17 +08:00
|
|
|
AddPragmaHandler(new PragmaExecCharsetHandler());
|
2012-03-03 06:51:54 +08:00
|
|
|
AddPragmaHandler(new PragmaIncludeAliasHandler());
|
2018-09-12 01:10:44 +08:00
|
|
|
AddPragmaHandler(new PragmaHdrstopHandler());
|
2010-06-27 01:11:39 +08:00
|
|
|
}
|
2016-04-04 22:22:58 +08:00
|
|
|
|
|
|
|
// Pragmas added by plugins
|
|
|
|
for (PragmaHandlerRegistry::iterator it = PragmaHandlerRegistry::begin(),
|
|
|
|
ie = PragmaHandlerRegistry::end();
|
|
|
|
it != ie; ++it) {
|
|
|
|
AddPragmaHandler(it->instantiate().release());
|
|
|
|
}
|
2006-07-03 06:41:36 +08:00
|
|
|
}
|
2014-05-01 20:54:03 +08:00
|
|
|
|
|
|
|
/// Ignore all pragmas, useful for modes such as -Eonly which would otherwise
|
|
|
|
/// warn about those pragmas being unknown.
|
|
|
|
void Preprocessor::IgnorePragmas() {
|
|
|
|
AddPragmaHandler(new EmptyPragmaHandler());
|
|
|
|
// Also ignore all pragmas in all namespaces created
|
|
|
|
// in Preprocessor::RegisterBuiltinPragmas().
|
|
|
|
AddPragmaHandler("GCC", new EmptyPragmaHandler());
|
|
|
|
AddPragmaHandler("clang", new EmptyPragmaHandler());
|
|
|
|
}
|