Skip to content

Commit

Permalink
[PragmaHandler] Expose #pragma location
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jdenny-ornl committed May 21, 2019
1 parent b541730 commit ddde0ec
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 111 deletions.
2 changes: 1 addition & 1 deletion clang/docs/ClangPlugins.rst
Expand Up @@ -55,7 +55,7 @@ registering it using ``PragmaHandlerRegistry::Add<>``:
class ExamplePragmaHandler : public PragmaHandler {
public:
ExamplePragmaHandler() : PragmaHandler("example_pragma") { }
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &PragmaTok) {
// Handle the pragma
}
Expand Down
2 changes: 1 addition & 1 deletion clang/examples/AnnotateFunctions/AnnotateFunctions.cpp
Expand Up @@ -58,7 +58,7 @@ class PragmaAnnotateHandler : public PragmaHandler {
public:
PragmaAnnotateHandler() : PragmaHandler("enable_annotate") { }

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &PragmaTok) override {

Token Tok;
Expand Down
13 changes: 10 additions & 3 deletions clang/include/clang/Lex/Pragma.h
Expand Up @@ -14,6 +14,7 @@
#define LLVM_CLANG_LEX_PRAGMA_H

#include "clang/Basic/LLVM.h"
#include "clang/Basic/SourceLocation.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include <string>
Expand Down Expand Up @@ -46,6 +47,12 @@ class Token;
PIK___pragma
};

/// Describes how and where the pragma was introduced.
struct PragmaIntroducer {
PragmaIntroducerKind Kind;
SourceLocation Loc;
};

/// PragmaHandler - Instances of this interface defined to handle the various
/// pragmas that the language front-end uses. Each handler optionally has a
/// name (e.g. "pack") and the HandlePragma method is invoked when a pragma with
Expand All @@ -64,7 +71,7 @@ class PragmaHandler {
virtual ~PragmaHandler();

StringRef getName() const { return Name; }
virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
virtual void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &FirstToken) = 0;

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

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &FirstToken) override;
};

Expand Down Expand Up @@ -111,7 +118,7 @@ class PragmaNamespace : public PragmaHandler {

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

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &Tok) override;

