ParsedAttr.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //======- ParsedAttr.cpp --------------------------------------------------===//
  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 ParsedAttr class implementation
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Sema/ParsedAttr.h"
  13. #include "clang/AST/ASTContext.h"
  14. #include "clang/Basic/AttrSubjectMatchRules.h"
  15. #include "clang/Basic/IdentifierTable.h"
  16. #include "clang/Basic/TargetInfo.h"
  17. #include "clang/Sema/SemaInternal.h"
  18. #include "llvm/ADT/SmallString.h"
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include <cassert>
  22. #include <cstddef>
  23. #include <utility>
  24. using namespace clang;
  25. IdentifierLoc *IdentifierLoc::create(ASTContext &Ctx, SourceLocation Loc,
  26. IdentifierInfo *Ident) {
  27. IdentifierLoc *Result = new (Ctx) IdentifierLoc;
  28. Result->Loc = Loc;
  29. Result->Ident = Ident;
  30. return Result;
  31. }
  32. size_t ParsedAttr::allocated_size() const {
  33. if (IsAvailability) return AttributeFactory::AvailabilityAllocSize;
  34. else if (IsTypeTagForDatatype)
  35. return AttributeFactory::TypeTagForDatatypeAllocSize;
  36. else if (IsProperty)
  37. return AttributeFactory::PropertyAllocSize;
  38. else if (HasParsedType)
  39. return totalSizeToAlloc<ArgsUnion, detail::AvailabilityData,
  40. detail::TypeTagForDatatypeData, ParsedType,
  41. detail::PropertyData>(0, 0, 0, 1, 0);
  42. return totalSizeToAlloc<ArgsUnion, detail::AvailabilityData,
  43. detail::TypeTagForDatatypeData, ParsedType,
  44. detail::PropertyData>(NumArgs, 0, 0, 0, 0);
  45. }
  46. AttributeFactory::AttributeFactory() {
  47. // Go ahead and configure all the inline capacity. This is just a memset.
  48. FreeLists.resize(InlineFreeListsCapacity);
  49. }
  50. AttributeFactory::~AttributeFactory() = default;
  51. static size_t getFreeListIndexForSize(size_t size) {
  52. assert(size >= sizeof(ParsedAttr));
  53. assert((size % sizeof(void*)) == 0);
  54. return ((size - sizeof(ParsedAttr)) / sizeof(void *));
  55. }
  56. void *AttributeFactory::allocate(size_t size) {
  57. // Check for a previously reclaimed attribute.
  58. size_t index = getFreeListIndexForSize(size);
  59. if (index < FreeLists.size() && !FreeLists[index].empty()) {
  60. ParsedAttr *attr = FreeLists[index].back();
  61. FreeLists[index].pop_back();
  62. return attr;
  63. }
  64. // Otherwise, allocate something new.
  65. return Alloc.Allocate(size, alignof(AttributeFactory));
  66. }
  67. void AttributeFactory::deallocate(ParsedAttr *Attr) {
  68. size_t size = Attr->allocated_size();
  69. size_t freeListIndex = getFreeListIndexForSize(size);
  70. // Expand FreeLists to the appropriate size, if required.
  71. if (freeListIndex >= FreeLists.size())
  72. FreeLists.resize(freeListIndex + 1);
  73. #ifndef NDEBUG
  74. // In debug mode, zero out the attribute to help find memory overwriting.
  75. memset(Attr, 0, size);
  76. #endif
  77. // Add 'Attr' to the appropriate free-list.
  78. FreeLists[freeListIndex].push_back(Attr);
  79. }
  80. void AttributeFactory::reclaimPool(AttributePool &cur) {
  81. for (ParsedAttr *AL : cur.Attrs)
  82. deallocate(AL);
  83. }
  84. void AttributePool::takePool(AttributePool &pool) {
  85. Attrs.insert(Attrs.end(), pool.Attrs.begin(), pool.Attrs.end());
  86. pool.Attrs.clear();
  87. }
  88. #include "clang/Sema/AttrParsedAttrKinds.inc"
  89. static StringRef normalizeAttrScopeName(StringRef ScopeName,
  90. ParsedAttr::Syntax SyntaxUsed) {
  91. // Normalize the "__gnu__" scope name to be "gnu" and the "_Clang" scope name
  92. // to be "clang".
  93. if (SyntaxUsed == ParsedAttr::AS_CXX11 ||
  94. SyntaxUsed == ParsedAttr::AS_C2x) {
  95. if (ScopeName == "__gnu__")
  96. ScopeName = "gnu";
  97. else if (ScopeName == "_Clang")
  98. ScopeName = "clang";
  99. }
  100. return ScopeName;
  101. }
  102. static StringRef normalizeAttrName(StringRef AttrName,
  103. StringRef NormalizedScopeName,
  104. ParsedAttr::Syntax SyntaxUsed) {
  105. // Normalize the attribute name, __foo__ becomes foo. This is only allowable
  106. // for GNU attributes, and attributes using the double square bracket syntax.
  107. bool ShouldNormalize =
  108. SyntaxUsed == ParsedAttr::AS_GNU ||
  109. ((SyntaxUsed == ParsedAttr::AS_CXX11 ||
  110. SyntaxUsed == ParsedAttr::AS_C2x) &&
  111. (NormalizedScopeName == "gnu" || NormalizedScopeName == "clang"));
  112. if (ShouldNormalize && AttrName.size() >= 4 && AttrName.startswith("__") &&
  113. AttrName.endswith("__"))
  114. AttrName = AttrName.slice(2, AttrName.size() - 2);
  115. return AttrName;
  116. }
  117. ParsedAttr::Kind ParsedAttr::getKind(const IdentifierInfo *Name,
  118. const IdentifierInfo *ScopeName,
  119. Syntax SyntaxUsed) {
  120. StringRef AttrName = Name->getName();
  121. SmallString<64> FullName;
  122. if (ScopeName)
  123. FullName += normalizeAttrScopeName(ScopeName->getName(), SyntaxUsed);
  124. AttrName = normalizeAttrName(AttrName, FullName, SyntaxUsed);
  125. // Ensure that in the case of C++11 attributes, we look for '::foo' if it is
  126. // unscoped.
  127. if (ScopeName || SyntaxUsed == AS_CXX11 || SyntaxUsed == AS_C2x)
  128. FullName += "::";
  129. FullName += AttrName;
  130. return ::getAttrKind(FullName, SyntaxUsed);
  131. }
  132. unsigned ParsedAttr::getAttributeSpellingListIndex() const {
  133. // Both variables will be used in tablegen generated
  134. // attribute spell list index matching code.
  135. auto Syntax = static_cast<ParsedAttr::Syntax>(SyntaxUsed);
  136. StringRef Scope =
  137. ScopeName ? normalizeAttrScopeName(ScopeName->getName(), Syntax) : "";
  138. StringRef Name = normalizeAttrName(AttrName->getName(), Scope, Syntax);
  139. #include "clang/Sema/AttrSpellingListIndex.inc"
  140. }
  141. struct ParsedAttrInfo {
  142. unsigned NumArgs : 4;
  143. unsigned OptArgs : 4;
  144. unsigned HasCustomParsing : 1;
  145. unsigned IsTargetSpecific : 1;
  146. unsigned IsType : 1;
  147. unsigned IsStmt : 1;
  148. unsigned IsKnownToGCC : 1;
  149. unsigned IsSupportedByPragmaAttribute : 1;
  150. bool (*DiagAppertainsToDecl)(Sema &S, const ParsedAttr &Attr, const Decl *);
  151. bool (*DiagLangOpts)(Sema &S, const ParsedAttr &Attr);
  152. bool (*ExistsInTarget)(const TargetInfo &Target);
  153. unsigned (*SpellingIndexToSemanticSpelling)(const ParsedAttr &Attr);
  154. void (*GetPragmaAttributeMatchRules)(
  155. llvm::SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &Rules,
  156. const LangOptions &LangOpts);
  157. };
  158. namespace {
  159. #include "clang/Sema/AttrParsedAttrImpl.inc"
  160. } // namespace
  161. static const ParsedAttrInfo &getInfo(const ParsedAttr &A) {
  162. return AttrInfoMap[A.getKind()];
  163. }
  164. unsigned ParsedAttr::getMinArgs() const { return getInfo(*this).NumArgs; }
  165. unsigned ParsedAttr::getMaxArgs() const {
  166. return getMinArgs() + getInfo(*this).OptArgs;
  167. }
  168. bool ParsedAttr::hasCustomParsing() const {
  169. return getInfo(*this).HasCustomParsing;
  170. }
  171. bool ParsedAttr::diagnoseAppertainsTo(Sema &S, const Decl *D) const {
  172. return getInfo(*this).DiagAppertainsToDecl(S, *this, D);
  173. }
  174. bool ParsedAttr::appliesToDecl(const Decl *D,
  175. attr::SubjectMatchRule MatchRule) const {
  176. return checkAttributeMatchRuleAppliesTo(D, MatchRule);
  177. }
  178. void ParsedAttr::getMatchRules(
  179. const LangOptions &LangOpts,
  180. SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &MatchRules)
  181. const {
  182. return getInfo(*this).GetPragmaAttributeMatchRules(MatchRules, LangOpts);
  183. }
  184. bool ParsedAttr::diagnoseLangOpts(Sema &S) const {
  185. return getInfo(*this).DiagLangOpts(S, *this);
  186. }
  187. bool ParsedAttr::isTargetSpecificAttr() const {
  188. return getInfo(*this).IsTargetSpecific;
  189. }
  190. bool ParsedAttr::isTypeAttr() const { return getInfo(*this).IsType; }
  191. bool ParsedAttr::isStmtAttr() const { return getInfo(*this).IsStmt; }
  192. bool ParsedAttr::existsInTarget(const TargetInfo &Target) const {
  193. return getInfo(*this).ExistsInTarget(Target);
  194. }
  195. bool ParsedAttr::isKnownToGCC() const { return getInfo(*this).IsKnownToGCC; }
  196. bool ParsedAttr::isSupportedByPragmaAttribute() const {
  197. return getInfo(*this).IsSupportedByPragmaAttribute;
  198. }
  199. unsigned ParsedAttr::getSemanticSpelling() const {
  200. return getInfo(*this).SpellingIndexToSemanticSpelling(*this);
  201. }
  202. bool ParsedAttr::hasVariadicArg() const {
  203. // If the attribute has the maximum number of optional arguments, we will
  204. // claim that as being variadic. If we someday get an attribute that
  205. // legitimately bumps up against that maximum, we can use another bit to track
  206. // whether it's truly variadic or not.
  207. return getInfo(*this).OptArgs == 15;
  208. }