Skip to content

Commit ddde0ec

Browse files
committedMay 21, 2019
[PragmaHandler] Expose #pragma location
Currently, a pragma AST node's recorded location starts at the namespace token (such as `omp` in the case of OpenMP) after the `#pragma` token, and the `#pragma` location isn't available. However, the `#pragma` location can be useful when, for example, rewriting a directive using Clang's Rewrite facility. This patch makes `#pragma` locations available in any `PragmaHandler` but it doesn't yet make use of them. This patch also uses the new `struct PragmaIntroducer` to simplify `Preprocessor::HandlePragmaDirective`. It doesn't do the same for `PPCallbacks::PragmaDirective` because that changes the API documented in `clang-tools-extra/docs/pp-trace.rst`, and I'm not sure about backward compatibility guarantees there. Reviewed By: ABataev, lebedev.ri, aaron.ballman Differential Revision: https://reviews.llvm.org/D61643 llvm-svn: 361335
1 parent b541730 commit ddde0ec

File tree

8 files changed

+112
-111
lines changed

8 files changed

+112
-111
lines changed
 

‎clang/docs/ClangPlugins.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ registering it using ``PragmaHandlerRegistry::Add<>``:
5555
class ExamplePragmaHandler : public PragmaHandler {
5656
public:
5757
ExamplePragmaHandler() : PragmaHandler("example_pragma") { }
58-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
58+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
5959
Token &PragmaTok) {
6060
// Handle the pragma
6161
}

‎clang/examples/AnnotateFunctions/AnnotateFunctions.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class PragmaAnnotateHandler : public PragmaHandler {
5858
public:
5959
PragmaAnnotateHandler() : PragmaHandler("enable_annotate") { }
6060

61-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
61+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
6262
Token &PragmaTok) override {
6363

6464
Token Tok;

‎clang/include/clang/Lex/Pragma.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVM_CLANG_LEX_PRAGMA_H
1515

1616
#include "clang/Basic/LLVM.h"
17+
#include "clang/Basic/SourceLocation.h"
1718
#include "llvm/ADT/StringMap.h"
1819
#include "llvm/ADT/StringRef.h"
1920
#include <string>
@@ -46,6 +47,12 @@ class Token;
4647
PIK___pragma
4748
};
4849

50+
/// Describes how and where the pragma was introduced.
51+
struct PragmaIntroducer {
52+
PragmaIntroducerKind Kind;
53+
SourceLocation Loc;
54+
};
55+
4956
/// PragmaHandler - Instances of this interface defined to handle the various
5057
/// pragmas that the language front-end uses. Each handler optionally has a
5158
/// name (e.g. "pack") and the HandlePragma method is invoked when a pragma with
@@ -64,7 +71,7 @@ class PragmaHandler {
6471
virtual ~PragmaHandler();
6572

6673
StringRef getName() const { return Name; }
67-
virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
74+
virtual void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
6875
Token &FirstToken) = 0;
6976

7077
/// getIfNamespace - If this is a namespace, return it. This is equivalent to
@@ -78,7 +85,7 @@ class EmptyPragmaHandler : public PragmaHandler {
7885
public:
7986
explicit EmptyPragmaHandler(StringRef Name = StringRef());
8087

81-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
88+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
8289
Token &FirstToken) override;
8390
};
8491

@@ -111,7 +118,7 @@ class PragmaNamespace : public PragmaHandler {
111118

112119
bool IsEmpty() const { return Handlers.empty(); }
113120

114-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
121+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
115122
Token &Tok) override;
116123

117124
PragmaNamespace *getIfNamespace() override { return this; }

