r192513 - Consumed analysis: switch from tests_consumed/unconsumed to a general
DeLesley Hutchins
delesley at google.com
Fri Oct 11 15:30:48 PDT 2013
Author: delesley
Date: Fri Oct 11 17:30:48 2013
New Revision: 192513
URL: http://llvm.org/viewvc/llvm-project?rev=192513&view=rev
Log:
Consumed analysis: switch from tests_consumed/unconsumed to a general
tests_typestate attribute. Patch by chris.wailes at gmail.com.
Removed:
cfe/trunk/test/SemaCXX/warn-consumed-analysis-strict.cpp
Modified:
cfe/trunk/include/clang/Analysis/Analyses/Consumed.h
cfe/trunk/include/clang/Basic/Attr.td
cfe/trunk/include/clang/Basic/DiagnosticGroups.td
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Analysis/Consumed.cpp
cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
cfe/trunk/lib/Sema/SemaDeclAttr.cpp
cfe/trunk/test/SemaCXX/warn-consumed-analysis.cpp
cfe/trunk/test/SemaCXX/warn-consumed-parsing.cpp
Modified: cfe/trunk/include/clang/Analysis/Analyses/Consumed.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/Analyses/Consumed.h?rev=192513&r1=192512&r2=192513&view=diff
==============================================================================
--- cfe/trunk/include/clang/Analysis/Analyses/Consumed.h (original)
+++ cfe/trunk/include/clang/Analysis/Analyses/Consumed.h Fri Oct 11 17:30:48 2013
@@ -74,17 +74,6 @@ namespace consumed {
virtual void warnReturnTypestateMismatch(SourceLocation Loc,
StringRef ExpectedState,
StringRef ObservedState) {}
-
- /// \brief Warn about unnecessary-test errors.
- /// \param VariableName -- The name of the variable that holds the unique
- /// value.
- ///
- /// \param VariableState -- The known state of the value.
- ///
- /// \param Loc -- The SourceLocation of the unnecessary test.
- virtual void warnUnnecessaryTest(StringRef VariableName,
- StringRef VariableState,
- SourceLocation Loc) {}
/// \brief Warn about use-while-consumed errors.
/// \param MethodName -- The name of the method that was incorrectly
Modified: cfe/trunk/include/clang/Basic/Attr.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Attr.td?rev=192513&r1=192512&r2=192513&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/Attr.td (original)
+++ cfe/trunk/include/clang/Basic/Attr.td Fri Oct 11 17:30:48 2013
@@ -967,14 +967,12 @@ def CallableWhen : InheritableAttr {
["Unknown", "Consumed", "Unconsumed"]>];
}
-def TestsUnconsumed : InheritableAttr {
- let Spellings = [GNU<"tests_unconsumed">];
- let Subjects = [CXXMethod];
-}
-
-def TestsConsumed : InheritableAttr {
- let Spellings = [GNU<"tests_consumed">];
+def TestsTypestate : InheritableAttr {
+ let Spellings = [GNU<"tests_typestate">];
let Subjects = [CXXMethod];
+ let Args = [EnumArgument<"TestState", "ConsumedState",
+ ["consumed", "unconsumed"],
+ ["Consumed", "Unconsumed"]>];
}
def Consumes : InheritableAttr {
Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=192513&r1=192512&r2=192513&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Fri Oct 11 17:30:48 2013
@@ -506,7 +506,6 @@ def ThreadSafetyBeta : DiagGroup<"thread
// Uniqueness Analysis warnings
def Consumed : DiagGroup<"consumed">;
-def ConsumedStrict : DiagGroup<"consumed-strict", [Consumed]>;
// Note that putting warnings in -Wall will not disable them by default. If a
// warning should be active _only_ when -Wall is passed in, mark it as
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=192513&r1=192512&r2=192513&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Oct 11 17:30:48 2013
@@ -2216,6 +2216,8 @@ def warn_attr_on_unconsumable_class : Wa
def warn_return_typestate_for_unconsumable_type : Warning<
"return state set for an unconsumable type '%0'">, InGroup<Consumed>,
DefaultIgnore;
+def warn_invalid_test_typestate : Warning<
+ "invalid test typestate '%0'">, InGroup<Consumed>, DefaultIgnore;
def warn_return_typestate_mismatch : Warning<
"return value not in expected state; expected '%0', observed '%1'">,
InGroup<Consumed>, DefaultIgnore;
@@ -2223,11 +2225,6 @@ def warn_loop_state_mismatch : Warning<
"state of variable '%0' must match at the entry and exit of loop">,
InGroup<Consumed>, DefaultIgnore;
-// ConsumedStrict warnings
-def warn_unnecessary_test : Warning<
- "unnecessary test. Variable '%0' is known to be in the '%1' state">,
- InGroup<ConsumedStrict>, DefaultIgnore;
-
def warn_impcast_vector_scalar : Warning<
"implicit conversion turns vector to scalar: %0 to %1">,
InGroup<Conversion>, DefaultIgnore;
Modified: cfe/trunk/lib/Analysis/Consumed.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/Consumed.cpp?rev=192513&r1=192512&r2=192513&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/Consumed.cpp (original)
+++ cfe/trunk/lib/Analysis/Consumed.cpp Fri Oct 11 17:30:48 2013
@@ -137,7 +137,7 @@ static bool isKnownState(ConsumedState S
}
static bool isTestingFunction(const FunctionDecl *FunDecl) {
- return FunDecl->hasAttr<TestsUnconsumedAttr>();
+ return FunDecl->hasAttr<TestsTypestateAttr>();
}
static ConsumedState mapConsumableAttrState(const QualType QT) {
@@ -187,6 +187,17 @@ static StringRef stateToString(ConsumedS
llvm_unreachable("invalid enum");
}
+static ConsumedState testsFor(const FunctionDecl *FunDecl) {
+ assert(isTestingFunction(FunDecl));
+ switch (FunDecl->getAttr<TestsTypestateAttr>()->getTestState()) {
+ case TestsTypestateAttr::Unconsumed:
+ return CS_Unconsumed;
+ case TestsTypestateAttr::Consumed:
+ return CS_Consumed;
+ }
+ llvm_unreachable("invalid enum");
+}
+
namespace {
struct VarTestResult {
const VarDecl *Var;
@@ -341,7 +352,6 @@ class ConsumedStmtVisitor : public Const
ConsumedStateMap *StateMap;
MapType PropagationMap;
void forwardInfo(const Stmt *From, const Stmt *To);
- void handleTestingFunctionCall(const CallExpr *Call, const VarDecl *Var);
bool isLikeMoveAssignment(const CXXMethodDecl *MethodDecl);
void propagateReturnType(const Stmt *Call, const FunctionDecl *Fun,
QualType ReturnType);
@@ -428,22 +438,6 @@ void ConsumedStmtVisitor::forwardInfo(co
PropagationMap.insert(PairType(To, Entry->second));
}
-void ConsumedStmtVisitor::handleTestingFunctionCall(const CallExpr *Call,
- const VarDecl *Var) {
-
- ConsumedState VarState = StateMap->getState(Var);
-
- if (VarState != CS_Unknown) {
- SourceLocation CallLoc = Call->getExprLoc();
-
- if (!CallLoc.isMacroID())
- Analyzer.WarningsHandler.warnUnnecessaryTest(Var->getNameAsString(),
- stateToString(VarState), CallLoc);
- }
-
- PropagationMap.insert(PairType(Call, PropagationInfo(Var, CS_Unconsumed)));
-}
-
bool ConsumedStmtVisitor::isLikeMoveAssignment(
const CXXMethodDecl *MethodDecl) {
@@ -643,7 +637,8 @@ void ConsumedStmtVisitor::VisitCXXMember
if (PInfo.isVar()) {
if (isTestingFunction(MethodDecl))
- handleTestingFunctionCall(Call, PInfo.getVar());
+ PropagationMap.insert(PairType(Call,
+ PropagationInfo(PInfo.getVar(), testsFor(MethodDecl))));
else if (MethodDecl->hasAttr<ConsumesAttr>())
StateMap->setState(PInfo.getVar(), consumed::CS_Consumed);
}
@@ -731,7 +726,8 @@ void ConsumedStmtVisitor::VisitCXXOperat
if (PInfo.isVar()) {
if (isTestingFunction(FunDecl))
- handleTestingFunctionCall(Call, PInfo.getVar());
+ PropagationMap.insert(PairType(Call,
+ PropagationInfo(PInfo.getVar(), testsFor(FunDecl))));
else if (FunDecl->hasAttr<ConsumesAttr>())
StateMap->setState(PInfo.getVar(), consumed::CS_Consumed);
}
Modified: cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp?rev=192513&r1=192512&r2=192513&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp (original)
+++ cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp Fri Oct 11 17:30:48 2013
@@ -1501,15 +1501,6 @@ public:
Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
}
- void warnUnnecessaryTest(StringRef VariableName, StringRef VariableState,
- SourceLocation Loc) {
-
- PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_unnecessary_test) <<
- VariableName << VariableState);
-
- Warnings.push_back(DelayedDiag(Warning, OptionalNotes()));
- }
-
void warnUseOfTempInInvalidState(StringRef MethodName, StringRef State,
SourceLocation Loc) {
Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=192513&r1=192512&r2=192513&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Fri Oct 11 17:30:48 2013
@@ -1079,8 +1079,11 @@ static void handleCallableWhenAttr(Sema
States.size(), Attr.getAttributeSpellingListIndex()));
}
-static void handleTestsConsumedAttr(Sema &S, Decl *D,
- const AttributeList &Attr) {
+
+static void handleTestsTypestateAttr(Sema &S, Decl *D,
+ const AttributeList &Attr) {
+ if (!checkAttributeNumArgs(S, Attr, 1)) return;
+
if (!isa<CXXMethodDecl>(D)) {
S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
Attr.getName() << ExpectedMethod;
@@ -1090,25 +1093,29 @@ static void handleTestsConsumedAttr(Sema
if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
return;
- D->addAttr(::new (S.Context)
- TestsConsumedAttr(Attr.getRange(), S.Context,
- Attr.getAttributeSpellingListIndex()));
-}
-
-static void handleTestsUnconsumedAttr(Sema &S, Decl *D,
- const AttributeList &Attr) {
- if (!isa<CXXMethodDecl>(D)) {
- S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) <<
- Attr.getName() << ExpectedMethod;
- return;
- }
+ TestsTypestateAttr::ConsumedState TestState;
- if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr))
+ if (Attr.isArgIdent(0)) {
+ StringRef Param = Attr.getArgAsIdent(0)->Ident->getName();
+
+ if (Param == "consumed") {
+ TestState = TestsTypestateAttr::Consumed;
+ } else if (Param == "unconsumed") {
+ TestState = TestsTypestateAttr::Unconsumed;
+ } else {
+ S.Diag(Attr.getLoc(), diag::warn_invalid_test_typestate) << Param;
+ return;
+ }
+
+ } else {
+ S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) <<
+ Attr.getName() << AANT_ArgumentIdentifier;
return;
+ }
D->addAttr(::new (S.Context)
- TestsUnconsumedAttr(Attr.getRange(), S.Context,
- Attr.getAttributeSpellingListIndex()));
+ TestsTypestateAttr(Attr.getRange(), S.Context, TestState,
+ Attr.getAttributeSpellingListIndex()));
}
static void handleReturnTypestateAttr(Sema &S, Decl *D,
@@ -4782,7 +4789,7 @@ static void ProcessDeclAttribute(Sema &S
handleAcquiredAfterAttr(S, D, Attr);
break;
- // Uniqueness analysis attributes.
+ // Consumed analysis attributes.
case AttributeList::AT_Consumable:
handleConsumableAttr(S, D, Attr);
break;
@@ -4792,11 +4799,8 @@ static void ProcessDeclAttribute(Sema &S
case AttributeList::AT_CallableWhen:
handleCallableWhenAttr(S, D, Attr);
break;
- case AttributeList::AT_TestsConsumed:
- handleTestsConsumedAttr(S, D, Attr);
- break;
- case AttributeList::AT_TestsUnconsumed:
- handleTestsUnconsumedAttr(S, D, Attr);
+ case AttributeList::AT_TestsTypestate:
+ handleTestsTypestateAttr(S, D, Attr);
break;
case AttributeList::AT_ReturnTypestate:
handleReturnTypestateAttr(S, D, Attr);
Removed: cfe/trunk/test/SemaCXX/warn-consumed-analysis-strict.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-consumed-analysis-strict.cpp?rev=192512&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-consumed-analysis-strict.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-consumed-analysis-strict.cpp (removed)
@@ -1,66 +0,0 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -Wconsumed-strict -std=c++11 %s
-
-#define CALLABLE_WHEN(...) __attribute__ ((callable_when(__VA_ARGS__)))
-#define CONSUMABLE(state) __attribute__ ((consumable(state)))
-#define CONSUMES __attribute__ ((consumes))
-#define RETURN_TYPESTATE(state) __attribute__ ((return_typestate(state)))
-#define TESTS_UNCONSUMED __attribute__ ((tests_unconsumed))
-
-#define TEST_VAR(Var) Var.isValid()
-
-typedef decltype(nullptr) nullptr_t;
-
-template <typename T>
-class CONSUMABLE(unconsumed) ConsumableClass {
- T var;
-
- public:
- ConsumableClass();
- ConsumableClass(nullptr_t p) RETURN_TYPESTATE(consumed);
- ConsumableClass(T val);
- ConsumableClass(ConsumableClass<T> &other);
- ConsumableClass(ConsumableClass<T> &&other);
-
- ConsumableClass<T>& operator=(ConsumableClass<T> &other);
- ConsumableClass<T>& operator=(ConsumableClass<T> &&other);
- ConsumableClass<T>& operator=(nullptr_t) CONSUMES;
-
- template <typename U>
- ConsumableClass<T>& operator=(ConsumableClass<U> &other);
-
- template <typename U>
- ConsumableClass<T>& operator=(ConsumableClass<U> &&other);
-
- void operator()(int a) CONSUMES;
- void operator*() const CALLABLE_WHEN("unconsumed");
- void unconsumedCall() const CALLABLE_WHEN("unconsumed");
-
- bool isValid() const TESTS_UNCONSUMED;
- operator bool() const TESTS_UNCONSUMED;
- bool operator!=(nullptr_t) const TESTS_UNCONSUMED;
-
- void constCall() const;
- void nonconstCall();
-
- void consume() CONSUMES;
-};
-
-void testIfStmt() {
- ConsumableClass<int> var;
-
- if (var.isValid()) { // expected-warning {{unnecessary test. Variable 'var' is known to be in the 'consumed' state}}
-
- // Empty
-
- } else {
- // Empty
- }
-}
-
-void testNoWarnTestFromMacroExpansion() {
- ConsumableClass<int> var(42);
-
- if (TEST_VAR(var)) {
- *var;
- }
-}
Modified: cfe/trunk/test/SemaCXX/warn-consumed-analysis.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-consumed-analysis.cpp?rev=192513&r1=192512&r2=192513&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-consumed-analysis.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-consumed-analysis.cpp Fri Oct 11 17:30:48 2013
@@ -6,7 +6,7 @@
#define CONSUMABLE(state) __attribute__ ((consumable(state)))
#define CONSUMES __attribute__ ((consumes))
#define RETURN_TYPESTATE(state) __attribute__ ((return_typestate(state)))
-#define TESTS_UNCONSUMED __attribute__ ((tests_unconsumed))
+#define TESTS_TYPESTATE(state) __attribute__ ((tests_typestate(state)))
typedef decltype(nullptr) nullptr_t;
@@ -36,9 +36,10 @@ public:
void unconsumedCall() const CALLABLE_WHEN("unconsumed");
void callableWhenUnknown() const CALLABLE_WHEN("unconsumed", "unknown");
- bool isValid() const TESTS_UNCONSUMED;
- operator bool() const TESTS_UNCONSUMED;
- bool operator!=(nullptr_t) const TESTS_UNCONSUMED;
+ bool isValid() const TESTS_TYPESTATE(unconsumed);
+ operator bool() const TESTS_TYPESTATE(unconsumed);
+ bool operator!=(nullptr_t) const TESTS_TYPESTATE(unconsumed);
+ bool operator==(nullptr_t) const TESTS_TYPESTATE(consumed);
void constCall() const;
void nonconstCall();
@@ -146,6 +147,12 @@ void testIfStmt() {
} else {
*var; // expected-warning {{invalid invocation of method 'operator*' on object 'var' while it is in the 'consumed' state}}
}
+
+ if (var == nullptr) {
+ *var; // expected-warning {{invalid invocation of method 'operator*' on object 'var' while it is in the 'consumed' state}}
+ } else {
+ // Empty
+ }
}
void testComplexConditionals0() {
Modified: cfe/trunk/test/SemaCXX/warn-consumed-parsing.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-consumed-parsing.cpp?rev=192513&r1=192512&r2=192513&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-consumed-parsing.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-consumed-parsing.cpp Fri Oct 11 17:30:48 2013
@@ -4,7 +4,7 @@
#define CONSUMABLE(state) __attribute__ ((consumable(state)))
#define CONSUMES __attribute__ ((consumes))
#define RETURN_TYPESTATE(state) __attribute__ ((return_typestate(state)))
-#define TESTS_UNCONSUMED __attribute__ ((tests_unconsumed))
+#define TESTS_TYPESTATE(state) __attribute__ ((tests_typestate(state)))
// FIXME: This test is here because the warning is issued by the Consumed
// analysis, not SemaDeclAttr. The analysis won't run after an error
@@ -18,18 +18,18 @@ int returnTypestateForUnconsumable() {
class AttrTester0 {
void consumes() __attribute__ ((consumes(42))); // expected-error {{attribute takes no arguments}}
- bool testsUnconsumed() __attribute__ ((tests_unconsumed(42))); // expected-error {{attribute takes no arguments}}
+ bool testsUnconsumed() __attribute__ ((tests_typestate())); // expected-error {{attribute takes one argument}}
void callableWhen() __attribute__ ((callable_when())); // expected-error {{attribute takes at least 1 argument}}
};
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 var1 TESTS_TYPESTATE(consumed); // expected-warning {{'tests_typestate' attribute only applies to methods}}
int var2 CALLABLE_WHEN(42); // expected-warning {{'callable_when' attribute only applies to methods}}
int var3 CONSUMABLE(consumed); // expected-warning {{'consumable' attribute only applies to classes}}
int var4 RETURN_TYPESTATE(consumed); // expected-warning {{'return_typestate' attribute only applies to functions}}
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 function1() TESTS_TYPESTATE(consumed); // expected-warning {{'tests_typestate' attribute only applies to methods}}
void function2() CALLABLE_WHEN(42); // expected-warning {{'callable_when' attribute only applies to methods}}
void function3() CONSUMABLE(consumed); // expected-warning {{'consumable' attribute only applies to classes}}
@@ -38,7 +38,7 @@ class CONSUMABLE(unknown) AttrTester1 {
void callableWhen1() CALLABLE_WHEN(42); // expected-error {{'callable_when' attribute requires a string}}
void callableWhen2() CALLABLE_WHEN("foo"); // expected-warning {{'callable_when' attribute argument not supported: foo}}
void consumes() CONSUMES;
- bool testsUnconsumed() TESTS_UNCONSUMED;
+ bool testsUnconsumed() TESTS_TYPESTATE(consumed);
};
AttrTester1 returnTypestateTester0() RETURN_TYPESTATE(not_a_state); // expected-warning {{'return_typestate' attribute argument not supported: 'not_a_state'}}
@@ -47,7 +47,7 @@ AttrTester1 returnTypestateTester1() RET
class AttrTester2 {
void callableWhen() CALLABLE_WHEN("unconsumed"); // expected-warning {{consumed analysis attribute is attached to member of class 'AttrTester2' which isn't marked as consumable}}
void consumes() CONSUMES; // expected-warning {{consumed analysis attribute is attached to member of class 'AttrTester2' which isn't marked as consumable}}
- bool testsUnconsumed() TESTS_UNCONSUMED; // expected-warning {{consumed analysis attribute is attached to member of class 'AttrTester2' which isn't marked as consumable}}
+ bool testsUnconsumed() TESTS_TYPESTATE(consumed); // expected-warning {{consumed analysis attribute is attached to member of class 'AttrTester2' which isn't marked as consumable}}
};
class CONSUMABLE(42) AttrTester3; // expected-error {{'consumable' attribute requires an identifier}}
More information about the cfe-commits
mailing list