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
["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 {
// 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
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;
"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;
}
static bool isTestingFunction(const FunctionDecl *FunDecl) {
- return FunDecl->hasAttr<TestsUnconsumedAttr>();
+ return FunDecl->hasAttr<TestsTypestateAttr>();
}
static ConsumedState mapConsumableAttrState(const QualType QT) {
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;
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);
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) {
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);
}
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);
}
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) {
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;
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,
handleAcquiredAfterAttr(S, D, Attr);
break;
- // Uniqueness analysis attributes.
+ // Consumed analysis attributes.
case AttributeList::AT_Consumable:
handleConsumableAttr(S, D, Attr);
break;
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);
+++ /dev/null
-// 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;
- }
-}
#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;
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();
} 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() {
#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
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}}
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'}}
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}}