‎clang/include/clang/Lex/Preprocessor.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -2291,8 +2291,7 @@ class Preprocessor {
22912291
void HandleElifDirective(Token &ElifToken, const Token &HashToken);
22922292

22932293
// Pragmas.
2294-
void HandlePragmaDirective(SourceLocation IntroducerLoc,
2295-
PragmaIntroducerKind Introducer);
2294+
void HandlePragmaDirective(PragmaIntroducer Introducer);
22962295

22972296
public:
22982297
void HandlePragmaOnce(Token &OnceTok);

‎clang/lib/Frontend/PrintPreprocessedOutput.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ struct UnknownPragmaHandler : public PragmaHandler {
664664
bool RequireTokenExpansion)
665665
: Prefix(prefix), Callbacks(callbacks),
666666
ShouldExpandTokens(RequireTokenExpansion) {}
667-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
667+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
668668
Token &PragmaTok) override {
669669
// Figure out what line we went to and insert the appropriate number of
670670
// newline characters.

‎clang/lib/Lex/PPDirectives.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ void Preprocessor::HandleDirective(Token &Result) {
982982

983983
// C99 6.10.6 - Pragma Directive.
984984
case tok::pp_pragma:
985-
return HandlePragmaDirective(SavedHash.getLocation(), PIK_HashPragma);
985+
return HandlePragmaDirective({PIK_HashPragma, SavedHash.getLocation()});
986986

987987
// GNU Extensions.
988988
case tok::pp_import:

‎clang/lib/Lex/Pragma.cpp

+28-30
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ PragmaHandler::~PragmaHandler() = default;
6363
EmptyPragmaHandler::EmptyPragmaHandler(StringRef Name) : PragmaHandler(Name) {}
6464

6565
void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
66-
PragmaIntroducerKind Introducer,
66+
PragmaIntroducer Introducer,
6767
Token &FirstToken) {}
6868

6969
//===----------------------------------------------------------------------===//
@@ -98,8 +98,7 @@ void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
9898
}
9999

