ParsedAttr.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. struct ParsedAttrInfo {
  89. unsigned NumArgs : 4;
  90. unsigned OptArgs : 4;
  91. unsigned HasCustomParsing : 1;
  92. unsigned IsTargetSpecific : 1;
  93. unsigned IsType : 1;
  94. unsigned IsStmt : 1;
  95. unsigned IsKnownToGCC : 1;
  96. unsigned IsSupportedByPragmaAttribute : 1;
  97. bool (*DiagAppertainsToDecl)(Sema &S, const ParsedAttr &Attr, const Decl *);
  98. bool (*DiagLangOpts)(Sema &S, const ParsedAttr &Attr);
  99. bool (*ExistsInTarget)(const TargetInfo &Target);
  100. unsigned (*SpellingIndexToSemanticSpelling)(const ParsedAttr &Attr);
  101. void (*GetPragmaAttributeMatchRules)(
  102. llvm::SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &Rules,
  103. const LangOptions &LangOpts);
  104. };
  105. namespace {
  106. #include "clang/Sema/AttrParsedAttrImpl.inc"
  107. } // namespace
  108. static const ParsedAttrInfo &getInfo(const ParsedAttr &A) {
  109. return AttrInfoMap[A.getKind()];
  110. }
  111. unsigned ParsedAttr::getMinArgs() const { return getInfo(*this).NumArgs; }
  112. unsigned ParsedAttr::getMaxArgs() const {
  113. return getMinArgs() + getInfo(*this).OptArgs;
  114. }
  115. bool ParsedAttr::hasCustomParsing() const {
  116. return getInfo(*this).HasCustomParsing;
  117. }
  118. bool ParsedAttr::diagnoseAppertainsTo(Sema &S, const Decl *D) const {
  119. return getInfo(*this).DiagAppertainsToDecl(S, *this, D);
  120. }
  121. bool ParsedAttr::appliesToDecl(const Decl *D,
  122. attr::SubjectMatchRule MatchRule) const {
  123. return checkAttributeMatchRuleAppliesTo(D, MatchRule);
  124. }
  125. void ParsedAttr::getMatchRules(
  126. const LangOptions &LangOpts,
  127. SmallVectorImpl<std::pair<attr::SubjectMatchRule, bool>> &MatchRules)
  128. const {
  129. return getInfo(*this).GetPragmaAttributeMatchRules(MatchRules, LangOpts);
  130. }
  131. bool ParsedAttr::diagnoseLangOpts(Sema &S) const {
  132. return getInfo(*this).DiagLangOpts(S, *this);
  133. }
  134. bool ParsedAttr::isTargetSpecificAttr() const {
  135. return getInfo(*this).IsTargetSpecific;
  136. }
  137. bool ParsedAttr::isTypeAttr() const { return getInfo(*this).IsType; }
  138. bool ParsedAttr::isStmtAttr() const { return getInfo(*this).IsStmt; }
  139. bool ParsedAttr::existsInTarget(const TargetInfo &Target) const {
  140. return getInfo(*this).ExistsInTarget(Target);
  141. }
  142. bool ParsedAttr::isKnownToGCC() const { return getInfo(*this).IsKnownToGCC; }
  143. bool ParsedAttr::isSupportedByPragmaAttribute() const {
  144. return getInfo(*this).IsSupportedByPragmaAttribute;
  145. }
  146. unsigned ParsedAttr::getSemanticSpelling() const {
  147. return getInfo(*this).SpellingIndexToSemanticSpelling(*this);
  148. }
  149. bool ParsedAttr::hasVariadicArg() const {
  150. // If the attribute has the maximum number of optional arguments, we will
  151. // claim that as being variadic. If we someday get an attribute that
  152. // legitimately bumps up against that maximum, we can use another bit to track
  153. // whether it's truly variadic or not.
  154. return getInfo(*this).OptArgs == 15;
  155. }