1 //===--- Pragma.h - Pragma registration and handling ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PragmaHandler and PragmaTable interfaces.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_PRAGMA_H
15 #define LLVM_CLANG_PRAGMA_H
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
25 class PragmaNamespace;
28 * \brief Describes how the pragma was introduced, e.g., with #pragma,
29 * _Pragma, or __pragma.
31 enum PragmaIntroducerKind {
33 * \brief The pragma was introduced via #pragma.
38 * \brief The pragma was introduced via the C99 _Pragma(string-literal).
43 * \brief The pragma was introduced via the Microsoft
44 * __pragma(token-string).
49 /// PragmaHandler - Instances of this interface defined to handle the various
50 /// pragmas that the language front-end uses. Each handler optionally has a
51 /// name (e.g. "pack") and the HandlePragma method is invoked when a pragma with
52 /// that identifier is found. If a handler does not match any of the declared
53 /// pragmas the handler with a null identifier is invoked, if it exists.
55 /// Note that the PragmaNamespace class can be used to subdivide pragmas, e.g.
56 /// we treat "#pragma STDC" and "#pragma GCC" as namespaces that contain other
61 explicit PragmaHandler(llvm::StringRef name) : Name(name) {}
63 virtual ~PragmaHandler();
65 llvm::StringRef getName() const { return Name; }
66 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
67 Token &FirstToken) = 0;
69 /// getIfNamespace - If this is a namespace, return it. This is equivalent to
70 /// using a dynamic_cast, but doesn't require RTTI.
71 virtual PragmaNamespace *getIfNamespace() { return 0; }
74 /// EmptyPragmaHandler - A pragma handler which takes no action, which can be
75 /// used to ignore particular pragmas.
76 class EmptyPragmaHandler : public PragmaHandler {
80 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
84 /// PragmaNamespace - This PragmaHandler subdivides the namespace of pragmas,
85 /// allowing hierarchical pragmas to be defined. Common examples of namespaces
86 /// are "#pragma GCC", "#pragma STDC", and "#pragma omp", but any namespaces may
87 /// be (potentially recursively) defined.
88 class PragmaNamespace : public PragmaHandler {
89 /// Handlers - This is a map of the handlers in this namespace with their name
92 llvm::StringMap<PragmaHandler*> Handlers;
94 explicit PragmaNamespace(llvm::StringRef Name) : PragmaHandler(Name) {}
95 virtual ~PragmaNamespace();
97 /// FindHandler - Check to see if there is already a handler for the
98 /// specified name. If not, return the handler for the null name if it
99 /// exists, otherwise return null. If IgnoreNull is true (the default) then
100 /// the null handler isn't returned on failure to match.
101 PragmaHandler *FindHandler(llvm::StringRef Name,
102 bool IgnoreNull = true) const;
104 /// AddPragma - Add a pragma to this namespace.
106 void AddPragma(PragmaHandler *Handler);
108 /// RemovePragmaHandler - Remove the given handler from the
110 void RemovePragmaHandler(PragmaHandler *Handler);
113 return Handlers.empty();
116 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
119 virtual PragmaNamespace *getIfNamespace() { return this; }
123 } // end namespace clang