100100
void PragmaNamespace::HandlePragma(Preprocessor &PP,
101-
PragmaIntroducerKind Introducer,
102-
Token &Tok) {
101+
PragmaIntroducer Introducer, Token &Tok) {
103102
// Read the 'namespace' that the directive is in, e.g. STDC. Do not macro
104103
// expand it, the user can have a STDC #define, that should not affect this.
105104
PP.LexUnexpandedToken(Tok);
@@ -124,10 +123,9 @@ void PragmaNamespace::HandlePragma(Preprocessor &PP,
124123

125124
/// HandlePragmaDirective - The "\#pragma" directive has been parsed. Lex the
126125
/// rest of the pragma, passing it to the registered pragma handlers.
127-
void Preprocessor::HandlePragmaDirective(SourceLocation IntroducerLoc,
128-
PragmaIntroducerKind Introducer) {
126+
void Preprocessor::HandlePragmaDirective(PragmaIntroducer Introducer) {
129127
if (Callbacks)
130-
Callbacks->PragmaDirective(IntroducerLoc, Introducer);
128+
Callbacks->PragmaDirective(Introducer.Loc, Introducer.Kind);
131129

132130
if (!PragmasEnabled)
133131
return;
@@ -321,7 +319,7 @@ void Preprocessor::Handle_Pragma(Token &Tok) {
321319
EnterSourceFileWithLexer(TL, nullptr);
322320

323321
// With everything set up, lex this as a #pragma directive.
324-
HandlePragmaDirective(PragmaLoc, PIK__Pragma);
322+
HandlePragmaDirective({PIK__Pragma, PragmaLoc});
325323

326324
// Finally, return whatever came after the pragma directive.
327325
return Lex(Tok);
@@ -371,7 +369,7 @@ void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
371369
/*IsReinject*/ false);
372370

373371
// With everything set up, lex this as a #pragma directive.
374-
HandlePragmaDirective(PragmaLoc, PIK___pragma);
372+
HandlePragmaDirective({PIK___pragma, PragmaLoc});
375373

376374
// Finally, return whatever came after the pragma directive.
377375
return Lex(Tok);
@@ -959,7 +957,7 @@ namespace {
959957
struct PragmaOnceHandler : public PragmaHandler {
960958
PragmaOnceHandler() : PragmaHandler("once") {}
961959

962-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
960+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
963961
Token &OnceTok) override {
964962
PP.CheckEndOfDirective("pragma once");
965963
PP.HandlePragmaOnce(OnceTok);
@@ -971,7 +969,7 @@ struct PragmaOnceHandler : public PragmaHandler {
971969
struct PragmaMarkHandler : public PragmaHandler {
972970
PragmaMarkHandler() : PragmaHandler("mark") {}
973971

974-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
972+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
975973
Token &MarkTok) override {
976974
PP.HandlePragmaMark();
977975
}
@@ -981,7 +979,7 @@ struct PragmaMarkHandler : public PragmaHandler {
981979
struct PragmaPoisonHandler : public PragmaHandler {
982980
PragmaPoisonHandler() : PragmaHandler("poison") {}
983981

984-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
982+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
985983
Token &PoisonTok) override {
986984
PP.HandlePragmaPoison();
987985
}
@@ -992,7 +990,7 @@ struct PragmaPoisonHandler : public PragmaHandler {
992990
struct PragmaSystemHeaderHandler : public PragmaHandler {
993991
PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}
994992

995-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
993+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
996994
Token &SHToken) override {
997995
PP.HandlePragmaSystemHeader(SHToken);
998996
PP.CheckEndOfDirective("pragma");
@@ -1002,7 +1000,7 @@ struct PragmaSystemHeaderHandler : public PragmaHandler {
10021000
struct PragmaDependencyHandler : public PragmaHandler {
10031001
PragmaDependencyHandler() : PragmaHandler("dependency") {}
10041002

1005-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1003+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
10061004
Token &DepToken) override {
10071005
PP.HandlePragmaDependency(DepToken);
10081006
}
@@ -1011,7 +1009,7 @@ struct PragmaDependencyHandler : public PragmaHandler {
10111009
struct PragmaDebugHandler : public PragmaHandler {
10121010
PragmaDebugHandler() : PragmaHandler("__debug") {}
10131011

1014-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1012+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
10151013
Token &DebugToken) override {
10161014
Token Tok;
10171015
PP.LexUnexpandedToken(Tok);
@@ -1151,7 +1149,7 @@ struct PragmaDiagnosticHandler : public PragmaHandler {
11511149
explicit PragmaDiagnosticHandler(const char *NS)
11521150
: PragmaHandler("diagnostic"), Namespace(NS) {}
11531151

1154-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1152+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
11551153
Token &DiagToken) override {
11561154
SourceLocation DiagLoc = DiagToken.getLocation();
11571155
Token Tok;
@@ -1230,7 +1228,7 @@ struct PragmaDiagnosticHandler : public PragmaHandler {
12301228
/// "\#pragma hdrstop [<header-name-string>]"
12311229
struct PragmaHdrstopHandler : public PragmaHandler {
12321230
PragmaHdrstopHandler() : PragmaHandler("hdrstop") {}
1233-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1231+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
12341232
Token &DepToken) override {
12351233
PP.HandlePragmaHdrstop(DepToken);
12361234
}
@@ -1242,7 +1240,7 @@ struct PragmaHdrstopHandler : public PragmaHandler {
12421240
struct PragmaWarningHandler : public PragmaHandler {
12431241
PragmaWarningHandler() : PragmaHandler("warning") {}
12441242

1245-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1243+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
12461244
Token &Tok) override {
12471245
// Parse things like:
12481246
// warning(push, 1)
@@ -1365,7 +1363,7 @@ struct PragmaWarningHandler : public PragmaHandler {
13651363
struct PragmaExecCharsetHandler : public PragmaHandler {
13661364
PragmaExecCharsetHandler() : PragmaHandler("execution_character_set") {}
13671365

1368-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1366+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
13691367
Token &Tok) override {
13701368
// Parse things like:
13711369
// execution_character_set(push, "UTF-8")
@@ -1427,7 +1425,7 @@ struct PragmaExecCharsetHandler : public PragmaHandler {
14271425
struct PragmaIncludeAliasHandler : public PragmaHandler {
14281426
PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}
14291427

1430-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1428+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
14311429
Token &IncludeAliasTok) override {
14321430
PP.HandlePragmaIncludeAlias(IncludeAliasTok);
14331431
}
@@ -1470,7 +1468,7 @@ struct PragmaMessageHandler : public PragmaHandler {
14701468
: PragmaHandler(PragmaKind(Kind, true)), Kind(Kind),
14711469
Namespace(Namespace) {}
14721470

1473-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1471+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
14741472
Token &Tok) override {
14751473
SourceLocation MessageLoc = Tok.getLocation();
14761474
PP.Lex(Tok);
@@ -1526,7 +1524,7 @@ struct PragmaMessageHandler : public PragmaHandler {
15261524
struct PragmaModuleImportHandler : public PragmaHandler {
15271525
PragmaModuleImportHandler() : PragmaHandler("import") {}
15281526

1529-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1527+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
15301528
Token &Tok) override {
15311529
SourceLocation ImportLoc = Tok.getLocation();
15321530

@@ -1563,7 +1561,7 @@ struct PragmaModuleImportHandler : public PragmaHandler {
15631561
struct PragmaModuleBeginHandler : public PragmaHandler {
15641562
PragmaModuleBeginHandler() : PragmaHandler("begin") {}
15651563

1566-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1564+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
15671565
Token &Tok) override {
15681566
SourceLocation BeginLoc = Tok.getLocation();
15691567

@@ -1623,7 +1621,7 @@ struct PragmaModuleBeginHandler : public PragmaHandler {
16231621
struct PragmaModuleEndHandler : public PragmaHandler {
16241622
PragmaModuleEndHandler() : PragmaHandler("end") {}
16251623

1626-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1624+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
16271625
Token &Tok) override {
16281626
SourceLocation Loc = Tok.getLocation();
16291627

@@ -1643,7 +1641,7 @@ struct PragmaModuleEndHandler : public PragmaHandler {
16431641
struct PragmaModuleBuildHandler : public PragmaHandler {
16441642
PragmaModuleBuildHandler() : PragmaHandler("build") {}
16451643

1646-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1644+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
16471645
Token &Tok) override {
16481646
PP.HandlePragmaModuleBuild(Tok);
16491647
}
@@ -1653,7 +1651,7 @@ struct PragmaModuleBuildHandler : public PragmaHandler {
16531651
struct PragmaModuleLoadHandler : public PragmaHandler {
16541652
PragmaModuleLoadHandler() : PragmaHandler("load") {}
16551653

1656-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1654+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
16571655
Token &Tok) override {
16581656
SourceLocation Loc = Tok.getLocation();
16591657

@@ -1677,7 +1675,7 @@ struct PragmaModuleLoadHandler : public PragmaHandler {
16771675
struct PragmaPushMacroHandler : public PragmaHandler {
16781676
PragmaPushMacroHandler() : PragmaHandler("push_macro") {}
16791677

1680-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1678+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
16811679
Token &PushMacroTok) override {
16821680
PP.HandlePragmaPushMacro(PushMacroTok);
16831681
}
@@ -1688,7 +1686,7 @@ struct PragmaPushMacroHandler : public PragmaHandler {
16881686
struct PragmaPopMacroHandler : public PragmaHandler {
16891687
PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}
16901688

1691-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1689+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
16921690
Token &PopMacroTok) override {
16931691
PP.HandlePragmaPopMacro(PopMacroTok);
16941692
}
@@ -1699,7 +1697,7 @@ struct PragmaPopMacroHandler : public PragmaHandler {
16991697
struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
17001698
PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}
17011699

1702-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1700+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
17031701
Token &NameTok) override {
17041702
SourceLocation Loc = NameTok.getLocation();
17051703
bool IsBegin;
@@ -1754,7 +1752,7 @@ struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
17541752
struct PragmaAssumeNonNullHandler : public PragmaHandler {
17551753
PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {}
17561754

1757-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1755+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
17581756
Token &NameTok) override {
17591757
SourceLocation Loc = NameTok.getLocation();
17601758
bool IsBegin;
@@ -1823,7 +1821,7 @@ struct PragmaAssumeNonNullHandler : public PragmaHandler {
18231821
struct PragmaRegionHandler : public PragmaHandler {
18241822
PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) {}
18251823

1826-
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
1824+
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
18271825
Token &NameTok) override {
18281826
// #pragma region: endregion matches can be verified
18291827
// __pragma(region): no sense, but ignored by msvc

‎clang/lib/Parse/ParsePragma.cpp

+69-72
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.