Pragma.h revision c72cc5072cdc1a1a6e05f9d0f962f293a69248c4
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_PRAGMA_H 15#define LLVM_CLANG_PRAGMA_H 16 17#include <cassert> 18#include <vector> 19 20namespace clang { 21 class Preprocessor; 22 class Token; 23 class IdentifierInfo; 24 class PragmaNamespace; 25 26/// PragmaHandler - Instances of this interface defined to handle the various 27/// pragmas that the language front-end uses. Each handler optionally has a 28/// name (e.g. "pack") and the HandlePragma method is invoked when a pragma with 29/// that identifier is found. If a handler does not match any of the declared 30/// pragmas the handler with a null identifier is invoked, if it exists. 31/// 32/// Note that the PragmaNamespace class can be used to subdivide pragmas, e.g. 33/// we treat "#pragma STDC" and "#pragma GCC" as namespaces that contain other 34/// pragmas. 35class PragmaHandler { 36 const IdentifierInfo *Name; 37public: 38 PragmaHandler(const IdentifierInfo *name) : Name(name) {} 39 virtual ~PragmaHandler(); 40 41 const IdentifierInfo *getName() const { return Name; } 42 virtual void HandlePragma(Preprocessor &PP, Token &FirstToken) = 0; 43 44 /// getIfNamespace - If this is a namespace, return it. This is equivalent to 45 /// using a dynamic_cast, but doesn't require RTTI. 46 virtual PragmaNamespace *getIfNamespace() { return 0; } 47}; 48 49/// EmptyPragmaHandler - A pragma handler which takes no action, which can be 50/// used to ignore particular pragmas. 51class EmptyPragmaHandler : public PragmaHandler { 52public: 53 EmptyPragmaHandler(); 54 55 virtual void HandlePragma(Preprocessor &PP, Token &FirstToken); 56}; 57 58/// PragmaNamespace - This PragmaHandler subdivides the namespace of pragmas, 59/// allowing hierarchical pragmas to be defined. Common examples of namespaces 60/// are "#pragma GCC", "#pragma STDC", and "#pragma omp", but any namespaces may 61/// be (potentially recursively) defined. 62class PragmaNamespace : public PragmaHandler { 63 /// Handlers - This is the list of handlers in this namespace. 64 /// 65 std::vector<PragmaHandler*> Handlers; 66public: 67 PragmaNamespace(const IdentifierInfo *Name) : PragmaHandler(Name) {} 68 virtual ~PragmaNamespace(); 69 70 /// FindHandler - Check to see if there is already a handler for the 71 /// specified name. If not, return the handler for the null identifier if it 72 /// exists, otherwise return null. If IgnoreNull is true (the default) then 73 /// the null handler isn't returned on failure to match. 74 PragmaHandler *FindHandler(const IdentifierInfo *Name, 75 bool IgnoreNull = true) const; 76 77 /// AddPragma - Add a pragma to this namespace. 78 /// 79 void AddPragma(PragmaHandler *Handler) { 80 Handlers.push_back(Handler); 81 } 82 83 /// RemovePragmaHandler - Remove the given handler from the 84 /// namespace. 85 void RemovePragmaHandler(PragmaHandler *Handler); 86 87 bool IsEmpty() { 88 return Handlers.empty(); 89 } 90 91 virtual void HandlePragma(Preprocessor &PP, Token &FirstToken); 92 93 virtual PragmaNamespace *getIfNamespace() { return this; } 94}; 95 96 97} // end namespace clang 98 99#endif 100