Index: include/clang/Analysis/Analyses/Consumed.h =================================================================== --- include/clang/Analysis/Analyses/Consumed.h +++ include/clang/Analysis/Analyses/Consumed.h @@ -167,14 +167,9 @@ /// A class that handles the analysis of uniqueness violations. class ConsumedAnalyzer { - typedef llvm::DenseMap CacheMapType; - typedef std::pair CachePairType; - ConsumedBlockInfo BlockInfo; ConsumedStateMap *CurrStates; - CacheMapType ConsumableTypeCache; - bool hasConsumableAttributes(const CXXRecordDecl *RD); bool splitState(const CFGBlock *CurrBlock, const ConsumedStmtVisitor &Visitor); @@ -186,9 +181,6 @@ ConsumedAnalyzer(ConsumedWarningsHandlerBase &WarningsHandler) : WarningsHandler(WarningsHandler) {} - /// \brief Check to see if the type is a consumable type. - bool isConsumableType(QualType Type); - /// \brief Check a function's CFG for consumed violations. /// /// We traverse the blocks in the CFG, keeping track of the state of each Index: include/clang/Basic/Attr.td =================================================================== --- include/clang/Basic/Attr.td +++ include/clang/Basic/Attr.td @@ -928,6 +928,11 @@ // C/C++ consumed attributes. +def Consumable : InheritableAttr { + let Spellings = [GNU<"consumable">]; + let Subjects = [CXXRecord]; +} + def CallableWhenUnconsumed : InheritableAttr { let Spellings = [GNU<"callable_when_unconsumed">]; let Subjects = [CXXMethod]; @@ -938,16 +943,16 @@ let Subjects = [CXXMethod]; } -def Consumes : InheritableAttr { - let Spellings = [GNU<"consumes">]; - let Subjects = [CXXMethod, CXXConstructor]; -} - def TestsConsumed : InheritableAttr { let Spellings = [GNU<"tests_consumed">]; let Subjects = [CXXMethod]; } +def Consumes : InheritableAttr { + let Spellings = [GNU<"consumes">]; + let Subjects = [CXXMethod]; +} + // Type safety attributes for `void *' pointers and type tags. def ArgumentWithTypeTag : InheritableAttr { Index: include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- include/clang/Basic/DiagnosticSemaKinds.td +++ include/clang/Basic/DiagnosticSemaKinds.td @@ -2189,6 +2189,9 @@ def warn_use_of_temp_while_consumed : Warning< "invocation of method '%0' on a temporary object while it is in the " "'consumed' state">, InGroup, DefaultIgnore; +def warn_attr_on_unconsumable_class : Warning< + "consumed analysis attribute is attached to class '%0' which isn't marked " + "as consumable">, InGroup, DefaultIgnore; // ConsumedStrict warnings def warn_use_in_unknown_state : Warning< Index: lib/Analysis/Consumed.cpp =================================================================== --- lib/Analysis/Consumed.cpp +++ lib/Analysis/Consumed.cpp @@ -65,6 +65,13 @@ llvm_unreachable("invalid enum"); } +static bool isConsumableType(const QualType &QT) { + if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl()) + return RD->hasAttr(); + else + return false; +} + static bool isKnownState(ConsumedState State) { switch (State) { case CS_Unconsumed: @@ -475,7 +482,7 @@ ASTContext &CurrContext = AC.getASTContext(); QualType ThisType = Constructor->getThisType(CurrContext)->getPointeeType(); - if (Analyzer.isConsumableType(ThisType)) { + if (isConsumableType(ThisType)) { if (Constructor->hasAttr() || Constructor->isDefaultConstructor()) { @@ -666,7 +673,7 @@ void ConsumedStmtVisitor::VisitParmVarDecl(const ParmVarDecl *Param) { - if (Analyzer.isConsumableType(Param->getType())) + if (isConsumableType(Param->getType())) StateMap->setState(Param, consumed::CS_Unknown); } @@ -690,7 +697,7 @@ } void ConsumedStmtVisitor::VisitVarDecl(const VarDecl *Var) { - if (Analyzer.isConsumableType(Var->getType())) { + if (isConsumableType(Var->getType())) { if (Var->hasInit()) { PropagationInfo PInfo = PropagationMap.find(Var->getInit())->second; @@ -891,44 +898,6 @@ Map.erase(Var); } -bool ConsumedAnalyzer::isConsumableType(QualType Type) { - const CXXRecordDecl *RD = - dyn_cast_or_null(Type->getAsCXXRecordDecl()); - - if (!RD) return false; - - std::pair Entry = - ConsumableTypeCache.insert(std::make_pair(RD, false)); - - if (Entry.second) - Entry.first->second = hasConsumableAttributes(RD); - - return Entry.first->second; -} - -// TODO: Walk the base classes to see if any of them are unique types. -// (Deferred) -bool ConsumedAnalyzer::hasConsumableAttributes(const CXXRecordDecl *RD) { - for (CXXRecordDecl::method_iterator MI = RD->method_begin(), - ME = RD->method_end(); MI != ME; ++MI) { - - for (Decl::attr_iterator AI = (*MI)->attr_begin(), AE = (*MI)->attr_end(); - AI != AE; ++AI) { - - switch ((*AI)->getKind()) { - case attr::CallableWhenUnconsumed: - case attr::TestsUnconsumed: - return true; - - default: - break; - } - } - } - - return false; -} - bool ConsumedAnalyzer::splitState(const CFGBlock *CurrBlock, const ConsumedStmtVisitor &Visitor) { Index: lib/Sema/SemaDeclAttr.cpp =================================================================== --- lib/Sema/SemaDeclAttr.cpp +++ lib/Sema/SemaDeclAttr.cpp @@ -969,19 +969,52 @@ Attr.getAttributeSpellingListIndex())); } -static void handleConsumesAttr(Sema &S, Decl *D, - const AttributeList &Attr) { +static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { if (!checkAttributeNumArgs(S, Attr, 0)) return; - if (!(isa(D) || isa(D))) { - S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) - << Attr.getName() << ExpectedMethod; + if (!isa(D)) { + S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << + Attr.getName() << ExpectedClass; return; } D->addAttr(::new (S.Context) + ConsumableAttr(Attr.getRange(), S.Context, + Attr.getAttributeSpellingListIndex())); +} + +static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, + const AttributeList &Attr) { + ASTContext &CurrContext = S.getASTContext(); + QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); + + if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { + if (!RD->hasAttr()) { + S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << + RD->getNameAsString(); + + return false; + } + } + + return true; +} + +static void handleConsumesAttr(Sema &S, Decl *D, const AttributeList &Attr) { + if (!checkAttributeNumArgs(S, Attr, 0)) return; + + if (!isa(D)) { + S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << + Attr.getName() << ExpectedMethod; + return; + } + + if (!checkForConsumableClass(S, cast(D), Attr)) + return; + + D->addAttr(::new (S.Context) ConsumesAttr(Attr.getRange(), S.Context, - Attr.getAttributeSpellingListIndex())); + Attr.getAttributeSpellingListIndex())); } static void handleCallableWhenUnconsumedAttr(Sema &S, Decl *D, @@ -989,14 +1022,17 @@ if (!checkAttributeNumArgs(S, Attr, 0)) return; if (!isa(D)) { - S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) - << Attr.getName() << ExpectedMethod; + S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << + Attr.getName() << ExpectedMethod; return; } + if (!checkForConsumableClass(S, cast(D), Attr)) + return; + D->addAttr(::new (S.Context) CallableWhenUnconsumedAttr(Attr.getRange(), S.Context, - Attr.getAttributeSpellingListIndex())); + Attr.getAttributeSpellingListIndex())); } static void handleTestsConsumedAttr(Sema &S, Decl *D, @@ -1004,14 +1040,17 @@ if (!checkAttributeNumArgs(S, Attr, 0)) return; if (!isa(D)) { - S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) - << Attr.getName() << ExpectedMethod; + S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << + Attr.getName() << ExpectedMethod; return; } + if (!checkForConsumableClass(S, cast(D), Attr)) + return; + D->addAttr(::new (S.Context) TestsConsumedAttr(Attr.getRange(), S.Context, - Attr.getAttributeSpellingListIndex())); + Attr.getAttributeSpellingListIndex())); } static void handleTestsUnconsumedAttr(Sema &S, Decl *D, @@ -1019,14 +1058,17 @@ if (!checkAttributeNumArgs(S, Attr, 0)) return; if (!isa(D)) { - S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) - << Attr.getName() << ExpectedMethod; + S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << + Attr.getName() << ExpectedMethod; return; } + if (!checkForConsumableClass(S, cast(D), Attr)) + return; + D->addAttr(::new (S.Context) TestsUnconsumedAttr(Attr.getRange(), S.Context, - Attr.getAttributeSpellingListIndex())); + Attr.getAttributeSpellingListIndex())); } static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, @@ -4995,6 +5037,9 @@ break; // Uniqueness analysis attributes. + case AttributeList::AT_Consumable: + handleConsumableAttr(S, D, Attr); + break; case AttributeList::AT_Consumes: handleConsumesAttr(S, D, Attr); break; Index: test/SemaCXX/warn-consumed-analysis-strict.cpp =================================================================== --- test/SemaCXX/warn-consumed-analysis-strict.cpp +++ test/SemaCXX/warn-consumed-analysis-strict.cpp @@ -1,15 +1,16 @@ // RUN: %clang_cc1 -fsyntax-only -verify -Wconsumed-strict -std=c++11 %s #define CALLABLE_WHEN_UNCONSUMED __attribute__ ((callable_when_unconsumed)) -#define CONSUMES __attribute__ ((consumes)) -#define TESTS_UNCONSUMED __attribute__ ((tests_unconsumed)) +#define CONSUMABLE __attribute__ ((consumable)) +#define CONSUMES __attribute__ ((consumes)) +#define TESTS_UNCONSUMED __attribute__ ((tests_unconsumed)) #define TEST_VAR(Var) Var.isValid() typedef decltype(nullptr) nullptr_t; template -class ConsumableClass { +class CONSUMABLE ConsumableClass { T var; public: Index: test/SemaCXX/warn-consumed-analysis.cpp =================================================================== --- test/SemaCXX/warn-consumed-analysis.cpp +++ test/SemaCXX/warn-consumed-analysis.cpp @@ -1,13 +1,14 @@ // RUN: %clang_cc1 -fsyntax-only -verify -Wconsumed -std=c++11 %s #define CALLABLE_WHEN_UNCONSUMED __attribute__ ((callable_when_unconsumed)) -#define CONSUMES __attribute__ ((consumes)) -#define TESTS_UNCONSUMED __attribute__ ((tests_unconsumed)) +#define CONSUMABLE __attribute__ ((consumable)) +#define CONSUMES __attribute__ ((consumes)) +#define TESTS_UNCONSUMED __attribute__ ((tests_unconsumed)) typedef decltype(nullptr) nullptr_t; template -class ConsumableClass { +class CONSUMABLE ConsumableClass { T var; public: Index: test/SemaCXX/warn-consumed-parsing.cpp =================================================================== --- test/SemaCXX/warn-consumed-parsing.cpp +++ test/SemaCXX/warn-consumed-parsing.cpp @@ -1,29 +1,35 @@ // RUN: %clang_cc1 -fsyntax-only -verify -Wconsumed -std=c++11 %s +#define CONSUMABLE __attribute__ ((consumable)) #define CONSUMES __attribute__ ((consumes)) #define TESTS_UNCONSUMED __attribute__ ((tests_unconsumed)) #define CALLABLE_WHEN_UNCONSUMED __attribute__ ((callable_when_unconsumed)) class AttrTester0 { - void Consumes(void) __attribute__ ((consumes(42))); // expected-error {{attribute takes no arguments}} - bool TestsUnconsumed(void) __attribute__ ((tests_unconsumed(42))); // expected-error {{attribute takes no arguments}} - void CallableWhenUnconsumed(void) + void Consumes() __attribute__ ((consumes(42))); // expected-error {{attribute takes no arguments}} + bool TestsUnconsumed() __attribute__ ((tests_unconsumed(42))); // expected-error {{attribute takes no arguments}} + void CallableWhenUnconsumed() __attribute__ ((callable_when_unconsumed(42))); // expected-error {{attribute takes no arguments}} }; int var0 CONSUMES; // expected-warning {{'consumes' attribute only applies to methods}} int var1 TESTS_UNCONSUMED; // expected-warning {{'tests_unconsumed' attribute only applies to methods}} int var2 CALLABLE_WHEN_UNCONSUMED; // expected-warning {{'callable_when_unconsumed' attribute only applies to methods}} +int var3 CONSUMABLE; // expected-warning {{'consumable' attribute only applies to classes}} -void function0(void) CONSUMES; // expected-warning {{'consumes' attribute only applies to methods}} -void function1(void) TESTS_UNCONSUMED; // expected-warning {{'tests_unconsumed' attribute only applies to methods}} -void function2(void) CALLABLE_WHEN_UNCONSUMED; // expected-warning {{'callable_when_unconsumed' attribute only applies to methods}} +void function0() CONSUMES; // expected-warning {{'consumes' attribute only applies to methods}} +void function1() TESTS_UNCONSUMED; // expected-warning {{'tests_unconsumed' attribute only applies to methods}} +void function2() CALLABLE_WHEN_UNCONSUMED; // expected-warning {{'callable_when_unconsumed' attribute only applies to methods}} +void function3() CONSUMABLE; // expected-warning {{'consumable' attribute only applies to classes}} -class AttrTester1 { - void consumes(void) CONSUMES; - bool testsUnconsumed(void) TESTS_UNCONSUMED; +class CONSUMABLE AttrTester1 { + void callableWhenUnconsumed() CALLABLE_WHEN_UNCONSUMED; + void consumes() CONSUMES; + bool testsUnconsumed() TESTS_UNCONSUMED; }; class AttrTester2 { - void callableWhenUnconsumed(void) CALLABLE_WHEN_UNCONSUMED; + void callableWhenUnconsumed() CALLABLE_WHEN_UNCONSUMED; // expected-warning {{consumed analysis attribute is attached to class 'AttrTester2' which isn't marked as consumable}} + void consumes() CONSUMES; // expected-warning {{consumed analysis attribute is attached to class 'AttrTester2' which isn't marked as consumable}} + bool testsUnconsumed() TESTS_UNCONSUMED; // expected-warning {{consumed analysis attribute is attached to class 'AttrTester2' which isn't marked as consumable}} };