2013-03-24 06:12:25 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2015-06-19 00:34:27 +03:00
|
|
|
|
2013-03-24 06:12:25 +04:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2013-04-12 07:20:02 +04:00
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
2013-03-24 06:12:25 +04:00
|
|
|
#include "clang/Basic/Version.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendPluginRegistry.h"
|
2013-04-12 07:20:02 +04:00
|
|
|
#include "clang/Frontend/MultiplexConsumer.h"
|
2013-03-24 06:12:25 +04:00
|
|
|
#include "clang/Sema/Sema.h"
|
2013-05-28 01:04:18 +04:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2014-12-13 22:18:54 +03:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2014-12-10 18:46:10 +03:00
|
|
|
#include <memory>
|
2013-03-24 06:12:25 +04:00
|
|
|
|
|
|
|
#define CLANG_VERSION_FULL (CLANG_VERSION_MAJOR * 100 + CLANG_VERSION_MINOR)
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
2014-12-10 18:46:10 +03:00
|
|
|
#if CLANG_VERSION_FULL >= 306
|
|
|
|
typedef std::unique_ptr<ASTConsumer> ASTConsumerPtr;
|
|
|
|
#else
|
|
|
|
typedef ASTConsumer *ASTConsumerPtr;
|
|
|
|
#endif
|
|
|
|
|
2013-03-24 06:12:25 +04:00
|
|
|
namespace {
|
2013-04-12 07:20:02 +04:00
|
|
|
|
2015-07-05 19:16:55 +03:00
|
|
|
QualType GetCallReturnType(const CallExpr *expr) {
|
|
|
|
#if CLANG_VERSION_FULL >= 307
|
|
|
|
return expr->getCallReturnType(expr->getCalleeDecl()->getASTContext());
|
|
|
|
#else
|
|
|
|
return expr->getCallReturnType();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-04-12 07:20:02 +04:00
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
class DiagnosticsMatcher {
|
|
|
|
public:
|
|
|
|
DiagnosticsMatcher();
|
|
|
|
|
2014-12-10 18:46:10 +03:00
|
|
|
ASTConsumerPtr makeASTConsumer() {
|
2013-04-12 07:20:02 +04:00
|
|
|
return astMatcher.newASTConsumer();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-12-23 02:10:32 +03:00
|
|
|
class ScopeChecker : public MatchFinder::MatchCallback {
|
2013-05-28 01:05:02 +04:00
|
|
|
public:
|
|
|
|
virtual void run(const MatchFinder::MatchResult &Result);
|
|
|
|
};
|
|
|
|
|
2014-12-18 23:25:15 +03:00
|
|
|
class ArithmeticArgChecker : public MatchFinder::MatchCallback {
|
|
|
|
public:
|
|
|
|
virtual void run(const MatchFinder::MatchResult &Result);
|
|
|
|
};
|
|
|
|
|
2014-12-23 02:15:42 +03:00
|
|
|
class TrivialCtorDtorChecker : public MatchFinder::MatchCallback {
|
|
|
|
public:
|
|
|
|
virtual void run(const MatchFinder::MatchResult &Result);
|
|
|
|
};
|
|
|
|
|
2014-12-21 20:33:25 +03:00
|
|
|
class NaNExprChecker : public MatchFinder::MatchCallback {
|
|
|
|
public:
|
|
|
|
virtual void run(const MatchFinder::MatchResult &Result);
|
|
|
|
};
|
|
|
|
|
2014-12-25 22:44:02 +03:00
|
|
|
class NoAddRefReleaseOnReturnChecker : public MatchFinder::MatchCallback {
|
|
|
|
public:
|
|
|
|
virtual void run(const MatchFinder::MatchResult &Result);
|
|
|
|
};
|
|
|
|
|
2015-04-11 03:28:34 +03:00
|
|
|
class RefCountedInsideLambdaChecker : public MatchFinder::MatchCallback {
|
|
|
|
public:
|
|
|
|
virtual void run(const MatchFinder::MatchResult &Result);
|
|
|
|
};
|
|
|
|
|
2015-04-11 06:05:46 +03:00
|
|
|
class ExplicitOperatorBoolChecker : public MatchFinder::MatchCallback {
|
|
|
|
public:
|
|
|
|
virtual void run(const MatchFinder::MatchResult &Result);
|
|
|
|
};
|
|
|
|
|
2015-07-30 05:01:58 +03:00
|
|
|
class NoDuplicateRefCntMemberChecker : public MatchFinder::MatchCallback {
|
|
|
|
public:
|
|
|
|
virtual void run(const MatchFinder::MatchResult &Result);
|
|
|
|
};
|
|
|
|
|
2015-06-20 02:30:51 +03:00
|
|
|
class NeedsNoVTableTypeChecker : public MatchFinder::MatchCallback {
|
|
|
|
public:
|
|
|
|
virtual void run(const MatchFinder::MatchResult &Result);
|
|
|
|
};
|
|
|
|
|
2015-06-19 00:34:27 +03:00
|
|
|
class NonMemMovableChecker : public MatchFinder::MatchCallback {
|
|
|
|
public:
|
|
|
|
virtual void run(const MatchFinder::MatchResult &Result);
|
|
|
|
};
|
|
|
|
|
2015-07-27 16:10:30 +03:00
|
|
|
class ExplicitImplicitChecker : public MatchFinder::MatchCallback {
|
|
|
|
public:
|
|
|
|
virtual void run(const MatchFinder::MatchResult &Result);
|
|
|
|
};
|
|
|
|
|
2015-08-12 00:44:36 +03:00
|
|
|
class NoAutoTypeChecker : public MatchFinder::MatchCallback {
|
|
|
|
public:
|
|
|
|
virtual void run(const MatchFinder::MatchResult &Result);
|
|
|
|
};
|
|
|
|
|
2015-08-07 00:05:19 +03:00
|
|
|
ScopeChecker scopeChecker;
|
2014-12-18 23:25:15 +03:00
|
|
|
ArithmeticArgChecker arithmeticArgChecker;
|
2014-12-23 02:15:42 +03:00
|
|
|
TrivialCtorDtorChecker trivialCtorDtorChecker;
|
2014-12-21 20:33:25 +03:00
|
|
|
NaNExprChecker nanExprChecker;
|
2014-12-25 22:44:02 +03:00
|
|
|
NoAddRefReleaseOnReturnChecker noAddRefReleaseOnReturnChecker;
|
2015-04-11 03:28:34 +03:00
|
|
|
RefCountedInsideLambdaChecker refCountedInsideLambdaChecker;
|
2015-04-11 06:05:46 +03:00
|
|
|
ExplicitOperatorBoolChecker explicitOperatorBoolChecker;
|
2015-07-30 05:01:58 +03:00
|
|
|
NoDuplicateRefCntMemberChecker noDuplicateRefCntMemberChecker;
|
2015-06-20 02:30:51 +03:00
|
|
|
NeedsNoVTableTypeChecker needsNoVTableTypeChecker;
|
2015-06-19 00:34:27 +03:00
|
|
|
NonMemMovableChecker nonMemMovableChecker;
|
2015-07-27 16:10:30 +03:00
|
|
|
ExplicitImplicitChecker explicitImplicitChecker;
|
2015-08-12 00:44:36 +03:00
|
|
|
NoAutoTypeChecker noAutoTypeChecker;
|
2013-04-12 07:20:02 +04:00
|
|
|
MatchFinder astMatcher;
|
|
|
|
};
|
|
|
|
|
2014-12-13 22:18:54 +03:00
|
|
|
namespace {
|
|
|
|
|
2015-04-11 06:05:46 +03:00
|
|
|
std::string getDeclarationNamespace(const Decl *decl) {
|
2014-12-13 22:18:54 +03:00
|
|
|
const DeclContext *DC = decl->getDeclContext()->getEnclosingNamespaceContext();
|
|
|
|
const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
|
|
|
|
if (!ND) {
|
2015-04-11 06:05:46 +03:00
|
|
|
return "";
|
2014-12-13 22:18:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
while (const DeclContext *ParentDC = ND->getParent()) {
|
|
|
|
if (!isa<NamespaceDecl>(ParentDC)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ND = cast<NamespaceDecl>(ParentDC);
|
|
|
|
}
|
|
|
|
|
2014-12-16 20:54:06 +03:00
|
|
|
const auto& name = ND->getName();
|
2015-04-11 06:05:46 +03:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isInIgnoredNamespaceForImplicitCtor(const Decl *decl) {
|
|
|
|
std::string name = getDeclarationNamespace(decl);
|
|
|
|
if (name == "") {
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-16 20:54:06 +03:00
|
|
|
|
2015-06-15 20:31:44 +03:00
|
|
|
|
|
|
|
return name == "std" || // standard C++ lib
|
|
|
|
name == "__gnu_cxx" || // gnu C++ lib
|
|
|
|
name == "boost" || // boost
|
|
|
|
name == "webrtc" || // upstream webrtc
|
|
|
|
name.substr(0, 4) == "icu_" || // icu
|
|
|
|
name == "google" || // protobuf
|
|
|
|
name == "google_breakpad" || // breakpad
|
|
|
|
name == "soundtouch" || // libsoundtouch
|
|
|
|
name == "stagefright" || // libstagefright
|
|
|
|
name == "MacFileUtilities" || // MacFileUtilities
|
|
|
|
name == "dwarf2reader" || // dwarf2reader
|
|
|
|
name == "arm_ex_to_module" || // arm_ex_to_module
|
|
|
|
name == "testing"; // gtest
|
2014-12-13 22:18:54 +03:00
|
|
|
}
|
|
|
|
|
2015-04-11 06:05:46 +03:00
|
|
|
bool isInIgnoredNamespaceForImplicitConversion(const Decl *decl) {
|
|
|
|
std::string name = getDeclarationNamespace(decl);
|
|
|
|
if (name == "") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return name == "std" || // standard C++ lib
|
|
|
|
name == "__gnu_cxx" || // gnu C++ lib
|
|
|
|
name == "google_breakpad" || // breakpad
|
|
|
|
name == "testing"; // gtest
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isIgnoredPathForImplicitCtor(const Decl *decl) {
|
2014-12-13 22:18:54 +03:00
|
|
|
SourceLocation Loc = decl->getLocation();
|
|
|
|
const SourceManager &SM = decl->getASTContext().getSourceManager();
|
|
|
|
SmallString<1024> FileName = SM.getFilename(Loc);
|
|
|
|
llvm::sys::fs::make_absolute(FileName);
|
|
|
|
llvm::sys::path::reverse_iterator begin = llvm::sys::path::rbegin(FileName),
|
|
|
|
end = llvm::sys::path::rend(FileName);
|
|
|
|
for (; begin != end; ++begin) {
|
|
|
|
if (begin->compare_lower(StringRef("skia")) == 0 ||
|
|
|
|
begin->compare_lower(StringRef("angle")) == 0 ||
|
|
|
|
begin->compare_lower(StringRef("harfbuzz")) == 0 ||
|
|
|
|
begin->compare_lower(StringRef("hunspell")) == 0 ||
|
|
|
|
begin->compare_lower(StringRef("scoped_ptr.h")) == 0 ||
|
|
|
|
begin->compare_lower(StringRef("graphite2")) == 0) {
|
|
|
|
return true;
|
2015-07-17 21:57:00 +03:00
|
|
|
}
|
|
|
|
if (begin->compare_lower(StringRef("chromium")) == 0) {
|
|
|
|
// Ignore security/sandbox/chromium but not ipc/chromium.
|
|
|
|
++begin;
|
|
|
|
return begin != end && begin->compare_lower(StringRef("sandbox")) == 0;
|
2014-12-13 22:18:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-11 06:05:46 +03:00
|
|
|
bool isIgnoredPathForImplicitConversion(const Decl *decl) {
|
|
|
|
decl = decl->getCanonicalDecl();
|
|
|
|
SourceLocation Loc = decl->getLocation();
|
|
|
|
const SourceManager &SM = decl->getASTContext().getSourceManager();
|
|
|
|
SmallString<1024> FileName = SM.getFilename(Loc);
|
|
|
|
llvm::sys::fs::make_absolute(FileName);
|
|
|
|
llvm::sys::path::reverse_iterator begin = llvm::sys::path::rbegin(FileName),
|
|
|
|
end = llvm::sys::path::rend(FileName);
|
|
|
|
for (; begin != end; ++begin) {
|
|
|
|
if (begin->compare_lower(StringRef("graphite2")) == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isInterestingDeclForImplicitConversion(const Decl *decl) {
|
|
|
|
return !isInIgnoredNamespaceForImplicitConversion(decl) &&
|
|
|
|
!isIgnoredPathForImplicitConversion(decl);
|
2014-12-13 22:18:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-21 02:43:12 +03:00
|
|
|
class CustomTypeAnnotation {
|
|
|
|
enum ReasonKind {
|
|
|
|
RK_None,
|
|
|
|
RK_Direct,
|
|
|
|
RK_ArrayElement,
|
|
|
|
RK_BaseClass,
|
|
|
|
RK_Field,
|
2015-07-24 19:52:00 +03:00
|
|
|
RK_TemplateInherited,
|
2015-07-21 02:43:12 +03:00
|
|
|
};
|
|
|
|
struct AnnotationReason {
|
|
|
|
QualType Type;
|
|
|
|
ReasonKind Kind;
|
|
|
|
const FieldDecl *Field;
|
|
|
|
|
|
|
|
bool valid() const { return Kind != RK_None; }
|
|
|
|
};
|
|
|
|
typedef DenseMap<void *, AnnotationReason> ReasonCache;
|
|
|
|
|
|
|
|
const char *Spelling;
|
|
|
|
const char *Pretty;
|
|
|
|
ReasonCache Cache;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CustomTypeAnnotation(const char *Spelling, const char *Pretty)
|
|
|
|
: Spelling(Spelling), Pretty(Pretty) {};
|
|
|
|
|
|
|
|
// Checks if this custom annotation "effectively affects" the given type.
|
|
|
|
bool hasEffectiveAnnotation(QualType T) {
|
|
|
|
return directAnnotationReason(T).valid();
|
|
|
|
}
|
|
|
|
void dumpAnnotationReason(DiagnosticsEngine &Diag, QualType T, SourceLocation Loc);
|
|
|
|
|
2015-08-18 21:58:14 +03:00
|
|
|
void reportErrorIfPresent(DiagnosticsEngine &Diag, QualType T, SourceLocation Loc,
|
|
|
|
unsigned ErrorID, unsigned NoteID) {
|
2015-08-12 00:01:24 +03:00
|
|
|
if (hasEffectiveAnnotation(T)) {
|
|
|
|
Diag.Report(Loc, ErrorID) << T;
|
|
|
|
Diag.Report(Loc, NoteID);
|
|
|
|
dumpAnnotationReason(Diag, T, Loc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 02:43:12 +03:00
|
|
|
private:
|
|
|
|
bool hasLiteralAnnotation(QualType T) const;
|
|
|
|
AnnotationReason directAnnotationReason(QualType T);
|
|
|
|
};
|
|
|
|
|
|
|
|
static CustomTypeAnnotation StackClass =
|
|
|
|
CustomTypeAnnotation("moz_stack_class", "stack");
|
|
|
|
static CustomTypeAnnotation GlobalClass =
|
|
|
|
CustomTypeAnnotation("moz_global_class", "global");
|
|
|
|
static CustomTypeAnnotation NonHeapClass =
|
|
|
|
CustomTypeAnnotation("moz_nonheap_class", "non-heap");
|
2015-08-07 00:07:04 +03:00
|
|
|
static CustomTypeAnnotation HeapClass =
|
|
|
|
CustomTypeAnnotation("moz_heap_class", "heap");
|
2015-07-21 02:43:12 +03:00
|
|
|
static CustomTypeAnnotation MustUse =
|
|
|
|
CustomTypeAnnotation("moz_must_use", "must-use");
|
|
|
|
|
2013-03-24 06:12:25 +04:00
|
|
|
class MozChecker : public ASTConsumer, public RecursiveASTVisitor<MozChecker> {
|
|
|
|
DiagnosticsEngine &Diag;
|
|
|
|
const CompilerInstance &CI;
|
2013-04-12 07:20:02 +04:00
|
|
|
DiagnosticsMatcher matcher;
|
2013-03-24 06:12:25 +04:00
|
|
|
public:
|
|
|
|
MozChecker(const CompilerInstance &CI) : Diag(CI.getDiagnostics()), CI(CI) {}
|
2013-04-12 07:20:02 +04:00
|
|
|
|
2014-12-10 18:46:10 +03:00
|
|
|
ASTConsumerPtr getOtherConsumer() {
|
2013-04-12 07:20:02 +04:00
|
|
|
return matcher.makeASTConsumer();
|
|
|
|
}
|
|
|
|
|
2013-03-24 06:12:25 +04:00
|
|
|
virtual void HandleTranslationUnit(ASTContext &ctx) {
|
|
|
|
TraverseDecl(ctx.getTranslationUnitDecl());
|
|
|
|
}
|
2013-03-24 06:13:24 +04:00
|
|
|
|
2015-07-18 07:11:31 +03:00
|
|
|
static bool hasCustomAnnotation(const Decl *D, const char *Spelling) {
|
|
|
|
iterator_range<specific_attr_iterator<AnnotateAttr> > Attrs =
|
|
|
|
D->specific_attrs<AnnotateAttr>();
|
|
|
|
|
|
|
|
for (AnnotateAttr *Attr : Attrs) {
|
|
|
|
if (Attr->getAnnotation() == Spelling) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-03-24 06:13:24 +04:00
|
|
|
|
2015-07-18 07:11:31 +03:00
|
|
|
return false;
|
2013-03-24 06:13:24 +04:00
|
|
|
}
|
|
|
|
|
2015-07-10 16:46:00 +03:00
|
|
|
void HandleUnusedExprResult(const Stmt *stmt) {
|
|
|
|
const Expr* E = dyn_cast_or_null<Expr>(stmt);
|
|
|
|
if (E) {
|
|
|
|
QualType T = E->getType();
|
2015-07-21 02:43:12 +03:00
|
|
|
if (MustUse.hasEffectiveAnnotation(T)) {
|
|
|
|
unsigned errorID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "Unused value of must-use type %0");
|
|
|
|
|
|
|
|
Diag.Report(E->getLocStart(), errorID) << T;
|
|
|
|
MustUse.dumpAnnotationReason(Diag, T, E->getLocStart());
|
2015-07-10 16:46:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-24 06:13:24 +04:00
|
|
|
bool VisitCXXRecordDecl(CXXRecordDecl *d) {
|
|
|
|
// We need definitions, not declarations
|
|
|
|
if (!d->isThisDeclarationADefinition()) return true;
|
|
|
|
|
|
|
|
// Look through all of our immediate bases to find methods that need to be
|
|
|
|
// overridden
|
|
|
|
typedef std::vector<CXXMethodDecl *> OverridesVector;
|
|
|
|
OverridesVector must_overrides;
|
|
|
|
for (CXXRecordDecl::base_class_iterator base = d->bases_begin(),
|
|
|
|
e = d->bases_end(); base != e; ++base) {
|
|
|
|
// The base is either a class (CXXRecordDecl) or it's a templated class...
|
|
|
|
CXXRecordDecl *parent = base->getType()
|
|
|
|
.getDesugaredType(d->getASTContext())->getAsCXXRecordDecl();
|
|
|
|
// The parent might not be resolved to a type yet. In this case, we can't
|
|
|
|
// do any checking here. For complete correctness, we should visit
|
|
|
|
// template instantiations, but this case is likely to be rare, so we will
|
|
|
|
// ignore it until it becomes important.
|
|
|
|
if (!parent) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
parent = parent->getDefinition();
|
|
|
|
for (CXXRecordDecl::method_iterator M = parent->method_begin();
|
|
|
|
M != parent->method_end(); ++M) {
|
|
|
|
if (hasCustomAnnotation(*M, "moz_must_override"))
|
|
|
|
must_overrides.push_back(*M);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (OverridesVector::iterator it = must_overrides.begin();
|
|
|
|
it != must_overrides.end(); ++it) {
|
|
|
|
bool overridden = false;
|
|
|
|
for (CXXRecordDecl::method_iterator M = d->method_begin();
|
|
|
|
!overridden && M != d->method_end(); ++M) {
|
|
|
|
// The way that Clang checks if a method M overrides its parent method
|
|
|
|
// is if the method has the same name but would not overload.
|
|
|
|
if (M->getName() == (*it)->getName() &&
|
2014-12-16 20:54:06 +03:00
|
|
|
!CI.getSema().IsOverload(*M, (*it), false)) {
|
2013-03-24 06:13:24 +04:00
|
|
|
overridden = true;
|
2014-12-16 20:54:06 +03:00
|
|
|
break;
|
|
|
|
}
|
2013-03-24 06:13:24 +04:00
|
|
|
}
|
|
|
|
if (!overridden) {
|
|
|
|
unsigned overrideID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "%0 must override %1");
|
|
|
|
unsigned overrideNote = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "function to override is here");
|
|
|
|
Diag.Report(d->getLocation(), overrideID) << d->getDeclName() <<
|
|
|
|
(*it)->getDeclName();
|
|
|
|
Diag.Report((*it)->getLocation(), overrideNote);
|
|
|
|
}
|
|
|
|
}
|
2014-12-13 22:18:54 +03:00
|
|
|
|
2013-03-24 06:13:24 +04:00
|
|
|
return true;
|
|
|
|
}
|
2015-07-10 16:46:00 +03:00
|
|
|
|
|
|
|
bool VisitSwitchCase(SwitchCase* stmt) {
|
|
|
|
HandleUnusedExprResult(stmt->getSubStmt());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool VisitCompoundStmt(CompoundStmt* stmt) {
|
|
|
|
for (CompoundStmt::body_iterator it = stmt->body_begin(), e = stmt->body_end();
|
|
|
|
it != e; ++it) {
|
|
|
|
HandleUnusedExprResult(*it);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool VisitIfStmt(IfStmt* Stmt) {
|
|
|
|
HandleUnusedExprResult(Stmt->getThen());
|
|
|
|
HandleUnusedExprResult(Stmt->getElse());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool VisitWhileStmt(WhileStmt* Stmt) {
|
|
|
|
HandleUnusedExprResult(Stmt->getBody());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool VisitDoStmt(DoStmt* Stmt) {
|
|
|
|
HandleUnusedExprResult(Stmt->getBody());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool VisitForStmt(ForStmt* Stmt) {
|
|
|
|
HandleUnusedExprResult(Stmt->getBody());
|
|
|
|
HandleUnusedExprResult(Stmt->getInit());
|
|
|
|
HandleUnusedExprResult(Stmt->getInc());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool VisitBinComma(BinaryOperator* Op) {
|
|
|
|
HandleUnusedExprResult(Op->getLHS());
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-24 06:12:25 +04:00
|
|
|
};
|
2013-05-28 01:04:18 +04:00
|
|
|
|
2015-04-11 03:28:34 +03:00
|
|
|
/// A cached data of whether classes are refcounted or not.
|
|
|
|
typedef DenseMap<const CXXRecordDecl *,
|
|
|
|
std::pair<const Decl *, bool> > RefCountedMap;
|
|
|
|
RefCountedMap refCountedClasses;
|
|
|
|
|
|
|
|
bool classHasAddRefRelease(const CXXRecordDecl *D) {
|
|
|
|
const RefCountedMap::iterator& it = refCountedClasses.find(D);
|
|
|
|
if (it != refCountedClasses.end()) {
|
|
|
|
return it->second.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool seenAddRef = false;
|
|
|
|
bool seenRelease = false;
|
2015-04-11 03:28:34 +03:00
|
|
|
for (CXXRecordDecl::method_iterator method = D->method_begin();
|
|
|
|
method != D->method_end(); ++method) {
|
2015-05-08 19:29:11 +03:00
|
|
|
const auto &name = method->getName();
|
2015-04-11 03:28:34 +03:00
|
|
|
if (name == "AddRef") {
|
|
|
|
seenAddRef = true;
|
|
|
|
} else if (name == "Release") {
|
|
|
|
seenRelease = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
refCountedClasses[D] = std::make_pair(D, seenAddRef && seenRelease);
|
|
|
|
return seenAddRef && seenRelease;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isClassRefCounted(QualType T);
|
|
|
|
|
|
|
|
bool isClassRefCounted(const CXXRecordDecl *D) {
|
|
|
|
// Normalize so that D points to the definition if it exists.
|
|
|
|
if (!D->hasDefinition())
|
|
|
|
return false;
|
|
|
|
D = D->getDefinition();
|
|
|
|
// Base class: anyone with AddRef/Release is obviously a refcounted class.
|
|
|
|
if (classHasAddRefRelease(D))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Look through all base cases to figure out if the parent is a refcounted class.
|
2015-04-11 03:28:34 +03:00
|
|
|
for (CXXRecordDecl::base_class_const_iterator base = D->bases_begin();
|
|
|
|
base != D->bases_end(); ++base) {
|
|
|
|
bool super = isClassRefCounted(base->getType());
|
2015-04-11 03:28:34 +03:00
|
|
|
if (super) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isClassRefCounted(QualType T) {
|
|
|
|
while (const ArrayType *arrTy = T->getAsArrayTypeUnsafe())
|
|
|
|
T = arrTy->getElementType();
|
|
|
|
CXXRecordDecl *clazz = T->getAsCXXRecordDecl();
|
2015-07-21 02:43:12 +03:00
|
|
|
return clazz ? isClassRefCounted(clazz) : false;
|
2015-04-11 03:28:34 +03:00
|
|
|
}
|
|
|
|
|
2015-06-19 00:34:27 +03:00
|
|
|
/// A cached data of whether classes are memmovable, and if not, what declaration
|
|
|
|
/// makes them non-movable
|
|
|
|
typedef DenseMap<const CXXRecordDecl *, const CXXRecordDecl *> InferredMovability;
|
|
|
|
InferredMovability inferredMovability;
|
|
|
|
|
|
|
|
bool isClassNonMemMovable(QualType T);
|
|
|
|
const CXXRecordDecl* isClassNonMemMovableWorker(QualType T);
|
|
|
|
|
|
|
|
const CXXRecordDecl* isClassNonMemMovableWorker(const CXXRecordDecl *D) {
|
|
|
|
// If we have a definition, then we want to standardize our reference to point
|
|
|
|
// to the definition node. If we don't have a definition, that means that either
|
|
|
|
// we only have a forward declaration of the type in our file, or we are being
|
|
|
|
// passed a template argument which is not used, and thus never instantiated by
|
|
|
|
// clang.
|
|
|
|
// As the argument isn't used, we can't memmove it (as we don't know it's size),
|
|
|
|
// which means not reporting an error is OK.
|
|
|
|
if (!D->hasDefinition()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
D = D->getDefinition();
|
|
|
|
|
|
|
|
// Are we explicitly marked as non-memmovable class?
|
|
|
|
if (MozChecker::hasCustomAnnotation(D, "moz_non_memmovable")) {
|
|
|
|
return D;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look through all base cases to figure out if the parent is a non-memmovable class.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator base = D->bases_begin();
|
|
|
|
base != D->bases_end(); ++base) {
|
|
|
|
const CXXRecordDecl *result = isClassNonMemMovableWorker(base->getType());
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look through all members to figure out if a member is a non-memmovable class.
|
|
|
|
for (RecordDecl::field_iterator field = D->field_begin(), e = D->field_end();
|
|
|
|
field != e; ++field) {
|
|
|
|
const CXXRecordDecl *result = isClassNonMemMovableWorker(field->getType());
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXRecordDecl* isClassNonMemMovableWorker(QualType T) {
|
|
|
|
while (const ArrayType *arrTy = T->getAsArrayTypeUnsafe())
|
|
|
|
T = arrTy->getElementType();
|
|
|
|
const CXXRecordDecl *clazz = T->getAsCXXRecordDecl();
|
|
|
|
return clazz ? isClassNonMemMovableWorker(clazz) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isClassNonMemMovable(const CXXRecordDecl *D) {
|
|
|
|
InferredMovability::iterator it =
|
|
|
|
inferredMovability.find(D);
|
|
|
|
if (it != inferredMovability.end())
|
|
|
|
return !!it->second;
|
|
|
|
const CXXRecordDecl *result = isClassNonMemMovableWorker(D);
|
|
|
|
inferredMovability.insert(std::make_pair(D, result));
|
|
|
|
return !!result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isClassNonMemMovable(QualType T) {
|
|
|
|
while (const ArrayType *arrTy = T->getAsArrayTypeUnsafe())
|
|
|
|
T = arrTy->getElementType();
|
|
|
|
const CXXRecordDecl *clazz = T->getAsCXXRecordDecl();
|
|
|
|
return clazz ? isClassNonMemMovable(clazz) : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXRecordDecl* findWhyClassIsNonMemMovable(QualType T) {
|
|
|
|
while (const ArrayType *arrTy = T->getAsArrayTypeUnsafe())
|
|
|
|
T = arrTy->getElementType();
|
|
|
|
CXXRecordDecl *clazz = T->getAsCXXRecordDecl();
|
|
|
|
InferredMovability::iterator it =
|
|
|
|
inferredMovability.find(clazz);
|
|
|
|
assert(it != inferredMovability.end());
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2015-04-11 06:05:46 +03:00
|
|
|
template<class T>
|
|
|
|
bool IsInSystemHeader(const ASTContext &AC, const T &D) {
|
|
|
|
auto &SourceManager = AC.getSourceManager();
|
|
|
|
auto ExpansionLoc = SourceManager.getExpansionLoc(D.getLocStart());
|
|
|
|
if (ExpansionLoc.isInvalid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return SourceManager.isInSystemHeader(ExpansionLoc);
|
|
|
|
}
|
|
|
|
|
2015-07-30 05:01:58 +03:00
|
|
|
const FieldDecl *getClassRefCntMember(const CXXRecordDecl *D) {
|
|
|
|
for (RecordDecl::field_iterator field = D->field_begin(), e = D->field_end();
|
|
|
|
field != e; ++field) {
|
|
|
|
if (field->getName() == "mRefCnt") {
|
|
|
|
return *field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FieldDecl *getClassRefCntMember(QualType T) {
|
|
|
|
while (const ArrayType *arrTy = T->getAsArrayTypeUnsafe())
|
|
|
|
T = arrTy->getElementType();
|
|
|
|
CXXRecordDecl *clazz = T->getAsCXXRecordDecl();
|
|
|
|
return clazz ? getClassRefCntMember(clazz) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FieldDecl *getBaseRefCntMember(QualType T);
|
|
|
|
|
|
|
|
const FieldDecl *getBaseRefCntMember(const CXXRecordDecl *D) {
|
|
|
|
const FieldDecl *refCntMember = getClassRefCntMember(D);
|
|
|
|
if (refCntMember && isClassRefCounted(D)) {
|
|
|
|
return refCntMember;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator base = D->bases_begin(), e = D->bases_end();
|
|
|
|
base != e; ++base) {
|
|
|
|
refCntMember = getBaseRefCntMember(base->getType());
|
|
|
|
if (refCntMember) {
|
|
|
|
return refCntMember;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FieldDecl *getBaseRefCntMember(QualType T) {
|
|
|
|
while (const ArrayType *arrTy = T->getAsArrayTypeUnsafe())
|
|
|
|
T = arrTy->getElementType();
|
|
|
|
CXXRecordDecl *clazz = T->getAsCXXRecordDecl();
|
|
|
|
return clazz ? getBaseRefCntMember(clazz) : 0;
|
|
|
|
}
|
|
|
|
|
2015-06-20 02:30:51 +03:00
|
|
|
bool typeHasVTable(QualType T) {
|
|
|
|
while (const ArrayType *arrTy = T->getAsArrayTypeUnsafe())
|
|
|
|
T = arrTy->getElementType();
|
|
|
|
CXXRecordDecl* offender = T->getAsCXXRecordDecl();
|
|
|
|
return offender && offender->hasDefinition() && offender->isDynamicClass();
|
|
|
|
}
|
|
|
|
|
2013-04-12 07:20:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace ast_matchers {
|
|
|
|
|
2013-12-07 07:23:06 +04:00
|
|
|
/// This matcher will match any function declaration that is declared as a heap
|
|
|
|
/// allocator.
|
|
|
|
AST_MATCHER(FunctionDecl, heapAllocator) {
|
|
|
|
return MozChecker::hasCustomAnnotation(&Node, "moz_heap_allocator");
|
|
|
|
}
|
2014-12-18 23:25:15 +03:00
|
|
|
|
|
|
|
/// This matcher will match any declaration that is marked as not accepting
|
|
|
|
/// arithmetic expressions in its arguments.
|
|
|
|
AST_MATCHER(Decl, noArithmeticExprInArgs) {
|
|
|
|
return MozChecker::hasCustomAnnotation(&Node, "moz_no_arith_expr_in_arg");
|
|
|
|
}
|
|
|
|
|
2014-12-23 02:15:42 +03:00
|
|
|
/// This matcher will match any C++ class that is marked as having a trivial
|
|
|
|
/// constructor and destructor.
|
|
|
|
AST_MATCHER(CXXRecordDecl, hasTrivialCtorDtor) {
|
|
|
|
return MozChecker::hasCustomAnnotation(&Node, "moz_trivial_ctor_dtor");
|
|
|
|
}
|
|
|
|
|
2014-12-25 22:44:02 +03:00
|
|
|
/// This matcher will match any function declaration that is marked to prohibit
|
|
|
|
/// calling AddRef or Release on its return value.
|
|
|
|
AST_MATCHER(FunctionDecl, hasNoAddRefReleaseOnReturnAttr) {
|
|
|
|
return MozChecker::hasCustomAnnotation(&Node, "moz_no_addref_release_on_return");
|
|
|
|
}
|
|
|
|
|
2014-12-18 23:25:15 +03:00
|
|
|
/// This matcher will match all arithmetic binary operators.
|
|
|
|
AST_MATCHER(BinaryOperator, binaryArithmeticOperator) {
|
|
|
|
BinaryOperatorKind opcode = Node.getOpcode();
|
|
|
|
return opcode == BO_Mul ||
|
|
|
|
opcode == BO_Div ||
|
|
|
|
opcode == BO_Rem ||
|
|
|
|
opcode == BO_Add ||
|
|
|
|
opcode == BO_Sub ||
|
|
|
|
opcode == BO_Shl ||
|
|
|
|
opcode == BO_Shr ||
|
|
|
|
opcode == BO_And ||
|
|
|
|
opcode == BO_Xor ||
|
|
|
|
opcode == BO_Or ||
|
|
|
|
opcode == BO_MulAssign ||
|
|
|
|
opcode == BO_DivAssign ||
|
|
|
|
opcode == BO_RemAssign ||
|
|
|
|
opcode == BO_AddAssign ||
|
|
|
|
opcode == BO_SubAssign ||
|
|
|
|
opcode == BO_ShlAssign ||
|
|
|
|
opcode == BO_ShrAssign ||
|
|
|
|
opcode == BO_AndAssign ||
|
|
|
|
opcode == BO_XorAssign ||
|
|
|
|
opcode == BO_OrAssign;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This matcher will match all arithmetic unary operators.
|
|
|
|
AST_MATCHER(UnaryOperator, unaryArithmeticOperator) {
|
|
|
|
UnaryOperatorKind opcode = Node.getOpcode();
|
|
|
|
return opcode == UO_PostInc ||
|
|
|
|
opcode == UO_PostDec ||
|
|
|
|
opcode == UO_PreInc ||
|
|
|
|
opcode == UO_PreDec ||
|
|
|
|
opcode == UO_Plus ||
|
|
|
|
opcode == UO_Minus ||
|
|
|
|
opcode == UO_Not;
|
|
|
|
}
|
2014-12-21 20:33:25 +03:00
|
|
|
|
|
|
|
/// This matcher will match == and != binary operators.
|
|
|
|
AST_MATCHER(BinaryOperator, binaryEqualityOperator) {
|
|
|
|
BinaryOperatorKind opcode = Node.getOpcode();
|
|
|
|
return opcode == BO_EQ || opcode == BO_NE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This matcher will match floating point types.
|
|
|
|
AST_MATCHER(QualType, isFloat) {
|
|
|
|
return Node->isRealFloatingType();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This matcher will match locations in system headers. This is adopted from
|
|
|
|
/// isExpansionInSystemHeader in newer clangs, but modified in order to work
|
|
|
|
/// with old clangs that we use on infra.
|
|
|
|
AST_MATCHER(BinaryOperator, isInSystemHeader) {
|
2015-04-11 06:05:46 +03:00
|
|
|
return IsInSystemHeader(Finder->getASTContext(), Node);
|
2014-12-21 20:33:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This matcher will match locations in SkScalar.h. This header contains a
|
|
|
|
/// known NaN-testing expression which we would like to whitelist.
|
|
|
|
AST_MATCHER(BinaryOperator, isInSkScalarDotH) {
|
|
|
|
SourceLocation Loc = Node.getOperatorLoc();
|
|
|
|
auto &SourceManager = Finder->getASTContext().getSourceManager();
|
|
|
|
SmallString<1024> FileName = SourceManager.getFilename(Loc);
|
|
|
|
return llvm::sys::path::rbegin(FileName)->equals("SkScalar.h");
|
|
|
|
}
|
|
|
|
|
2014-12-25 22:44:02 +03:00
|
|
|
/// This matcher will match all accesses to AddRef or Release methods.
|
|
|
|
AST_MATCHER(MemberExpr, isAddRefOrRelease) {
|
|
|
|
ValueDecl *Member = Node.getMemberDecl();
|
|
|
|
CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member);
|
|
|
|
if (Method) {
|
2015-05-08 19:29:11 +03:00
|
|
|
const auto &Name = Method->getName();
|
2014-12-25 22:44:02 +03:00
|
|
|
return Name == "AddRef" || Name == "Release";
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-11 03:28:34 +03:00
|
|
|
/// This matcher will select classes which are refcounted.
|
|
|
|
AST_MATCHER(QualType, isRefCounted) {
|
|
|
|
return isClassRefCounted(Node);
|
|
|
|
}
|
|
|
|
|
2015-06-03 23:51:36 +03:00
|
|
|
#if CLANG_VERSION_FULL < 304
|
|
|
|
|
|
|
|
/// The 'equalsBoundeNode' matcher was added in clang 3.4.
|
|
|
|
/// Since infra runs clang 3.3, we polyfill it here.
|
|
|
|
AST_POLYMORPHIC_MATCHER_P(equalsBoundNode,
|
|
|
|
std::string, ID) {
|
|
|
|
BoundNodesTree bindings = Builder->build();
|
|
|
|
bool haveMatchingResult = false;
|
|
|
|
struct Visitor : public BoundNodesTree::Visitor {
|
|
|
|
const NodeType &Node;
|
|
|
|
std::string ID;
|
|
|
|
bool &haveMatchingResult;
|
|
|
|
Visitor(const NodeType &Node, const std::string &ID, bool &haveMatchingResult)
|
|
|
|
: Node(Node), ID(ID), haveMatchingResult(haveMatchingResult) {}
|
|
|
|
void visitMatch(const BoundNodes &BoundNodesView) override {
|
|
|
|
if (BoundNodesView.getNodeAs<NodeType>(ID) == &Node) {
|
|
|
|
haveMatchingResult = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Visitor visitor(Node, ID, haveMatchingResult);
|
|
|
|
bindings.visitMatches(&visitor);
|
|
|
|
return haveMatchingResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2015-07-30 05:01:58 +03:00
|
|
|
AST_MATCHER(CXXRecordDecl, hasRefCntMember) {
|
|
|
|
return isClassRefCounted(&Node) && getClassRefCntMember(&Node);
|
|
|
|
}
|
|
|
|
|
2015-06-20 02:30:51 +03:00
|
|
|
AST_MATCHER(QualType, hasVTable) {
|
|
|
|
return typeHasVTable(Node);
|
|
|
|
}
|
|
|
|
|
|
|
|
AST_MATCHER(CXXRecordDecl, hasNeedsNoVTableTypeAttr) {
|
|
|
|
return MozChecker::hasCustomAnnotation(&Node, "moz_needs_no_vtable_type");
|
|
|
|
}
|
|
|
|
|
2015-06-19 00:34:27 +03:00
|
|
|
/// This matcher will select classes which are non-memmovable
|
|
|
|
AST_MATCHER(QualType, isNonMemMovable) {
|
|
|
|
return isClassNonMemMovable(Node);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This matcher will select classes which require a memmovable template arg
|
|
|
|
AST_MATCHER(CXXRecordDecl, needsMemMovable) {
|
|
|
|
return MozChecker::hasCustomAnnotation(&Node, "moz_needs_memmovable_type");
|
|
|
|
}
|
|
|
|
|
2015-07-27 16:10:30 +03:00
|
|
|
AST_MATCHER(CXXConstructorDecl, isInterestingImplicitCtor) {
|
|
|
|
const CXXConstructorDecl *decl = Node.getCanonicalDecl();
|
|
|
|
return
|
|
|
|
// Skip ignored namespaces and paths
|
|
|
|
!isInIgnoredNamespaceForImplicitCtor(decl) &&
|
|
|
|
!isIgnoredPathForImplicitCtor(decl) &&
|
|
|
|
// We only want Converting constructors
|
|
|
|
decl->isConvertingConstructor(false) &&
|
|
|
|
// We don't want copy of move constructors, as those are allowed to be implicit
|
|
|
|
!decl->isCopyOrMoveConstructor() &&
|
|
|
|
// We don't want deleted constructors.
|
|
|
|
!decl->isDeleted();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't call this "isImplicit" since it clashes with an existing matcher in clang.
|
|
|
|
AST_MATCHER(CXXConstructorDecl, isMarkedImplicit) {
|
|
|
|
return MozChecker::hasCustomAnnotation(&Node, "moz_implicit");
|
|
|
|
}
|
|
|
|
|
|
|
|
AST_MATCHER(CXXRecordDecl, isConcreteClass) {
|
|
|
|
return !Node.isAbstract();
|
|
|
|
}
|
|
|
|
|
2015-08-12 00:44:36 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-04-12 07:20:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-07-21 02:43:12 +03:00
|
|
|
void CustomTypeAnnotation::dumpAnnotationReason(DiagnosticsEngine &Diag, QualType T, SourceLocation Loc) {
|
|
|
|
unsigned InheritsID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "%1 is a %0 type because it inherits from a %0 type %2");
|
|
|
|
unsigned MemberID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "%1 is a %0 type because member %2 is a %0 type %3");
|
|
|
|
unsigned ArrayID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "%1 is a %0 type because it is an array of %0 type %2");
|
|
|
|
unsigned TemplID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "%1 is a %0 type because it has a template argument %0 type %2");
|
|
|
|
|
|
|
|
AnnotationReason Reason = directAnnotationReason(T);
|
|
|
|
for (;;) {
|
|
|
|
switch (Reason.Kind) {
|
|
|
|
case RK_ArrayElement:
|
|
|
|
Diag.Report(Loc, ArrayID)
|
|
|
|
<< Pretty << T << Reason.Type;
|
|
|
|
break;
|
|
|
|
case RK_BaseClass:
|
|
|
|
{
|
|
|
|
const CXXRecordDecl *Decl = T->getAsCXXRecordDecl();
|
|
|
|
assert(Decl && "This type should be a C++ class");
|
|
|
|
|
|
|
|
Diag.Report(Decl->getLocation(), InheritsID)
|
|
|
|
<< Pretty << T << Reason.Type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RK_Field:
|
|
|
|
Diag.Report(Reason.Field->getLocation(), MemberID)
|
|
|
|
<< Pretty << T << Reason.Field << Reason.Type;
|
|
|
|
break;
|
2015-07-24 19:52:00 +03:00
|
|
|
case RK_TemplateInherited:
|
|
|
|
{
|
|
|
|
const CXXRecordDecl *Decl = T->getAsCXXRecordDecl();
|
|
|
|
assert(Decl && "This type should be a C++ class");
|
|
|
|
|
|
|
|
Diag.Report(Decl->getLocation(), TemplID)
|
|
|
|
<< Pretty << T << Reason.Type;
|
|
|
|
break;
|
|
|
|
}
|
2015-07-21 02:43:12 +03:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
T = Reason.Type;
|
|
|
|
Reason = directAnnotationReason(T);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CustomTypeAnnotation::hasLiteralAnnotation(QualType T) const {
|
2015-07-27 19:29:29 +03:00
|
|
|
#if CLANG_VERSION_FULL >= 306
|
2015-07-21 02:43:12 +03:00
|
|
|
if (const TagDecl *D = T->getAsTagDecl()) {
|
2015-07-27 19:29:29 +03:00
|
|
|
#else
|
|
|
|
if (const CXXRecordDecl *D = T->getAsCXXRecordDecl()) {
|
|
|
|
#endif
|
2015-07-21 02:43:12 +03:00
|
|
|
return MozChecker::hasCustomAnnotation(D, Spelling);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CustomTypeAnnotation::AnnotationReason CustomTypeAnnotation::directAnnotationReason(QualType T) {
|
|
|
|
if (hasLiteralAnnotation(T)) {
|
|
|
|
AnnotationReason Reason = { T, RK_Direct, nullptr };
|
|
|
|
return Reason;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have a cached answer
|
|
|
|
void *Key = T.getAsOpaquePtr();
|
|
|
|
ReasonCache::iterator Cached = Cache.find(T.getAsOpaquePtr());
|
|
|
|
if (Cached != Cache.end()) {
|
|
|
|
return Cached->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have a type which we can recurse into
|
|
|
|
if (const ArrayType *Array = T->getAsArrayTypeUnsafe()) {
|
|
|
|
if (hasEffectiveAnnotation(Array->getElementType())) {
|
|
|
|
AnnotationReason Reason = { Array->getElementType(), RK_ArrayElement, nullptr };
|
|
|
|
Cache[Key] = Reason;
|
|
|
|
return Reason;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recurse into base classes
|
|
|
|
if (const CXXRecordDecl *Decl = T->getAsCXXRecordDecl()) {
|
|
|
|
if (Decl->hasDefinition()) {
|
|
|
|
Decl = Decl->getDefinition();
|
|
|
|
|
|
|
|
for (const CXXBaseSpecifier &Base : Decl->bases()) {
|
|
|
|
if (hasEffectiveAnnotation(Base.getType())) {
|
|
|
|
AnnotationReason Reason = { Base.getType(), RK_BaseClass, nullptr };
|
|
|
|
Cache[Key] = Reason;
|
|
|
|
return Reason;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recurse into members
|
|
|
|
for (const FieldDecl *Field : Decl->fields()) {
|
|
|
|
if (hasEffectiveAnnotation(Field->getType())) {
|
|
|
|
AnnotationReason Reason = { Field->getType(), RK_Field, Field };
|
|
|
|
Cache[Key] = Reason;
|
|
|
|
return Reason;
|
|
|
|
}
|
|
|
|
}
|
2015-07-24 19:52:00 +03:00
|
|
|
|
|
|
|
// Recurse into template arguments if the annotation
|
|
|
|
// MOZ_INHERIT_TYPE_ANNOTATIONS_FROM_TEMPLATE_ARGS is present
|
|
|
|
if (MozChecker::hasCustomAnnotation(
|
|
|
|
Decl, "moz_inherit_type_annotations_from_template_args")) {
|
|
|
|
const ClassTemplateSpecializationDecl *Spec =
|
|
|
|
dyn_cast<ClassTemplateSpecializationDecl>(Decl);
|
|
|
|
if (Spec) {
|
|
|
|
const TemplateArgumentList &Args = Spec->getTemplateArgs();
|
|
|
|
|
|
|
|
for (const TemplateArgument &Arg : Args.asArray()) {
|
|
|
|
if (Arg.getKind() == TemplateArgument::Type) {
|
|
|
|
QualType Type = Arg.getAsType();
|
|
|
|
|
|
|
|
if (hasEffectiveAnnotation(Type)) {
|
|
|
|
AnnotationReason Reason = { Type, RK_TemplateInherited, nullptr };
|
|
|
|
Cache[Key] = Reason;
|
|
|
|
return Reason;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-21 02:43:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AnnotationReason Reason = { QualType(), RK_None, nullptr };
|
|
|
|
Cache[Key] = Reason;
|
|
|
|
return Reason;
|
|
|
|
}
|
|
|
|
|
2015-08-07 00:05:19 +03:00
|
|
|
bool isPlacementNew(const CXXNewExpr *Expr) {
|
2013-12-07 07:23:06 +04:00
|
|
|
// Regular new expressions aren't placement new
|
2015-08-07 00:05:19 +03:00
|
|
|
if (Expr->getNumPlacementArgs() == 0)
|
2013-12-07 07:23:06 +04:00
|
|
|
return false;
|
2015-08-07 00:05:19 +03:00
|
|
|
const FunctionDecl *Decl = Expr->getOperatorNew();
|
|
|
|
if (Decl && MozChecker::hasCustomAnnotation(Decl, "moz_heap_allocator")) {
|
2013-12-07 07:23:06 +04:00
|
|
|
return false;
|
2015-08-07 00:05:19 +03:00
|
|
|
}
|
2013-12-07 07:23:06 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-07 00:05:19 +03:00
|
|
|
DiagnosticsMatcher::DiagnosticsMatcher() {
|
|
|
|
astMatcher.addMatcher(varDecl().bind("node"), &scopeChecker);
|
|
|
|
astMatcher.addMatcher(newExpr().bind("node"), &scopeChecker);
|
|
|
|
astMatcher.addMatcher(materializeTemporaryExpr().bind("node"), &scopeChecker);
|
|
|
|
astMatcher.addMatcher(callExpr(callee(functionDecl(heapAllocator()))).bind("node"),
|
|
|
|
&scopeChecker);
|
2014-12-23 02:10:32 +03:00
|
|
|
|
2014-12-18 23:25:15 +03:00
|
|
|
astMatcher.addMatcher(callExpr(allOf(hasDeclaration(noArithmeticExprInArgs()),
|
|
|
|
anyOf(
|
|
|
|
hasDescendant(binaryOperator(allOf(binaryArithmeticOperator(),
|
|
|
|
hasLHS(hasDescendant(declRefExpr())),
|
|
|
|
hasRHS(hasDescendant(declRefExpr()))
|
|
|
|
)).bind("node")),
|
|
|
|
hasDescendant(unaryOperator(allOf(unaryArithmeticOperator(),
|
|
|
|
hasUnaryOperand(allOf(hasType(builtinType()),
|
|
|
|
anyOf(hasDescendant(declRefExpr()), declRefExpr())))
|
|
|
|
)).bind("node"))
|
|
|
|
)
|
|
|
|
)).bind("call"),
|
|
|
|
&arithmeticArgChecker);
|
|
|
|
astMatcher.addMatcher(constructExpr(allOf(hasDeclaration(noArithmeticExprInArgs()),
|
|
|
|
anyOf(
|
|
|
|
hasDescendant(binaryOperator(allOf(binaryArithmeticOperator(),
|
|
|
|
hasLHS(hasDescendant(declRefExpr())),
|
|
|
|
hasRHS(hasDescendant(declRefExpr()))
|
|
|
|
)).bind("node")),
|
|
|
|
hasDescendant(unaryOperator(allOf(unaryArithmeticOperator(),
|
|
|
|
hasUnaryOperand(allOf(hasType(builtinType()),
|
|
|
|
anyOf(hasDescendant(declRefExpr()), declRefExpr())))
|
|
|
|
)).bind("node"))
|
|
|
|
)
|
|
|
|
)).bind("call"),
|
|
|
|
&arithmeticArgChecker);
|
2014-12-23 02:15:42 +03:00
|
|
|
|
|
|
|
astMatcher.addMatcher(recordDecl(hasTrivialCtorDtor()).bind("node"),
|
|
|
|
&trivialCtorDtorChecker);
|
2014-12-21 20:33:25 +03:00
|
|
|
|
|
|
|
astMatcher.addMatcher(binaryOperator(allOf(binaryEqualityOperator(),
|
|
|
|
hasLHS(has(declRefExpr(hasType(qualType((isFloat())))).bind("lhs"))),
|
|
|
|
hasRHS(has(declRefExpr(hasType(qualType((isFloat())))).bind("rhs"))),
|
|
|
|
unless(anyOf(isInSystemHeader(), isInSkScalarDotH()))
|
|
|
|
)).bind("node"),
|
|
|
|
&nanExprChecker);
|
2014-12-25 22:44:02 +03:00
|
|
|
|
2015-04-19 20:22:35 +03:00
|
|
|
// First, look for direct parents of the MemberExpr.
|
2014-12-25 22:44:02 +03:00
|
|
|
astMatcher.addMatcher(callExpr(callee(functionDecl(hasNoAddRefReleaseOnReturnAttr()).bind("func")),
|
|
|
|
hasParent(memberExpr(isAddRefOrRelease(),
|
|
|
|
hasParent(callExpr())).bind("member")
|
|
|
|
)).bind("node"),
|
|
|
|
&noAddRefReleaseOnReturnChecker);
|
2015-04-19 20:22:35 +03:00
|
|
|
// Then, look for MemberExpr that need to be casted to the right type using
|
|
|
|
// an intermediary CastExpr before we get to the CallExpr.
|
|
|
|
astMatcher.addMatcher(callExpr(callee(functionDecl(hasNoAddRefReleaseOnReturnAttr()).bind("func")),
|
|
|
|
hasParent(castExpr(hasParent(memberExpr(isAddRefOrRelease(),
|
|
|
|
hasParent(callExpr())).bind("member"))))
|
|
|
|
).bind("node"),
|
|
|
|
&noAddRefReleaseOnReturnChecker);
|
2015-04-11 03:28:34 +03:00
|
|
|
|
2015-06-03 23:51:36 +03:00
|
|
|
// Match declrefs with type "pointer to object of ref-counted type" inside a
|
|
|
|
// lambda, where the declaration they reference is not inside the lambda.
|
|
|
|
// This excludes arguments and local variables, leaving only captured
|
|
|
|
// variables.
|
2015-04-11 03:28:34 +03:00
|
|
|
astMatcher.addMatcher(lambdaExpr(
|
2015-06-03 23:51:36 +03:00
|
|
|
hasDescendant(declRefExpr(hasType(pointerType(pointee(isRefCounted()))),
|
|
|
|
to(decl().bind("decl"))).bind("declref")),
|
|
|
|
unless(hasDescendant(decl(equalsBoundNode("decl"))))
|
2015-04-11 03:28:34 +03:00
|
|
|
),
|
|
|
|
&refCountedInsideLambdaChecker);
|
2015-04-11 06:05:46 +03:00
|
|
|
|
|
|
|
// Older clang versions such as the ones used on the infra recognize these
|
|
|
|
// conversions as 'operator _Bool', but newer clang versions recognize these
|
|
|
|
// as 'operator bool'.
|
|
|
|
astMatcher.addMatcher(methodDecl(anyOf(hasName("operator bool"),
|
|
|
|
hasName("operator _Bool"))).bind("node"),
|
|
|
|
&explicitOperatorBoolChecker);
|
2015-06-20 02:30:51 +03:00
|
|
|
|
2015-07-30 05:01:58 +03:00
|
|
|
astMatcher.addMatcher(recordDecl(allOf(decl().bind("decl"),
|
|
|
|
hasRefCntMember())),
|
|
|
|
&noDuplicateRefCntMemberChecker);
|
|
|
|
|
2015-06-20 02:30:51 +03:00
|
|
|
astMatcher.addMatcher(classTemplateSpecializationDecl(
|
|
|
|
allOf(hasAnyTemplateArgument(refersToType(hasVTable())),
|
|
|
|
hasNeedsNoVTableTypeAttr())).bind("node"),
|
|
|
|
&needsNoVTableTypeChecker);
|
2015-06-19 00:34:27 +03:00
|
|
|
|
|
|
|
// Handle non-mem-movable template specializations
|
|
|
|
astMatcher.addMatcher(classTemplateSpecializationDecl(
|
|
|
|
allOf(needsMemMovable(),
|
|
|
|
hasAnyTemplateArgument(refersToType(isNonMemMovable())))
|
|
|
|
).bind("specialization"),
|
|
|
|
&nonMemMovableChecker);
|
2015-07-27 16:10:30 +03:00
|
|
|
|
|
|
|
astMatcher.addMatcher(
|
|
|
|
constructorDecl(isInterestingImplicitCtor(),
|
|
|
|
ofClass(allOf(isConcreteClass(), decl().bind("class"))),
|
|
|
|
unless(isMarkedImplicit())).bind("ctor"),
|
|
|
|
&explicitImplicitChecker);
|
2015-08-12 00:44:36 +03:00
|
|
|
|
|
|
|
astMatcher.addMatcher(varDecl(hasType(autoNonAutoableType())
|
|
|
|
).bind("node"), &noAutoTypeChecker);
|
2013-04-12 07:20:02 +04:00
|
|
|
}
|
|
|
|
|
2015-08-07 00:05:19 +03:00
|
|
|
// These enum variants determine whether an allocation has occured in the code.
|
|
|
|
enum AllocationVariety {
|
|
|
|
AV_None,
|
|
|
|
AV_Global,
|
|
|
|
AV_Automatic,
|
|
|
|
AV_Temporary,
|
|
|
|
AV_Heap,
|
|
|
|
};
|
|
|
|
|
2014-12-23 02:10:32 +03:00
|
|
|
void DiagnosticsMatcher::ScopeChecker::run(
|
2013-04-12 07:20:02 +04:00
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
2015-07-21 02:43:12 +03:00
|
|
|
|
2015-08-07 00:05:19 +03:00
|
|
|
// There are a variety of different reasons why something could be allocated
|
|
|
|
AllocationVariety Variety = AV_None;
|
2015-07-21 02:43:12 +03:00
|
|
|
SourceLocation Loc;
|
|
|
|
QualType T;
|
|
|
|
|
2015-08-07 00:05:19 +03:00
|
|
|
// Determine the type of allocation which we detected
|
|
|
|
if (const VarDecl *D = Result.Nodes.getNodeAs<VarDecl>("node")) {
|
|
|
|
if (D->hasGlobalStorage()) {
|
|
|
|
Variety = AV_Global;
|
|
|
|
} else {
|
|
|
|
Variety = AV_Automatic;
|
|
|
|
}
|
|
|
|
T = D->getType();
|
|
|
|
Loc = D->getLocStart();
|
|
|
|
} else if (const CXXNewExpr *E = Result.Nodes.getNodeAs<CXXNewExpr>("node")) {
|
|
|
|
// New allocates things on the heap.
|
|
|
|
// We don't consider placement new to do anything, as it doesn't actually
|
|
|
|
// allocate the storage, and thus gives us no useful information.
|
|
|
|
if (!isPlacementNew(E)) {
|
|
|
|
Variety = AV_Heap;
|
|
|
|
T = E->getAllocatedType();
|
|
|
|
Loc = E->getLocStart();
|
|
|
|
}
|
|
|
|
} else if (const Expr *E = Result.Nodes.getNodeAs<MaterializeTemporaryExpr>("node")) {
|
|
|
|
Variety = AV_Temporary;
|
|
|
|
T = E->getType().getUnqualifiedType();
|
|
|
|
Loc = E->getLocStart();
|
|
|
|
} else if (const CallExpr *E = Result.Nodes.getNodeAs<CallExpr>("node")) {
|
|
|
|
T = E->getType()->getPointeeType();
|
|
|
|
if (!T.isNull()) {
|
|
|
|
// This will always allocate on the heap, as the heapAllocator() check
|
|
|
|
// was made in the matcher
|
|
|
|
Variety = AV_Heap;
|
|
|
|
Loc = E->getLocStart();
|
|
|
|
}
|
2013-05-28 01:05:02 +04:00
|
|
|
}
|
2014-12-23 02:10:32 +03:00
|
|
|
|
2015-08-07 00:05:19 +03:00
|
|
|
// Error messages for incorrect allocations.
|
|
|
|
unsigned StackID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "variable of type %0 only valid on the stack");
|
|
|
|
unsigned GlobalID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "variable of type %0 only valid as global");
|
|
|
|
unsigned HeapID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "variable of type %0 only valid on the heap");
|
|
|
|
unsigned NonHeapID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "variable of type %0 is not valid on the heap");
|
|
|
|
|
|
|
|
unsigned StackNoteID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "value incorrectly allocated in an automatic variable");
|
|
|
|
unsigned GlobalNoteID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "value incorrectly allocated in a global variable");
|
|
|
|
unsigned HeapNoteID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "value incorrectly allocated on the heap");
|
|
|
|
unsigned TemporaryNoteID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "value incorrectly allocated in a temporary");
|
|
|
|
|
|
|
|
// Report errors depending on the annotations on the input types.
|
|
|
|
switch (Variety) {
|
|
|
|
case AV_None:
|
|
|
|
return;
|
|
|
|
|
|
|
|
case AV_Global:
|
2015-08-18 21:58:14 +03:00
|
|
|
StackClass.reportErrorIfPresent(Diag, T, Loc, StackID, GlobalNoteID);
|
|
|
|
HeapClass.reportErrorIfPresent(Diag, T, Loc, HeapID, GlobalNoteID);
|
2015-08-07 00:05:19 +03:00
|
|
|
break;
|
2013-05-28 01:05:02 +04:00
|
|
|
|
2015-08-07 00:05:19 +03:00
|
|
|
case AV_Automatic:
|
2015-08-18 21:58:14 +03:00
|
|
|
GlobalClass.reportErrorIfPresent(Diag, T, Loc, GlobalID, StackNoteID);
|
|
|
|
HeapClass.reportErrorIfPresent(Diag, T, Loc, HeapID, StackNoteID);
|
2015-08-07 00:05:19 +03:00
|
|
|
break;
|
2015-07-21 02:43:12 +03:00
|
|
|
|
2015-08-07 00:05:19 +03:00
|
|
|
case AV_Temporary:
|
2015-08-18 21:58:14 +03:00
|
|
|
GlobalClass.reportErrorIfPresent(Diag, T, Loc, GlobalID, TemporaryNoteID);
|
|
|
|
HeapClass.reportErrorIfPresent(Diag, T, Loc, HeapID, TemporaryNoteID);
|
2015-08-07 00:05:19 +03:00
|
|
|
break;
|
2013-05-28 01:05:02 +04:00
|
|
|
|
2015-08-07 00:05:19 +03:00
|
|
|
case AV_Heap:
|
2015-08-18 21:58:14 +03:00
|
|
|
GlobalClass.reportErrorIfPresent(Diag, T, Loc, GlobalID, HeapNoteID);
|
|
|
|
StackClass.reportErrorIfPresent(Diag, T, Loc, StackID, HeapNoteID);
|
|
|
|
NonHeapClass.reportErrorIfPresent(Diag, T, Loc, NonHeapID, HeapNoteID);
|
2015-08-07 00:05:19 +03:00
|
|
|
break;
|
|
|
|
}
|
2013-04-12 07:20:02 +04:00
|
|
|
}
|
2013-03-24 06:12:25 +04:00
|
|
|
|
2014-12-18 23:25:15 +03:00
|
|
|
void DiagnosticsMatcher::ArithmeticArgChecker::run(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
|
|
|
unsigned errorID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "cannot pass an arithmetic expression of built-in types to %0");
|
|
|
|
const Expr *expr = Result.Nodes.getNodeAs<Expr>("node");
|
|
|
|
if (const CallExpr *call = Result.Nodes.getNodeAs<CallExpr>("call")) {
|
|
|
|
Diag.Report(expr->getLocStart(), errorID) << call->getDirectCallee();
|
|
|
|
} else if (const CXXConstructExpr *ctr = Result.Nodes.getNodeAs<CXXConstructExpr>("call")) {
|
|
|
|
Diag.Report(expr->getLocStart(), errorID) << ctr->getConstructor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 02:15:42 +03:00
|
|
|
void DiagnosticsMatcher::TrivialCtorDtorChecker::run(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
|
|
|
unsigned errorID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "class %0 must have trivial constructors and destructors");
|
|
|
|
const CXXRecordDecl *node = Result.Nodes.getNodeAs<CXXRecordDecl>("node");
|
|
|
|
|
|
|
|
bool badCtor = !node->hasTrivialDefaultConstructor();
|
|
|
|
bool badDtor = !node->hasTrivialDestructor();
|
|
|
|
if (badCtor || badDtor)
|
|
|
|
Diag.Report(node->getLocStart(), errorID) << node;
|
|
|
|
}
|
|
|
|
|
2014-12-21 20:33:25 +03:00
|
|
|
void DiagnosticsMatcher::NaNExprChecker::run(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
if (!Result.Context->getLangOpts().CPlusPlus) {
|
|
|
|
// mozilla::IsNaN is not usable in C, so there is no point in issuing these warnings.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
|
|
|
unsigned errorID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "comparing a floating point value to itself for NaN checking can lead to incorrect results");
|
|
|
|
unsigned noteID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "consider using mozilla::IsNaN instead");
|
|
|
|
const BinaryOperator *expr = Result.Nodes.getNodeAs<BinaryOperator>("node");
|
|
|
|
const DeclRefExpr *lhs = Result.Nodes.getNodeAs<DeclRefExpr>("lhs");
|
|
|
|
const DeclRefExpr *rhs = Result.Nodes.getNodeAs<DeclRefExpr>("rhs");
|
|
|
|
const ImplicitCastExpr *lhsExpr = dyn_cast<ImplicitCastExpr>(expr->getLHS());
|
|
|
|
const ImplicitCastExpr *rhsExpr = dyn_cast<ImplicitCastExpr>(expr->getRHS());
|
|
|
|
// The AST subtree that we are looking for will look like this:
|
|
|
|
// -BinaryOperator ==/!=
|
|
|
|
// |-ImplicitCastExpr LValueToRValue
|
|
|
|
// | |-DeclRefExpr
|
|
|
|
// |-ImplicitCastExpr LValueToRValue
|
|
|
|
// |-DeclRefExpr
|
|
|
|
// The check below ensures that we are dealing with the correct AST subtree shape, and
|
|
|
|
// also that both of the found DeclRefExpr's point to the same declaration.
|
|
|
|
if (lhs->getFoundDecl() == rhs->getFoundDecl() &&
|
|
|
|
lhsExpr && rhsExpr &&
|
|
|
|
std::distance(lhsExpr->child_begin(), lhsExpr->child_end()) == 1 &&
|
|
|
|
std::distance(rhsExpr->child_begin(), rhsExpr->child_end()) == 1 &&
|
|
|
|
*lhsExpr->child_begin() == lhs &&
|
|
|
|
*rhsExpr->child_begin() == rhs) {
|
|
|
|
Diag.Report(expr->getLocStart(), errorID);
|
|
|
|
Diag.Report(expr->getLocStart(), noteID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-25 22:44:02 +03:00
|
|
|
void DiagnosticsMatcher::NoAddRefReleaseOnReturnChecker::run(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
|
|
|
unsigned errorID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "%1 cannot be called on the return value of %0");
|
|
|
|
const Stmt *node = Result.Nodes.getNodeAs<Stmt>("node");
|
|
|
|
const FunctionDecl *func = Result.Nodes.getNodeAs<FunctionDecl>("func");
|
|
|
|
const MemberExpr *member = Result.Nodes.getNodeAs<MemberExpr>("member");
|
|
|
|
const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(member->getMemberDecl());
|
|
|
|
|
|
|
|
Diag.Report(node->getLocStart(), errorID) << func << method;
|
|
|
|
}
|
|
|
|
|
2015-04-11 03:28:34 +03:00
|
|
|
void DiagnosticsMatcher::RefCountedInsideLambdaChecker::run(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
|
|
|
unsigned errorID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
2015-06-03 23:51:36 +03:00
|
|
|
DiagnosticIDs::Error, "Refcounted variable %0 of type %1 cannot be captured by a lambda");
|
2015-04-11 03:28:34 +03:00
|
|
|
unsigned noteID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "Please consider using a smart pointer");
|
2015-06-03 23:51:36 +03:00
|
|
|
const DeclRefExpr *declref = Result.Nodes.getNodeAs<DeclRefExpr>("declref");
|
2015-04-11 03:28:34 +03:00
|
|
|
|
2015-06-03 23:51:36 +03:00
|
|
|
Diag.Report(declref->getLocStart(), errorID) << declref->getFoundDecl() <<
|
|
|
|
declref->getType()->getPointeeType();
|
|
|
|
Diag.Report(declref->getLocStart(), noteID);
|
2015-04-11 03:28:34 +03:00
|
|
|
}
|
|
|
|
|
2015-04-11 06:05:46 +03:00
|
|
|
void DiagnosticsMatcher::ExplicitOperatorBoolChecker::run(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
|
|
|
unsigned errorID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "bad implicit conversion operator for %0");
|
|
|
|
unsigned noteID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "consider adding the explicit keyword to %0");
|
|
|
|
const CXXConversionDecl *method = Result.Nodes.getNodeAs<CXXConversionDecl>("node");
|
|
|
|
const CXXRecordDecl *clazz = method->getParent();
|
|
|
|
|
|
|
|
if (!method->isExplicitSpecified() &&
|
|
|
|
!MozChecker::hasCustomAnnotation(method, "moz_implicit") &&
|
|
|
|
!IsInSystemHeader(method->getASTContext(), *method) &&
|
|
|
|
isInterestingDeclForImplicitConversion(method)) {
|
|
|
|
Diag.Report(method->getLocStart(), errorID) << clazz;
|
|
|
|
Diag.Report(method->getLocStart(), noteID) << "'operator bool'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 05:01:58 +03:00
|
|
|
void DiagnosticsMatcher::NoDuplicateRefCntMemberChecker::run(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
|
|
|
unsigned warningID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "Refcounted record %0 has multiple mRefCnt members");
|
|
|
|
unsigned note1ID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "Superclass %0 also has an mRefCnt member");
|
|
|
|
unsigned note2ID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "Consider using the _INHERITED macros for AddRef and Release here");
|
|
|
|
|
|
|
|
const CXXRecordDecl *decl = Result.Nodes.getNodeAs<CXXRecordDecl>("decl");
|
|
|
|
const FieldDecl *refCntMember = getClassRefCntMember(decl);
|
|
|
|
assert(refCntMember && "The matcher checked to make sure we have a refCntMember");
|
|
|
|
|
|
|
|
// Check every superclass for whether it has a base with a refcnt member, and warn for those which do
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator base = decl->bases_begin(), e = decl->bases_end();
|
|
|
|
base != e; ++base) {
|
|
|
|
const FieldDecl *baseRefCntMember = getBaseRefCntMember(base->getType());
|
|
|
|
if (baseRefCntMember) {
|
|
|
|
Diag.Report(decl->getLocStart(), warningID) << decl;
|
|
|
|
Diag.Report(baseRefCntMember->getLocStart(), note1ID) << baseRefCntMember->getParent();
|
|
|
|
Diag.Report(refCntMember->getLocStart(), note2ID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-20 02:30:51 +03:00
|
|
|
void DiagnosticsMatcher::NeedsNoVTableTypeChecker::run(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
|
|
|
unsigned errorID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "%0 cannot be instantiated because %1 has a VTable");
|
|
|
|
unsigned noteID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "bad instantiation of %0 requested here");
|
|
|
|
|
|
|
|
const ClassTemplateSpecializationDecl *specialization =
|
|
|
|
Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>("node");
|
|
|
|
|
|
|
|
// Get the offending template argument
|
|
|
|
QualType offender;
|
|
|
|
const TemplateArgumentList &args =
|
|
|
|
specialization->getTemplateInstantiationArgs();
|
|
|
|
for (unsigned i = 0; i < args.size(); ++i) {
|
|
|
|
offender = args[i].getAsType();
|
|
|
|
if (typeHasVTable(offender)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Diag.Report(specialization->getLocStart(), errorID) << specialization << offender;
|
|
|
|
Diag.Report(specialization->getPointOfInstantiation(), noteID) << specialization;
|
|
|
|
}
|
|
|
|
|
2015-06-19 00:34:27 +03:00
|
|
|
void DiagnosticsMatcher::NonMemMovableChecker::run(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
|
|
|
unsigned errorID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "Cannot instantiate %0 with non-memmovable template argument %1");
|
|
|
|
unsigned note1ID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "instantiation of %0 requested here");
|
|
|
|
unsigned note2ID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "%0 is non-memmovable because of the MOZ_NON_MEMMOVABLE annotation on %1");
|
|
|
|
unsigned note3ID = Diag.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Note, "%0");
|
|
|
|
|
|
|
|
// Get the specialization
|
|
|
|
const ClassTemplateSpecializationDecl *specialization =
|
|
|
|
Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>("specialization");
|
|
|
|
SourceLocation requestLoc = specialization->getPointOfInstantiation();
|
|
|
|
const CXXRecordDecl *templ =
|
|
|
|
specialization->getSpecializedTemplate()->getTemplatedDecl();
|
|
|
|
|
|
|
|
// Report an error for every template argument which is non-memmovable
|
|
|
|
const TemplateArgumentList &args =
|
|
|
|
specialization->getTemplateInstantiationArgs();
|
|
|
|
for (unsigned i = 0; i < args.size(); ++i) {
|
|
|
|
QualType argType = args[i].getAsType();
|
|
|
|
if (isClassNonMemMovable(args[i].getAsType())) {
|
|
|
|
const CXXRecordDecl *reason = findWhyClassIsNonMemMovable(argType);
|
|
|
|
Diag.Report(specialization->getLocation(), errorID)
|
|
|
|
<< specialization << argType;
|
|
|
|
// XXX It would be really nice if we could get the instantiation stack information
|
|
|
|
// from Sema such that we could print a full template instantiation stack, however,
|
|
|
|
// it seems as though that information is thrown out by the time we get here so we
|
|
|
|
// can only report one level of template specialization (which in many cases won't
|
|
|
|
// be useful)
|
|
|
|
Diag.Report(requestLoc, note1ID)
|
|
|
|
<< specialization;
|
|
|
|
Diag.Report(reason->getLocation(), note2ID)
|
|
|
|
<< argType << reason;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-27 16:10:30 +03:00
|
|
|
void DiagnosticsMatcher::ExplicitImplicitChecker::run(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
|
|
|
unsigned ErrorID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "bad implicit conversion constructor for %0");
|
|
|
|
unsigned NoteID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "consider adding the explicit keyword to the constructor");
|
|
|
|
|
|
|
|
// We've already checked everything in the matcher, so we just have to report the error.
|
|
|
|
|
|
|
|
const CXXConstructorDecl *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
|
|
|
|
const CXXRecordDecl *Decl = Result.Nodes.getNodeAs<CXXRecordDecl>("class");
|
|
|
|
|
|
|
|
Diag.Report(Ctor->getLocation(), ErrorID) << Decl->getDeclName();
|
|
|
|
Diag.Report(Ctor->getLocation(), NoteID);
|
|
|
|
}
|
|
|
|
|
2015-08-12 00:44:36 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-03-24 06:12:25 +04:00
|
|
|
class MozCheckAction : public PluginASTAction {
|
|
|
|
public:
|
2014-12-10 18:46:10 +03:00
|
|
|
ASTConsumerPtr CreateASTConsumer(CompilerInstance &CI, StringRef fileName) override {
|
|
|
|
#if CLANG_VERSION_FULL >= 306
|
2015-07-06 04:05:53 +03:00
|
|
|
std::unique_ptr<MozChecker> checker(llvm::make_unique<MozChecker>(CI));
|
|
|
|
ASTConsumerPtr other(checker->getOtherConsumer());
|
2014-12-10 18:46:10 +03:00
|
|
|
|
2015-07-06 04:05:53 +03:00
|
|
|
std::vector<ASTConsumerPtr> consumers;
|
2014-12-10 18:46:10 +03:00
|
|
|
consumers.push_back(std::move(checker));
|
2015-07-06 04:05:53 +03:00
|
|
|
consumers.push_back(std::move(other));
|
|
|
|
return llvm::make_unique<MultiplexConsumer>(std::move(consumers));
|
2014-12-10 18:46:10 +03:00
|
|
|
#else
|
2013-04-12 07:20:02 +04:00
|
|
|
MozChecker *checker = new MozChecker(CI);
|
|
|
|
|
|
|
|
ASTConsumer *consumers[] = { checker, checker->getOtherConsumer() };
|
|
|
|
return new MultiplexConsumer(consumers);
|
2014-12-10 18:46:10 +03:00
|
|
|
#endif
|
2013-03-24 06:12:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ParseArgs(const CompilerInstance &CI,
|
2014-12-10 18:46:10 +03:00
|
|
|
const std::vector<std::string> &args) override {
|
2013-03-24 06:12:25 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static FrontendPluginRegistry::Add<MozCheckAction>
|
|
|
|
X("moz-check", "check moz action");
|