clang  6.0.0svn
Pragma.h
Go to the documentation of this file.
1 //===- Pragma.h - Pragma registration and handling --------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the PragmaHandler and PragmaTable interfaces.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LEX_PRAGMA_H
15 #define LLVM_CLANG_LEX_PRAGMA_H
16 
17 #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  * \brief Describes how the pragma was introduced, e.g., with \#pragma,
30  * _Pragma, or __pragma.
31  */
33  /**
34  * \brief The pragma was introduced via \#pragma.
35  */
37 
38  /**
39  * \brief The pragma was introduced via the C99 _Pragma(string-literal).
40  */
42 
43  /**
44  * \brief The pragma was introduced via the Microsoft
45  * __pragma(token-string).
46  */
48  };
49 
50 /// PragmaHandler - Instances of this interface defined to handle the various
51 /// pragmas that the language front-end uses. Each handler optionally has a
52 /// name (e.g. "pack") and the HandlePragma method is invoked when a pragma with
53 /// that identifier is found. If a handler does not match any of the declared
54 /// pragmas the handler with a null identifier is invoked, if it exists.
55 ///
56 /// Note that the PragmaNamespace class can be used to subdivide pragmas, e.g.
57 /// we treat "\#pragma STDC" and "\#pragma GCC" as namespaces that contain other
58 /// pragmas.
60  std::string Name;
61 
62 public:
63  PragmaHandler() = default;
64  explicit PragmaHandler(StringRef name) : Name(name) {}
65  virtual ~PragmaHandler();
66 
67  StringRef getName() const { return Name; }
68  virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
69  Token &FirstToken) = 0;
70 
71  /// getIfNamespace - If this is a namespace, return it. This is equivalent to
72  /// using a dynamic_cast, but doesn't require RTTI.
73  virtual PragmaNamespace *getIfNamespace() { return nullptr; }
74 };
75 
76 /// EmptyPragmaHandler - A pragma handler which takes no action, which can be
77 /// used to ignore particular pragmas.
79 public:
80  explicit EmptyPragmaHandler(StringRef Name = StringRef());
81 
82  void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
83  Token &FirstToken) override;
84 };
85 
86 /// PragmaNamespace - This PragmaHandler subdivides the namespace of pragmas,
87 /// allowing hierarchical pragmas to be defined. Common examples of namespaces
88 /// are "\#pragma GCC", "\#pragma STDC", and "\#pragma omp", but any namespaces
89 /// may be (potentially recursively) defined.
91  /// Handlers - This is a map of the handlers in this namespace with their name
92  /// as key.
93  llvm::StringMap<PragmaHandler *> Handlers;
94 
95 public:
96  explicit PragmaNamespace(StringRef Name) : PragmaHandler(Name) {}
97  ~PragmaNamespace() override;
98 
99  /// FindHandler - Check to see if there is already a handler for the
100  /// specified name. If not, return the handler for the null name if it
101  /// exists, otherwise return null. If IgnoreNull is true (the default) then
102  /// the null handler isn't returned on failure to match.
103  PragmaHandler *FindHandler(StringRef Name,
104  bool IgnoreNull = true) const;
105 
106  /// AddPragma - Add a pragma to this namespace.
107  void AddPragma(PragmaHandler *Handler);
108 
109  /// RemovePragmaHandler - Remove the given handler from the
110  /// namespace.
111  void RemovePragmaHandler(PragmaHandler *Handler);
112 
113  bool IsEmpty() const { return Handlers.empty(); }
114 
115  void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
116  Token &FirstToken) override;
117 
118  PragmaNamespace *getIfNamespace() override { return this; }
119 };
120 
121 } // namespace clang
122 
123 #endif // LLVM_CLANG_LEX_PRAGMA_H
virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, Token &FirstToken)=0
The pragma was introduced via the Microsoft __pragma(token-string).
Definition: Pragma.h:47
The pragma was introduced via the C99 _Pragma(string-literal).
Definition: Pragma.h:41
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
PragmaIntroducerKind
Describes how the pragma was introduced, e.g., with #pragma, _Pragma, or __pragma.
Definition: Pragma.h:32
EmptyPragmaHandler - A pragma handler which takes no action, which can be used to ignore particular p...
Definition: Pragma.h:78
PragmaNamespace * getIfNamespace() override
getIfNamespace - If this is a namespace, return it.
Definition: Pragma.h:118
virtual ~PragmaHandler()
Dataflow Directional Tag Classes.
The pragma was introduced via #pragma.
Definition: Pragma.h:36
virtual PragmaNamespace * getIfNamespace()
getIfNamespace - If this is a namespace, return it.
Definition: Pragma.h:73
PragmaHandler - Instances of this interface defined to handle the various pragmas that the language f...
Definition: Pragma.h:59
PragmaNamespace - This PragmaHandler subdivides the namespace of pragmas, allowing hierarchical pragm...
Definition: Pragma.h:90
bool IsEmpty() const
Definition: Pragma.h:113
PragmaNamespace(StringRef Name)
Definition: Pragma.h:96
PragmaHandler(StringRef name)
Definition: Pragma.h:64
StringRef getName() const
Definition: Pragma.h:67
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:127