Bug 1192130 - Part 1: Add MOZ_NON_AUTOABLE to restrict using auto in place of certain types, r=ehsan

This commit is contained in:
Michael Layzell 2015-08-11 17:44:36 -04:00
parent be3d67125a
commit 40e60e8a8e
3 changed files with 74 additions and 0 deletions

View File

@ -103,6 +103,11 @@ private:
virtual void run(const MatchFinder::MatchResult &Result);
};
class NoAutoTypeChecker : public MatchFinder::MatchCallback {
public:
virtual void run(const MatchFinder::MatchResult &Result);
};
ScopeChecker scopeChecker;
ArithmeticArgChecker arithmeticArgChecker;
TrivialCtorDtorChecker trivialCtorDtorChecker;
@ -114,6 +119,7 @@ private:
NeedsNoVTableTypeChecker needsNoVTableTypeChecker;
NonMemMovableChecker nonMemMovableChecker;
ExplicitImplicitChecker explicitImplicitChecker;
NoAutoTypeChecker noAutoTypeChecker;
MatchFinder astMatcher;
};
@ -786,6 +792,15 @@ AST_MATCHER(CXXRecordDecl, isConcreteClass) {
return !Node.isAbstract();
}
AST_MATCHER(QualType, autoNonAutoableType) {
if (const AutoType *T = Node->getContainedAutoType()) {
if (const CXXRecordDecl *Rec = T->getAsCXXRecordDecl()) {
return MozChecker::hasCustomAnnotation(Rec, "moz_non_autoable");
}
}
return false;
}
}
}
@ -1032,6 +1047,9 @@ DiagnosticsMatcher::DiagnosticsMatcher() {
ofClass(allOf(isConcreteClass(), decl().bind("class"))),
unless(isMarkedImplicit())).bind("ctor"),
&explicitImplicitChecker);
astMatcher.addMatcher(varDecl(hasType(autoNonAutoableType())
).bind("node"), &noAutoTypeChecker);
}
// These enum variants determine whether an allocation has occured in the code.
@ -1348,6 +1366,20 @@ void DiagnosticsMatcher::ExplicitImplicitChecker::run(
Diag.Report(Ctor->getLocation(), NoteID);
}
void DiagnosticsMatcher::NoAutoTypeChecker::run(
const MatchFinder::MatchResult &Result) {
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
unsigned ErrorID = Diag.getDiagnosticIDs()->getCustomDiagID(
DiagnosticIDs::Error, "Cannot use auto to declare a variable of type %0");
unsigned NoteID = Diag.getDiagnosticIDs()->getCustomDiagID(
DiagnosticIDs::Note, "Please write out this type explicitly");
const VarDecl *D = Result.Nodes.getNodeAs<VarDecl>("node");
Diag.Report(D->getLocation(), ErrorID) << D->getType();
Diag.Report(D->getLocation(), NoteID);
}
class MozCheckAction : public PluginASTAction {
public:
ASTConsumerPtr CreateASTConsumer(CompilerInstance &CI, StringRef fileName) override {

View File

@ -0,0 +1,41 @@
#define MOZ_NON_AUTOABLE __attribute__((annotate("moz_non_autoable")))
template<class T>
struct MOZ_NON_AUTOABLE ExplicitTypeTemplate {};
struct MOZ_NON_AUTOABLE ExplicitType {};
struct NonExplicitType {};
void f() {
{
ExplicitType a;
auto b = a; // expected-error {{Cannot use auto to declare a variable of type 'ExplicitType'}} expected-note {{Please write out this type explicitly}}
auto &br = a; // expected-error {{Cannot use auto to declare a variable of type 'ExplicitType &'}} expected-note {{Please write out this type explicitly}}
const auto &brc = a; // expected-error {{Cannot use auto to declare a variable of type 'const ExplicitType &'}} expected-note {{Please write out this type explicitly}}
auto *bp = &a; // expected-error {{Cannot use auto to declare a variable of type 'ExplicitType *'}} expected-note {{Please write out this type explicitly}}
const auto *bpc = &a; // expected-error {{Cannot use auto to declare a variable of type 'const ExplicitType *'}} expected-note {{Please write out this type explicitly}}
}
{
ExplicitTypeTemplate<int> a;
auto b = a; // expected-error {{Cannot use auto to declare a variable of type 'ExplicitTypeTemplate<int>'}} expected-note {{Please write out this type explicitly}}
auto &br = a; // expected-error {{Cannot use auto to declare a variable of type 'ExplicitTypeTemplate<int> &'}} expected-note {{Please write out this type explicitly}}
const auto &brc = a; // expected-error {{Cannot use auto to declare a variable of type 'const ExplicitTypeTemplate<int> &'}} expected-note {{Please write out this type explicitly}}
auto *bp = &a; // expected-error {{Cannot use auto to declare a variable of type 'ExplicitTypeTemplate<int> *'}} expected-note {{Please write out this type explicitly}}
const auto *bpc = &a; // expected-error {{Cannot use auto to declare a variable of type 'const ExplicitTypeTemplate<int> *'}} expected-note {{Please write out this type explicitly}}
}
{
NonExplicitType c;
auto d = c;
auto &dr = c;
const auto &drc = c;
auto *dp = &c;
const auto *dpc = &c;
}
}
ExplicitType A;
auto B = A; // expected-error {{Cannot use auto to declare a variable of type 'ExplicitType'}} expected-note {{Please write out this type explicitly}}
NonExplicitType C;
auto D = C;

View File

@ -19,6 +19,7 @@ SOURCES += [
'TestNeedsNoVTableType.cpp',
'TestNoAddRefReleaseOnReturn.cpp',
'TestNoArithmeticExprInArgument.cpp',
'TestNoAutoType.cpp',
'TestNoDuplicateRefCntMember.cpp',
'TestNonHeapClass.cpp',
'TestNonMemMovable.cpp',