clang  14.0.0git
Pragma.h
Go to the documentation of this file.
1 //===- Pragma.h - Pragma registration and handling --------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the PragmaHandler and PragmaTable interfaces.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_LEX_PRAGMA_H
14 #define LLVM_CLANG_LEX_PRAGMA_H
15 
16 #include "clang/Basic/LLVM.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/StringRef.h"
20 #include <string>
21 
22 namespace clang {
23 
24 class PragmaNamespace;
25 class Preprocessor;
26 class Token;
27 
28  /**
29  * Describes how the pragma was introduced, e.g., with \#pragma,
30  * _Pragma, or __pragma.
31  */
33  /**
34  * The pragma was introduced via \#pragma.
35  */
37 
38  /**
39  * The pragma was introduced via the C99 _Pragma(string-literal).
40  */
42 
43  /**
44  * The pragma was introduced via the Microsoft
45  * __pragma(token-string).
46  */
48  };
49 
50  /// Describes how and where the pragma was introduced.
54  };
55 
56 /// PragmaHandler - Instances of this interface defined to handle the various
57 /// pragmas that the language front-end uses. Each handler optionally has a
58 /// name (e.g. "pack") and the HandlePragma method is invoked when a pragma with
59 /// that identifier is found. If a handler does not match any of the declared
60 /// pragmas the handler with a null identifier is invoked, if it exists.
61 ///
62 /// Note that the PragmaNamespace class can be used to subdivide pragmas, e.g.
63 /// we treat "\#pragma STDC" and "\#pragma GCC" as namespaces that contain other
64 /// pragmas.
66  std::string Name;
67 
68 public:
69  PragmaHandler() = default;
70  explicit PragmaHandler(StringRef name) : Name(name) {}
71  virtual ~PragmaHandler();
72 
73  StringRef getName() const { return Name; }
74  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
75  Token &FirstToken) = 0;
76 
77  /// getIfNamespace - If this is a namespace, return it. This is equivalent to
78  /// using a dynamic_cast, but doesn't require RTTI.
79  virtual PragmaNamespace *getIfNamespace() { return nullptr; }
80 };
81 
82 /// EmptyPragmaHandler - A pragma handler which takes no action, which can be
83 /// used to ignore particular pragmas.
85 public:
86  explicit EmptyPragmaHandler(StringRef Name = StringRef());
87 
88  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
89  Token &FirstToken) override;
90 };
91 
92 /// PragmaNamespace - This PragmaHandler subdivides the namespace of pragmas,
93 /// allowing hierarchical pragmas to be defined. Common examples of namespaces
94 /// are "\#pragma GCC", "\#pragma STDC", and "\#pragma omp", but any namespaces
95 /// may be (potentially recursively) defined.
97  /// Handlers - This is a map of the handlers in this namespace with their name
98  /// as key.
99  llvm::StringMap<std::unique_ptr<PragmaHandler>> Handlers;
100 
101 public:
102  explicit PragmaNamespace(StringRef Name) : PragmaHandler(Name) {}
103 
104  /// FindHandler - Check to see if there is already a handler for the
105  /// specified name. If not, return the handler for the null name if it
106  /// exists, otherwise return null. If IgnoreNull is true (the default) then
107  /// the null handler isn't returned on failure to match.
108  PragmaHandler *FindHandler(StringRef Name,
109  bool IgnoreNull = true) const;
110 
111  /// AddPragma - Add a pragma to this namespace.
112  void AddPragma(PragmaHandler *Handler);
113 
114  /// RemovePragmaHandler - Remove the given handler from the
115  /// namespace.
116  void RemovePragmaHandler(PragmaHandler *Handler);
117 
118  bool IsEmpty() const { return Handlers.empty(); }
119 
120  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
121  Token &Tok) override;
122 
123  PragmaNamespace *getIfNamespace() override { return this; }
124 };
125 
126 } // namespace clang
127 
128 #endif // LLVM_CLANG_LEX_PRAGMA_H
clang::PragmaIntroducerKind
PragmaIntroducerKind
Describes how the pragma was introduced, e.g., with #pragma, _Pragma, or __pragma.
Definition: Pragma.h:32
clang::PragmaHandler::~PragmaHandler
virtual ~PragmaHandler()
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::PIK_HashPragma
@ PIK_HashPragma
The pragma was introduced via #pragma.
Definition: Pragma.h:36
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::PragmaNamespace::getIfNamespace
PragmaNamespace * getIfNamespace() override
getIfNamespace - If this is a namespace, return it.
Definition: Pragma.h:123
clang::PragmaNamespace::IsEmpty
bool IsEmpty() const
Definition: Pragma.h:118
clang::Token
Token - This structure provides full information about a lexed token.
Definition: Token.h:34
clang::EmptyPragmaHandler::HandlePragma
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, Token &FirstToken) override
Definition: Pragma.cpp:69
clang::PIK___pragma
@ PIK___pragma
The pragma was introduced via the Microsoft __pragma(token-string).
Definition: Pragma.h:47
clang::PragmaHandler::PragmaHandler
PragmaHandler(StringRef name)
Definition: Pragma.h:70
clang::PragmaNamespace::RemovePragmaHandler
void RemovePragmaHandler(PragmaHandler *Handler)
RemovePragmaHandler - Remove the given handler from the namespace.
Definition: Pragma.cpp:100
clang::PragmaNamespace::HandlePragma
void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, Token &Tok) override
Definition: Pragma.cpp:109
clang::PragmaHandler::PragmaHandler
PragmaHandler()=default
clang::PragmaHandler::getIfNamespace
virtual PragmaNamespace * getIfNamespace()
getIfNamespace - If this is a namespace, return it.
Definition: Pragma.h:79
clang::PragmaIntroducer::Kind
PragmaIntroducerKind Kind
Definition: Pragma.h:52
clang::PragmaHandler::getName
StringRef getName() const
Definition: Pragma.h:73
clang::PragmaNamespace::PragmaNamespace
PragmaNamespace(StringRef Name)
Definition: Pragma.h:102
clang::PragmaIntroducer
Describes how and where the pragma was introduced.
Definition: Pragma.h:51
clang::PragmaHandler
PragmaHandler - Instances of this interface defined to handle the various pragmas that the language f...
Definition: Pragma.h:65
SourceLocation.h
clang::PragmaNamespace::AddPragma
void AddPragma(PragmaHandler *Handler)
AddPragma - Add a pragma to this namespace.
Definition: Pragma.cpp:94
clang::EmptyPragmaHandler
EmptyPragmaHandler - A pragma handler which takes no action, which can be used to ignore particular p...
Definition: Pragma.h:84
LLVM.h
clang::EmptyPragmaHandler::EmptyPragmaHandler
EmptyPragmaHandler(StringRef Name=StringRef())
Definition: Pragma.cpp:67
clang::PragmaHandler::HandlePragma
virtual void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer, Token &FirstToken)=0
clang
Definition: CalledOnceCheck.h:17
clang::PragmaIntroducer::Loc
SourceLocation Loc
Definition: Pragma.h:53
clang::PragmaNamespace
PragmaNamespace - This PragmaHandler subdivides the namespace of pragmas, allowing hierarchical pragm...
Definition: Pragma.h:96
clang::PIK__Pragma
@ PIK__Pragma
The pragma was introduced via the C99 _Pragma(string-literal).
Definition: Pragma.h:41
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:130
clang::PragmaNamespace::FindHandler
PragmaHandler * FindHandler(StringRef Name, bool IgnoreNull=true) const
FindHandler - Check to see if there is already a handler for the specified name.
Definition: Pragma.cpp:81
clang::transformer::name
RangeSelector name(std::string ID)
Given a node with a "name", (like NamedDecl, DeclRefExpr, CxxCtorInitializer, and TypeLoc) selects th...
Definition: RangeSelector.cpp:200