PragmaNamespace *getIfNamespace() override { return this; }
Expand Down
3 changes: 1 addition & 2 deletions clang/include/clang/Lex/Preprocessor.h
Expand Up @@ -2291,8 +2291,7 @@ class Preprocessor {
void HandleElifDirective(Token &ElifToken, const Token &HashToken);

// Pragmas.
void HandlePragmaDirective(SourceLocation IntroducerLoc,
PragmaIntroducerKind Introducer);
void HandlePragmaDirective(PragmaIntroducer Introducer);

public:
void HandlePragmaOnce(Token &OnceTok);
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Frontend/PrintPreprocessedOutput.cpp
Expand Up @@ -664,7 +664,7 @@ struct UnknownPragmaHandler : public PragmaHandler {
bool RequireTokenExpansion)
: Prefix(prefix), Callbacks(callbacks),
ShouldExpandTokens(RequireTokenExpansion) {}
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &PragmaTok) override {
// Figure out what line we went to and insert the appropriate number of
// newline characters.
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Lex/PPDirectives.cpp
Expand Up @@ -982,7 +982,7 @@ void Preprocessor::HandleDirective(Token &Result) {

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

// GNU Extensions.
case tok::pp_import:
Expand Down
58 changes: 28 additions & 30 deletions clang/lib/Lex/Pragma.cpp
Expand Up @@ -63,7 +63,7 @@ PragmaHandler::~PragmaHandler() = default;
EmptyPragmaHandler::EmptyPragmaHandler(StringRef Name) : PragmaHandler(Name) {}

void EmptyPragmaHandler::HandlePragma(Preprocessor &PP,
PragmaIntroducerKind Introducer,
PragmaIntroducer Introducer,
Token &FirstToken) {}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -98,8 +98,7 @@ void PragmaNamespace::RemovePragmaHandler(PragmaHandler *Handler) {
}

void PragmaNamespace::HandlePragma(Preprocessor &PP,
PragmaIntroducerKind Introducer,
Token &Tok) {
PragmaIntroducer Introducer, Token &Tok) {
// 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);
Expand All @@ -124,10 +123,9 @@ void PragmaNamespace::HandlePragma(Preprocessor &PP,

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

if (!PragmasEnabled)
return;
Expand Down Expand Up @@ -321,7 +319,7 @@ void Preprocessor::Handle_Pragma(Token &Tok) {
EnterSourceFileWithLexer(TL, nullptr);

// With everything set up, lex this as a #pragma directive.
HandlePragmaDirective(PragmaLoc, PIK__Pragma);
HandlePragmaDirective({PIK__Pragma, PragmaLoc});

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

// With everything set up, lex this as a #pragma directive.
HandlePragmaDirective(PragmaLoc, PIK___pragma);
HandlePragmaDirective({PIK___pragma, PragmaLoc});

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

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &OnceTok) override {
PP.CheckEndOfDirective("pragma once");
PP.HandlePragmaOnce(OnceTok);
Expand All @@ -971,7 +969,7 @@ struct PragmaOnceHandler : public PragmaHandler {
struct PragmaMarkHandler : public PragmaHandler {
PragmaMarkHandler() : PragmaHandler("mark") {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &MarkTok) override {
PP.HandlePragmaMark();
}
Expand All @@ -981,7 +979,7 @@ struct PragmaMarkHandler : public PragmaHandler {
struct PragmaPoisonHandler : public PragmaHandler {
PragmaPoisonHandler() : PragmaHandler("poison") {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &PoisonTok) override {
PP.HandlePragmaPoison();
}
Expand All @@ -992,7 +990,7 @@ struct PragmaPoisonHandler : public PragmaHandler {
struct PragmaSystemHeaderHandler : public PragmaHandler {
PragmaSystemHeaderHandler() : PragmaHandler("system_header") {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &SHToken) override {
PP.HandlePragmaSystemHeader(SHToken);
PP.CheckEndOfDirective("pragma");
Expand All @@ -1002,7 +1000,7 @@ struct PragmaSystemHeaderHandler : public PragmaHandler {
struct PragmaDependencyHandler : public PragmaHandler {
PragmaDependencyHandler() : PragmaHandler("dependency") {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &DepToken) override {
PP.HandlePragmaDependency(DepToken);
}
Expand All @@ -1011,7 +1009,7 @@ struct PragmaDependencyHandler : public PragmaHandler {
struct PragmaDebugHandler : public PragmaHandler {
PragmaDebugHandler() : PragmaHandler("__debug") {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &DebugToken) override {
Token Tok;
PP.LexUnexpandedToken(Tok);
Expand Down Expand Up @@ -1151,7 +1149,7 @@ struct PragmaDiagnosticHandler : public PragmaHandler {
explicit PragmaDiagnosticHandler(const char *NS)
: PragmaHandler("diagnostic"), Namespace(NS) {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &DiagToken) override {
SourceLocation DiagLoc = DiagToken.getLocation();
Token Tok;
Expand Down Expand Up @@ -1230,7 +1228,7 @@ struct PragmaDiagnosticHandler : public PragmaHandler {
/// "\#pragma hdrstop [<header-name-string>]"
struct PragmaHdrstopHandler : public PragmaHandler {
PragmaHdrstopHandler() : PragmaHandler("hdrstop") {}
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &DepToken) override {
PP.HandlePragmaHdrstop(DepToken);
}
Expand All @@ -1242,7 +1240,7 @@ struct PragmaHdrstopHandler : public PragmaHandler {
struct PragmaWarningHandler : public PragmaHandler {
PragmaWarningHandler() : PragmaHandler("warning") {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &Tok) override {
// Parse things like:
// warning(push, 1)
Expand Down Expand Up @@ -1365,7 +1363,7 @@ struct PragmaWarningHandler : public PragmaHandler {
struct PragmaExecCharsetHandler : public PragmaHandler {
PragmaExecCharsetHandler() : PragmaHandler("execution_character_set") {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &Tok) override {
// Parse things like:
// execution_character_set(push, "UTF-8")
Expand Down Expand Up @@ -1427,7 +1425,7 @@ struct PragmaExecCharsetHandler : public PragmaHandler {
struct PragmaIncludeAliasHandler : public PragmaHandler {
PragmaIncludeAliasHandler() : PragmaHandler("include_alias") {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &IncludeAliasTok) override {
PP.HandlePragmaIncludeAlias(IncludeAliasTok);
}
Expand Down Expand Up @@ -1470,7 +1468,7 @@ struct PragmaMessageHandler : public PragmaHandler {
: PragmaHandler(PragmaKind(Kind, true)), Kind(Kind),
Namespace(Namespace) {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &Tok) override {
SourceLocation MessageLoc = Tok.getLocation();
PP.Lex(Tok);
Expand Down Expand Up @@ -1526,7 +1524,7 @@ struct PragmaMessageHandler : public PragmaHandler {
struct PragmaModuleImportHandler : public PragmaHandler {
PragmaModuleImportHandler() : PragmaHandler("import") {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &Tok) override {
SourceLocation ImportLoc = Tok.getLocation();

Expand Down Expand Up @@ -1563,7 +1561,7 @@ struct PragmaModuleImportHandler : public PragmaHandler {
struct PragmaModuleBeginHandler : public PragmaHandler {
PragmaModuleBeginHandler() : PragmaHandler("begin") {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &Tok) override {
SourceLocation BeginLoc = Tok.getLocation();

Expand Down Expand Up @@ -1623,7 +1621,7 @@ struct PragmaModuleBeginHandler : public PragmaHandler {
struct PragmaModuleEndHandler : public PragmaHandler {
PragmaModuleEndHandler() : PragmaHandler("end") {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &Tok) override {
SourceLocation Loc = Tok.getLocation();

Expand All @@ -1643,7 +1641,7 @@ struct PragmaModuleEndHandler : public PragmaHandler {
struct PragmaModuleBuildHandler : public PragmaHandler {
PragmaModuleBuildHandler() : PragmaHandler("build") {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &Tok) override {
PP.HandlePragmaModuleBuild(Tok);
}
Expand All @@ -1653,7 +1651,7 @@ struct PragmaModuleBuildHandler : public PragmaHandler {
struct PragmaModuleLoadHandler : public PragmaHandler {
PragmaModuleLoadHandler() : PragmaHandler("load") {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &Tok) override {
SourceLocation Loc = Tok.getLocation();

Expand All @@ -1677,7 +1675,7 @@ struct PragmaModuleLoadHandler : public PragmaHandler {
struct PragmaPushMacroHandler : public PragmaHandler {
PragmaPushMacroHandler() : PragmaHandler("push_macro") {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &PushMacroTok) override {
PP.HandlePragmaPushMacro(PushMacroTok);
}
Expand All @@ -1688,7 +1686,7 @@ struct PragmaPushMacroHandler : public PragmaHandler {
struct PragmaPopMacroHandler : public PragmaHandler {
PragmaPopMacroHandler() : PragmaHandler("pop_macro") {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &PopMacroTok) override {
PP.HandlePragmaPopMacro(PopMacroTok);
}
Expand All @@ -1699,7 +1697,7 @@ struct PragmaPopMacroHandler : public PragmaHandler {
struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
PragmaARCCFCodeAuditedHandler() : PragmaHandler("arc_cf_code_audited") {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &NameTok) override {
SourceLocation Loc = NameTok.getLocation();
bool IsBegin;
Expand Down Expand Up @@ -1754,7 +1752,7 @@ struct PragmaARCCFCodeAuditedHandler : public PragmaHandler {
struct PragmaAssumeNonNullHandler : public PragmaHandler {
PragmaAssumeNonNullHandler() : PragmaHandler("assume_nonnull") {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &NameTok) override {
SourceLocation Loc = NameTok.getLocation();
bool IsBegin;
Expand Down Expand Up @@ -1823,7 +1821,7 @@ struct PragmaAssumeNonNullHandler : public PragmaHandler {
struct PragmaRegionHandler : public PragmaHandler {
PragmaRegionHandler(const char *pragma) : PragmaHandler(pragma) {}

void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Token &NameTok) override {
// #pragma region: endregion matches can be verified
// __pragma(region): no sense, but ignored by msvc
Expand Down

0 comments on commit ddde0ec

Please sign in to comment.