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"
|
2016-02-03 17:46:46 +03:00
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
2016-02-05 18:29:26 +03:00
|
|
|
#include "clang/ASTMatchers/ASTMatchers.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
|
|
|
|
|
2016-02-03 21:33:33 +03:00
|
|
|
#ifndef HAVE_NEW_ASTMATCHER_NAMES
|
2016-02-02 22:15:37 +03:00
|
|
|
// In clang 3.8, a number of AST matchers were renamed to better match the
|
|
|
|
// respective AST node. We use the new names, and #define them to the old
|
|
|
|
// ones for compatibility with older versions.
|
|
|
|
#define cxxConstructExpr constructExpr
|
|
|
|
#define cxxConstructorDecl constructorDecl
|
|
|
|
#define cxxMethodDecl methodDecl
|
|
|
|
#define cxxNewExpr newExpr
|
|
|
|
#define cxxRecordDecl recordDecl
|
|
|
|
#endif
|
|
|
|
|
2016-07-19 09:59:22 +03:00
|
|
|
// Check if the given expression contains an assignment expression.
|
|
|
|
// This can either take the form of a Binary Operator or a
|
|
|
|
// Overloaded Operator Call.
|
|
|
|
bool HasSideEffectAssignment(const Expr *expr) {
|
|
|
|
if (auto opCallExpr = dyn_cast_or_null<CXXOperatorCallExpr>(expr)) {
|
|
|
|
auto binOp = opCallExpr->getOperator();
|
|
|
|
if (binOp == OO_Equal || (binOp >= OO_PlusEqual && binOp <= OO_PipeEqual)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (auto binOpExpr = dyn_cast_or_null<BinaryOperator>(expr)) {
|
|
|
|
if (binOpExpr->isAssignmentOp()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recurse to children.
|
|
|
|
for (const Stmt *SubStmt : expr->children()) {
|
|
|
|
auto childExpr = dyn_cast_or_null<Expr>(SubStmt);
|
|
|
|
if (childExpr && HasSideEffectAssignment(childExpr)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-24 06:12:25 +04:00
|
|
|
namespace {
|
2013-04-12 07:20:02 +04:00
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
class DiagnosticsMatcher {
|
|
|
|
public:
|
|
|
|
DiagnosticsMatcher();
|
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
ASTConsumerPtr makeASTConsumer() { return astMatcher.newASTConsumer(); }
|
2013-04-12 07:20:02 +04:00
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2016-04-15 05:15:20 +03:00
|
|
|
class NonMemMovableTemplateArgChecker : public MatchFinder::MatchCallback {
|
2015-06-19 00:34:27 +03:00
|
|
|
public:
|
|
|
|
virtual void run(const MatchFinder::MatchResult &Result);
|
|
|
|
};
|
|
|
|
|
2016-04-15 06:01:55 +03:00
|
|
|
class NonMemMovableMemberChecker : 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-09-03 17:31:57 +03:00
|
|
|
class NoExplicitMoveConstructorChecker : public MatchFinder::MatchCallback {
|
|
|
|
public:
|
|
|
|
virtual void run(const MatchFinder::MatchResult &Result);
|
|
|
|
};
|
|
|
|
|
2015-09-29 19:29:23 +03:00
|
|
|
class RefCountedCopyConstructorChecker : public MatchFinder::MatchCallback {
|
|
|
|
public:
|
|
|
|
virtual void run(const MatchFinder::MatchResult &Result);
|
|
|
|
};
|
|
|
|
|
2016-07-19 09:59:22 +03:00
|
|
|
class AssertAssignmentChecker : 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;
|
2016-04-15 05:15:20 +03:00
|
|
|
NonMemMovableTemplateArgChecker nonMemMovableTemplateArgChecker;
|
2016-04-15 06:01:55 +03:00
|
|
|
NonMemMovableMemberChecker nonMemMovableMemberChecker;
|
2015-07-27 16:10:30 +03:00
|
|
|
ExplicitImplicitChecker explicitImplicitChecker;
|
2015-08-12 00:44:36 +03:00
|
|
|
NoAutoTypeChecker noAutoTypeChecker;
|
2015-09-03 17:31:57 +03:00
|
|
|
NoExplicitMoveConstructorChecker noExplicitMoveConstructorChecker;
|
2015-09-29 19:29:23 +03:00
|
|
|
RefCountedCopyConstructorChecker refCountedCopyConstructorChecker;
|
2016-07-19 09:59:22 +03:00
|
|
|
AssertAssignmentChecker assertAttributionChecker;
|
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) {
|
2015-08-19 05:18:01 +03:00
|
|
|
const DeclContext *DC =
|
|
|
|
decl->getDeclContext()->getEnclosingNamespaceContext();
|
2014-12-13 22:18:54 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-08-19 05:18:01 +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
|
2015-11-18 23:03:25 +03:00
|
|
|
name == "rtc" || // upstream webrtc 'base' package
|
2015-06-15 20:31:44 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
return name == "std" || // standard C++ lib
|
|
|
|
name == "__gnu_cxx" || // gnu C++ lib
|
|
|
|
name == "google_breakpad" || // breakpad
|
|
|
|
name == "testing"; // gtest
|
2015-04-11 06:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2015-08-19 05:18:01 +03:00
|
|
|
end = llvm::sys::path::rend(FileName);
|
2014-12-13 22:18:54 +03:00
|
|
|
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),
|
2015-08-19 05:18:01 +03:00
|
|
|
end = llvm::sys::path::rend(FileName);
|
2015-04-11 06:05:46 +03:00
|
|
|
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-10-14 21:14:28 +03:00
|
|
|
|
|
|
|
bool isIgnoredExprForMustUse(const Expr *E) {
|
|
|
|
if (const CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(E)) {
|
|
|
|
switch (OpCall->getOperator()) {
|
|
|
|
case OO_Equal:
|
|
|
|
case OO_PlusEqual:
|
|
|
|
case OO_MinusEqual:
|
|
|
|
case OO_StarEqual:
|
|
|
|
case OO_SlashEqual:
|
|
|
|
case OO_PercentEqual:
|
|
|
|
case OO_CaretEqual:
|
|
|
|
case OO_AmpEqual:
|
|
|
|
case OO_PipeEqual:
|
|
|
|
case OO_LessLessEqual:
|
|
|
|
case OO_GreaterGreaterEqual:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) {
|
|
|
|
return Op->isAssignmentOp();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-02 22:50:37 +03:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
StringRef getNameChecked(const T& D) {
|
|
|
|
return D->getIdentifier() ? D->getName() : "";
|
|
|
|
}
|
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)
|
2015-08-19 05:18:01 +03:00
|
|
|
: Spelling(Spelling), Pretty(Pretty){};
|
2015-07-21 02:43:12 +03:00
|
|
|
|
2015-10-14 19:13:00 +03:00
|
|
|
virtual ~CustomTypeAnnotation() {}
|
|
|
|
|
2015-07-21 02:43:12 +03:00
|
|
|
// Checks if this custom annotation "effectively affects" the given type.
|
|
|
|
bool hasEffectiveAnnotation(QualType T) {
|
|
|
|
return directAnnotationReason(T).valid();
|
|
|
|
}
|
2015-08-19 05:18:01 +03:00
|
|
|
void dumpAnnotationReason(DiagnosticsEngine &Diag, QualType T,
|
|
|
|
SourceLocation Loc);
|
2015-07-21 02:43:12 +03:00
|
|
|
|
2015-08-19 05:18:01 +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);
|
2015-09-29 20:36:28 +03:00
|
|
|
AnnotationReason tmplArgAnnotationReason(ArrayRef<TemplateArgument> Args);
|
2015-10-14 19:13:00 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// Allow subclasses to apply annotations to external code:
|
|
|
|
virtual bool hasFakeAnnotation(const TagDecl *D) const { return false; }
|
2015-07-21 02:43:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static CustomTypeAnnotation StackClass =
|
2015-08-19 05:18:01 +03:00
|
|
|
CustomTypeAnnotation("moz_stack_class", "stack");
|
2015-07-21 02:43:12 +03:00
|
|
|
static CustomTypeAnnotation GlobalClass =
|
2015-08-19 05:18:01 +03:00
|
|
|
CustomTypeAnnotation("moz_global_class", "global");
|
2015-07-21 02:43:12 +03:00
|
|
|
static CustomTypeAnnotation NonHeapClass =
|
2015-08-19 05:18:01 +03:00
|
|
|
CustomTypeAnnotation("moz_nonheap_class", "non-heap");
|
2015-08-07 00:07:04 +03:00
|
|
|
static CustomTypeAnnotation HeapClass =
|
2015-08-19 05:18:01 +03:00
|
|
|
CustomTypeAnnotation("moz_heap_class", "heap");
|
2015-09-03 18:24:03 +03:00
|
|
|
static CustomTypeAnnotation NonTemporaryClass =
|
|
|
|
CustomTypeAnnotation("moz_non_temporary_class", "non-temporary");
|
2015-07-21 02:43:12 +03:00
|
|
|
static CustomTypeAnnotation MustUse =
|
2016-04-27 01:22:10 +03:00
|
|
|
CustomTypeAnnotation("moz_must_use_type", "must-use");
|
2015-10-14 19:13:00 +03:00
|
|
|
|
|
|
|
class MemMoveAnnotation final : public CustomTypeAnnotation {
|
|
|
|
public:
|
|
|
|
MemMoveAnnotation()
|
|
|
|
: CustomTypeAnnotation("moz_non_memmovable", "non-memmove()able") {}
|
|
|
|
|
|
|
|
virtual ~MemMoveAnnotation() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool hasFakeAnnotation(const TagDecl *D) const override {
|
|
|
|
// Annotate everything in ::std, with a few exceptions; see bug
|
|
|
|
// 1201314 for discussion.
|
|
|
|
if (getDeclarationNamespace(D) == "std") {
|
|
|
|
// This doesn't check that it's really ::std::pair and not
|
|
|
|
// ::std::something_else::pair, but should be good enough.
|
2016-06-02 22:50:37 +03:00
|
|
|
StringRef Name = getNameChecked(D);
|
2015-10-14 19:13:00 +03:00
|
|
|
if (Name == "pair" || Name == "atomic" || Name == "__atomic_base") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static MemMoveAnnotation NonMemMovable = MemMoveAnnotation();
|
2015-07-21 02:43:12 +03:00
|
|
|
|
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;
|
2015-08-19 05:18:01 +03:00
|
|
|
|
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
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
ASTConsumerPtr getOtherConsumer() { return matcher.makeASTConsumer(); }
|
2013-04-12 07:20:02 +04:00
|
|
|
|
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) {
|
2015-08-19 05:18:01 +03:00
|
|
|
iterator_range<specific_attr_iterator<AnnotateAttr>> Attrs =
|
|
|
|
D->specific_attrs<AnnotateAttr>();
|
2015-07-18 07:11:31 +03:00
|
|
|
|
|
|
|
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) {
|
2015-08-19 05:18:01 +03:00
|
|
|
const Expr *E = dyn_cast_or_null<Expr>(stmt);
|
2015-07-10 16:46:00 +03:00
|
|
|
if (E) {
|
2016-03-11 20:31:10 +03:00
|
|
|
E = E->IgnoreImplicit(); // Ignore ExprWithCleanup etc. implicit wrappers
|
2015-07-10 16:46:00 +03:00
|
|
|
QualType T = E->getType();
|
2015-10-14 21:14:28 +03:00
|
|
|
if (MustUse.hasEffectiveAnnotation(T) && !isIgnoredExprForMustUse(E)) {
|
2015-07-21 02:43:12 +03:00
|
|
|
unsigned errorID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
2015-08-19 05:18:01 +03:00
|
|
|
DiagnosticIDs::Error, "Unused value of must-use type %0");
|
2015-07-21 02:43:12 +03:00
|
|
|
|
|
|
|
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
|
2015-08-19 05:18:01 +03:00
|
|
|
if (!d->isThisDeclarationADefinition())
|
|
|
|
return true;
|
2013-03-24 06:13:24 +04:00
|
|
|
|
|
|
|
// 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(),
|
2015-08-19 05:18:01 +03:00
|
|
|
e = d->bases_end();
|
|
|
|
base != e; ++base) {
|
2013-03-24 06:13:24 +04:00
|
|
|
// The base is either a class (CXXRecordDecl) or it's a templated class...
|
|
|
|
CXXRecordDecl *parent = base->getType()
|
2015-08-19 05:18:01 +03:00
|
|
|
.getDesugaredType(d->getASTContext())
|
|
|
|
->getAsCXXRecordDecl();
|
2013-03-24 06:13:24 +04:00
|
|
|
// 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();
|
2015-08-19 05:18:01 +03:00
|
|
|
M != parent->method_end(); ++M) {
|
2013-03-24 06:13:24 +04:00
|
|
|
if (hasCustomAnnotation(*M, "moz_must_override"))
|
|
|
|
must_overrides.push_back(*M);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (OverridesVector::iterator it = must_overrides.begin();
|
2015-08-19 05:18:01 +03:00
|
|
|
it != must_overrides.end(); ++it) {
|
2013-03-24 06:13:24 +04:00
|
|
|
bool overridden = false;
|
|
|
|
for (CXXRecordDecl::method_iterator M = d->method_begin();
|
2015-08-19 05:18:01 +03:00
|
|
|
!overridden && M != d->method_end(); ++M) {
|
2013-03-24 06:13:24 +04:00
|
|
|
// 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.
|
2016-06-02 22:50:37 +03:00
|
|
|
if (getNameChecked(M) == getNameChecked(*it) &&
|
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");
|
2015-08-19 05:18:01 +03:00
|
|
|
Diag.Report(d->getLocation(), overrideID) << d->getDeclName()
|
|
|
|
<< (*it)->getDeclName();
|
2013-03-24 06:13:24 +04:00
|
|
|
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
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
bool VisitSwitchCase(SwitchCase *stmt) {
|
2015-07-10 16:46:00 +03:00
|
|
|
HandleUnusedExprResult(stmt->getSubStmt());
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-19 05:18:01 +03:00
|
|
|
bool VisitCompoundStmt(CompoundStmt *stmt) {
|
|
|
|
for (CompoundStmt::body_iterator it = stmt->body_begin(),
|
|
|
|
e = stmt->body_end();
|
2015-07-10 16:46:00 +03:00
|
|
|
it != e; ++it) {
|
|
|
|
HandleUnusedExprResult(*it);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-19 05:18:01 +03:00
|
|
|
bool VisitIfStmt(IfStmt *Stmt) {
|
2015-07-10 16:46:00 +03:00
|
|
|
HandleUnusedExprResult(Stmt->getThen());
|
|
|
|
HandleUnusedExprResult(Stmt->getElse());
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-19 05:18:01 +03:00
|
|
|
bool VisitWhileStmt(WhileStmt *Stmt) {
|
2015-07-10 16:46:00 +03:00
|
|
|
HandleUnusedExprResult(Stmt->getBody());
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-19 05:18:01 +03:00
|
|
|
bool VisitDoStmt(DoStmt *Stmt) {
|
2015-07-10 16:46:00 +03:00
|
|
|
HandleUnusedExprResult(Stmt->getBody());
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-19 05:18:01 +03:00
|
|
|
bool VisitForStmt(ForStmt *Stmt) {
|
2015-07-10 16:46:00 +03:00
|
|
|
HandleUnusedExprResult(Stmt->getBody());
|
|
|
|
HandleUnusedExprResult(Stmt->getInit());
|
|
|
|
HandleUnusedExprResult(Stmt->getInc());
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-19 05:18:01 +03:00
|
|
|
bool VisitBinComma(BinaryOperator *Op) {
|
2015-07-10 16:46:00 +03:00
|
|
|
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.
|
2015-08-19 05:18:01 +03:00
|
|
|
typedef DenseMap<const CXXRecordDecl *, std::pair<const Decl *, bool>>
|
|
|
|
RefCountedMap;
|
2015-04-11 03:28:34 +03:00
|
|
|
RefCountedMap refCountedClasses;
|
|
|
|
|
|
|
|
bool classHasAddRefRelease(const CXXRecordDecl *D) {
|
2015-08-19 05:18:01 +03:00
|
|
|
const RefCountedMap::iterator &it = refCountedClasses.find(D);
|
2015-04-11 03:28:34 +03:00
|
|
|
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) {
|
2016-06-02 22:50:37 +03:00
|
|
|
const auto &name = getNameChecked(method);
|
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;
|
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
// 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) {
|
2016-03-09 06:04:54 +03:00
|
|
|
while (const clang::ArrayType *arrTy = T->getAsArrayTypeUnsafe())
|
2015-04-11 03:28:34 +03:00
|
|
|
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-08-19 05:18:01 +03:00
|
|
|
template <class T> bool IsInSystemHeader(const ASTContext &AC, const T &D) {
|
2015-04-11 06:05:46 +03:00
|
|
|
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) {
|
2016-06-02 22:50:37 +03:00
|
|
|
if (getNameChecked(field) == "mRefCnt") {
|
2015-07-30 05:01:58 +03:00
|
|
|
return *field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FieldDecl *getBaseRefCntMember(QualType T);
|
|
|
|
|
|
|
|
const FieldDecl *getBaseRefCntMember(const CXXRecordDecl *D) {
|
|
|
|
const FieldDecl *refCntMember = getClassRefCntMember(D);
|
|
|
|
if (refCntMember && isClassRefCounted(D)) {
|
|
|
|
return refCntMember;
|
|
|
|
}
|
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
for (CXXRecordDecl::base_class_const_iterator base = D->bases_begin(),
|
|
|
|
e = D->bases_end();
|
2015-07-30 05:01:58 +03:00
|
|
|
base != e; ++base) {
|
|
|
|
refCntMember = getBaseRefCntMember(base->getType());
|
|
|
|
if (refCntMember) {
|
|
|
|
return refCntMember;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FieldDecl *getBaseRefCntMember(QualType T) {
|
2016-03-09 06:04:54 +03:00
|
|
|
while (const clang::ArrayType *arrTy = T->getAsArrayTypeUnsafe())
|
2015-07-30 05:01:58 +03:00
|
|
|
T = arrTy->getElementType();
|
|
|
|
CXXRecordDecl *clazz = T->getAsCXXRecordDecl();
|
|
|
|
return clazz ? getBaseRefCntMember(clazz) : 0;
|
|
|
|
}
|
|
|
|
|
2015-06-20 02:30:51 +03:00
|
|
|
bool typeHasVTable(QualType T) {
|
2016-03-09 06:04:54 +03:00
|
|
|
while (const clang::ArrayType *arrTy = T->getAsArrayTypeUnsafe())
|
2015-06-20 02:30:51 +03:00
|
|
|
T = arrTy->getElementType();
|
2015-08-19 05:18:01 +03:00
|
|
|
CXXRecordDecl *offender = T->getAsCXXRecordDecl();
|
2015-06-20 02:30:51 +03:00
|
|
|
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) {
|
2015-08-19 05:18:01 +03:00
|
|
|
return MozChecker::hasCustomAnnotation(&Node,
|
|
|
|
"moz_no_addref_release_on_return");
|
2014-12-25 22:44:02 +03:00
|
|
|
}
|
|
|
|
|
2014-12-18 23:25:15 +03:00
|
|
|
/// This matcher will match all arithmetic binary operators.
|
|
|
|
AST_MATCHER(BinaryOperator, binaryArithmeticOperator) {
|
|
|
|
BinaryOperatorKind opcode = Node.getOpcode();
|
2015-08-19 05:18:01 +03:00
|
|
|
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;
|
2014-12-18 23:25:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This matcher will match all arithmetic unary operators.
|
|
|
|
AST_MATCHER(UnaryOperator, unaryArithmeticOperator) {
|
|
|
|
UnaryOperatorKind opcode = Node.getOpcode();
|
2015-08-19 05:18:01 +03:00
|
|
|
return opcode == UO_PostInc || opcode == UO_PostDec || opcode == UO_PreInc ||
|
|
|
|
opcode == UO_PreDec || opcode == UO_Plus || opcode == UO_Minus ||
|
2014-12-18 23:25:15 +03:00
|
|
|
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.
|
2015-08-19 05:18:01 +03:00
|
|
|
AST_MATCHER(QualType, isFloat) { return Node->isRealFloatingType(); }
|
2014-12-21 20:33:25 +03:00
|
|
|
|
|
|
|
/// 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) {
|
2016-06-02 22:50:37 +03:00
|
|
|
const auto &Name = getNameChecked(Method);
|
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.
|
2015-07-30 05:01:58 +03:00
|
|
|
AST_MATCHER(CXXRecordDecl, hasRefCntMember) {
|
|
|
|
return isClassRefCounted(&Node) && getClassRefCntMember(&Node);
|
|
|
|
}
|
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
AST_MATCHER(QualType, hasVTable) { return typeHasVTable(Node); }
|
2015-06-20 02:30:51 +03:00
|
|
|
|
|
|
|
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
|
2015-09-10 18:23:53 +03:00
|
|
|
AST_MATCHER(QualType, isNonMemMovable) {
|
|
|
|
return NonMemMovable.hasEffectiveAnnotation(Node);
|
|
|
|
}
|
2015-06-19 00:34:27 +03:00
|
|
|
|
|
|
|
/// This matcher will select classes which require a memmovable template arg
|
2016-04-15 05:15:20 +03:00
|
|
|
AST_MATCHER(CXXRecordDecl, needsMemMovableTemplateArg) {
|
2015-06-19 00:34:27 +03:00
|
|
|
return MozChecker::hasCustomAnnotation(&Node, "moz_needs_memmovable_type");
|
|
|
|
}
|
|
|
|
|
2016-04-15 06:01:55 +03:00
|
|
|
/// This matcher will select classes which require all members to be memmovable
|
|
|
|
AST_MATCHER(CXXRecordDecl, needsMemMovableMembers) {
|
|
|
|
return MozChecker::hasCustomAnnotation(&Node, "moz_needs_memmovable_members");
|
|
|
|
}
|
|
|
|
|
2015-07-27 16:10:30 +03:00
|
|
|
AST_MATCHER(CXXConstructorDecl, isInterestingImplicitCtor) {
|
|
|
|
const CXXConstructorDecl *decl = Node.getCanonicalDecl();
|
|
|
|
return
|
2015-08-19 05:18:01 +03:00
|
|
|
// 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.
|
2015-07-27 16:10:30 +03:00
|
|
|
AST_MATCHER(CXXConstructorDecl, isMarkedImplicit) {
|
|
|
|
return MozChecker::hasCustomAnnotation(&Node, "moz_implicit");
|
|
|
|
}
|
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
AST_MATCHER(CXXRecordDecl, isConcreteClass) { return !Node.isAbstract(); }
|
2015-07-27 16:10:30 +03:00
|
|
|
|
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;
|
|
|
|
}
|
2015-09-03 17:31:57 +03:00
|
|
|
|
|
|
|
AST_MATCHER(CXXConstructorDecl, isExplicitMoveConstructor) {
|
|
|
|
return Node.isExplicit() && Node.isMoveConstructor();
|
|
|
|
}
|
2015-09-29 19:29:23 +03:00
|
|
|
|
|
|
|
AST_MATCHER(CXXConstructorDecl, isCompilerProvidedCopyConstructor) {
|
|
|
|
return !Node.isUserProvided() && Node.isCopyConstructor();
|
|
|
|
}
|
2016-07-19 09:59:22 +03:00
|
|
|
|
|
|
|
AST_MATCHER(CallExpr, isAssertAssignmentTestFunc) {
|
|
|
|
static const std::string assertName = "MOZ_AssertAssignmentTest";
|
|
|
|
const FunctionDecl *method = Node.getDirectCallee();
|
|
|
|
|
|
|
|
return method
|
|
|
|
&& method->getDeclName().isIdentifier()
|
|
|
|
&& method->getName() == assertName;
|
|
|
|
}
|
2016-07-19 00:29:04 +03:00
|
|
|
|
|
|
|
AST_MATCHER(CXXRecordDecl, isLambdaDecl) {
|
|
|
|
return Node.isLambda();
|
|
|
|
}
|
|
|
|
|
2013-04-12 07:20:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
void CustomTypeAnnotation::dumpAnnotationReason(DiagnosticsEngine &Diag,
|
|
|
|
QualType T,
|
|
|
|
SourceLocation Loc) {
|
2015-07-21 02:43:12 +03:00
|
|
|
unsigned InheritsID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
2015-08-19 05:18:01 +03:00
|
|
|
DiagnosticIDs::Note,
|
|
|
|
"%1 is a %0 type because it inherits from a %0 type %2");
|
2015-07-21 02:43:12 +03:00
|
|
|
unsigned MemberID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
2015-08-19 05:18:01 +03:00
|
|
|
DiagnosticIDs::Note, "%1 is a %0 type because member %2 is a %0 type %3");
|
2015-07-21 02:43:12 +03:00
|
|
|
unsigned ArrayID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
2015-08-19 05:18:01 +03:00
|
|
|
DiagnosticIDs::Note,
|
|
|
|
"%1 is a %0 type because it is an array of %0 type %2");
|
2015-07-21 02:43:12 +03:00
|
|
|
unsigned TemplID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
2015-08-19 05:18:01 +03:00
|
|
|
DiagnosticIDs::Note,
|
|
|
|
"%1 is a %0 type because it has a template argument %0 type %2");
|
2015-07-21 02:43:12 +03:00
|
|
|
|
|
|
|
AnnotationReason Reason = directAnnotationReason(T);
|
|
|
|
for (;;) {
|
|
|
|
switch (Reason.Kind) {
|
|
|
|
case RK_ArrayElement:
|
2015-08-19 05:18:01 +03:00
|
|
|
Diag.Report(Loc, ArrayID) << Pretty << T << Reason.Type;
|
2015-07-21 02:43:12 +03:00
|
|
|
break;
|
2015-08-19 05:18:01 +03:00
|
|
|
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;
|
|
|
|
}
|
2015-07-21 02:43:12 +03:00
|
|
|
case RK_Field:
|
|
|
|
Diag.Report(Reason.Field->getLocation(), MemberID)
|
2015-08-19 05:18:01 +03:00
|
|
|
<< Pretty << T << Reason.Field << Reason.Type;
|
2015-07-21 02:43:12 +03:00
|
|
|
break;
|
2015-08-19 05:18:01 +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:
|
2015-09-10 18:23:53 +03:00
|
|
|
// FIXME (bug 1203263): note the original annotation.
|
2015-07-21 02:43:12 +03:00
|
|
|
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-10-14 19:13:00 +03:00
|
|
|
return hasFakeAnnotation(D) || MozChecker::hasCustomAnnotation(D, Spelling);
|
2015-07-21 02:43:12 +03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
CustomTypeAnnotation::AnnotationReason
|
|
|
|
CustomTypeAnnotation::directAnnotationReason(QualType T) {
|
2015-07-21 02:43:12 +03:00
|
|
|
if (hasLiteralAnnotation(T)) {
|
2015-08-19 05:18:01 +03:00
|
|
|
AnnotationReason Reason = {T, RK_Direct, nullptr};
|
2015-07-21 02:43:12 +03:00
|
|
|
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
|
2016-03-09 06:04:54 +03:00
|
|
|
if (const clang::ArrayType *Array = T->getAsArrayTypeUnsafe()) {
|
2015-07-21 02:43:12 +03:00
|
|
|
if (hasEffectiveAnnotation(Array->getElementType())) {
|
2015-08-19 05:18:01 +03:00
|
|
|
AnnotationReason Reason = {Array->getElementType(), RK_ArrayElement,
|
|
|
|
nullptr};
|
2015-07-21 02:43:12 +03:00
|
|
|
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())) {
|
2015-08-19 05:18:01 +03:00
|
|
|
AnnotationReason Reason = {Base.getType(), RK_BaseClass, nullptr};
|
2015-07-21 02:43:12 +03:00
|
|
|
Cache[Key] = Reason;
|
|
|
|
return Reason;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recurse into members
|
|
|
|
for (const FieldDecl *Field : Decl->fields()) {
|
|
|
|
if (hasEffectiveAnnotation(Field->getType())) {
|
2015-08-19 05:18:01 +03:00
|
|
|
AnnotationReason Reason = {Field->getType(), RK_Field, Field};
|
2015-07-21 02:43:12 +03:00
|
|
|
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(
|
2015-08-19 05:18:01 +03:00
|
|
|
Decl, "moz_inherit_type_annotations_from_template_args")) {
|
2015-07-24 19:52:00 +03:00
|
|
|
const ClassTemplateSpecializationDecl *Spec =
|
2015-08-19 05:18:01 +03:00
|
|
|
dyn_cast<ClassTemplateSpecializationDecl>(Decl);
|
2015-07-24 19:52:00 +03:00
|
|
|
if (Spec) {
|
|
|
|
const TemplateArgumentList &Args = Spec->getTemplateArgs();
|
|
|
|
|
2015-09-29 20:36:28 +03:00
|
|
|
AnnotationReason Reason = tmplArgAnnotationReason(Args.asArray());
|
|
|
|
if (Reason.Kind != RK_None) {
|
|
|
|
Cache[Key] = Reason;
|
|
|
|
return Reason;
|
2015-07-24 19:52:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-21 02:43:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
AnnotationReason Reason = {QualType(), RK_None, nullptr};
|
2015-07-21 02:43:12 +03:00
|
|
|
Cache[Key] = Reason;
|
|
|
|
return Reason;
|
|
|
|
}
|
|
|
|
|
2015-09-29 20:36:28 +03:00
|
|
|
CustomTypeAnnotation::AnnotationReason
|
|
|
|
CustomTypeAnnotation::tmplArgAnnotationReason(ArrayRef<TemplateArgument> Args) {
|
|
|
|
for (const TemplateArgument &Arg : Args) {
|
|
|
|
if (Arg.getKind() == TemplateArgument::Type) {
|
|
|
|
QualType Type = Arg.getAsType();
|
|
|
|
if (hasEffectiveAnnotation(Type)) {
|
|
|
|
AnnotationReason Reason = {Type, RK_TemplateInherited, nullptr};
|
|
|
|
return Reason;
|
|
|
|
}
|
|
|
|
} else if (Arg.getKind() == TemplateArgument::Pack) {
|
|
|
|
AnnotationReason Reason = tmplArgAnnotationReason(Arg.getPackAsArray());
|
|
|
|
if (Reason.Kind != RK_None) {
|
|
|
|
return Reason;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AnnotationReason Reason = {QualType(), RK_None, nullptr};
|
|
|
|
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);
|
2016-02-02 22:15:37 +03:00
|
|
|
astMatcher.addMatcher(cxxNewExpr().bind("node"), &scopeChecker);
|
2015-08-07 00:05:19 +03:00
|
|
|
astMatcher.addMatcher(materializeTemporaryExpr().bind("node"), &scopeChecker);
|
2015-08-19 05:18:01 +03:00
|
|
|
astMatcher.addMatcher(
|
|
|
|
callExpr(callee(functionDecl(heapAllocator()))).bind("node"),
|
|
|
|
&scopeChecker);
|
2015-09-03 18:24:03 +03:00
|
|
|
astMatcher.addMatcher(parmVarDecl().bind("parm_vardecl"), &scopeChecker);
|
2015-08-19 05:18:01 +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(
|
2016-02-02 22:15:37 +03:00
|
|
|
cxxConstructExpr(
|
2015-08-19 05:18:01 +03:00
|
|
|
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
|
|
|
|
2016-02-02 22:15:37 +03:00
|
|
|
astMatcher.addMatcher(cxxRecordDecl(hasTrivialCtorDtor()).bind("node"),
|
2015-08-19 05:18:01 +03:00
|
|
|
&trivialCtorDtorChecker);
|
2014-12-21 20:33:25 +03:00
|
|
|
|
2015-08-19 05:18:01 +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.
|
2015-08-19 05:18:01 +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.
|
2015-08-19 05:18:01 +03:00
|
|
|
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
|
|
|
|
2016-07-19 00:29:04 +03:00
|
|
|
// We want to reject any code which captures a pointer to an object of a
|
|
|
|
// refcounted type, and then lets that value escape. As a primitive analysis,
|
|
|
|
// we reject any occurances of the lambda as a template parameter to a class
|
|
|
|
// (which could allow it to escape), as well as any presence of such a lambda
|
|
|
|
// in a return value (either from lambdas, or in c++14, auto functions).
|
|
|
|
//
|
|
|
|
// We check these lambdas' capture lists for raw pointers to refcounted types.
|
|
|
|
astMatcher.addMatcher(
|
|
|
|
functionDecl(returns(recordType(hasDeclaration(cxxRecordDecl(isLambdaDecl()).bind("decl"))))),
|
|
|
|
&refCountedInsideLambdaChecker);
|
|
|
|
astMatcher.addMatcher(lambdaExpr().bind("lambdaExpr"), &refCountedInsideLambdaChecker);
|
|
|
|
astMatcher.addMatcher(
|
|
|
|
classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType(
|
|
|
|
recordType(hasDeclaration(cxxRecordDecl(isLambdaDecl()).bind("decl")))))),
|
|
|
|
&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'.
|
2015-08-19 05:18:01 +03:00
|
|
|
astMatcher.addMatcher(
|
2016-02-02 22:15:37 +03:00
|
|
|
cxxMethodDecl(anyOf(hasName("operator bool"), hasName("operator _Bool")))
|
2015-08-19 05:18:01 +03:00
|
|
|
.bind("node"),
|
|
|
|
&explicitOperatorBoolChecker);
|
2015-06-20 02:30:51 +03:00
|
|
|
|
2016-08-04 19:54:25 +03:00
|
|
|
astMatcher.addMatcher(cxxRecordDecl().bind("decl"), &noDuplicateRefCntMemberChecker);
|
2015-07-30 05:01:58 +03:00
|
|
|
|
2015-08-19 05:18:01 +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
|
2015-08-19 05:18:01 +03:00
|
|
|
astMatcher.addMatcher(
|
|
|
|
classTemplateSpecializationDecl(
|
2016-04-15 05:15:20 +03:00
|
|
|
allOf(needsMemMovableTemplateArg(),
|
2015-08-19 05:18:01 +03:00
|
|
|
hasAnyTemplateArgument(refersToType(isNonMemMovable()))))
|
|
|
|
.bind("specialization"),
|
2016-04-15 05:15:20 +03:00
|
|
|
&nonMemMovableTemplateArgChecker);
|
2015-07-27 16:10:30 +03:00
|
|
|
|
2016-04-15 06:01:55 +03:00
|
|
|
// Handle non-mem-movable members
|
|
|
|
astMatcher.addMatcher(
|
|
|
|
cxxRecordDecl(needsMemMovableMembers())
|
|
|
|
.bind("decl"),
|
|
|
|
&nonMemMovableMemberChecker);
|
|
|
|
|
2016-02-05 18:29:26 +03:00
|
|
|
astMatcher.addMatcher(cxxConstructorDecl(isInterestingImplicitCtor(),
|
|
|
|
ofClass(allOf(isConcreteClass(),
|
|
|
|
decl().bind("class"))),
|
|
|
|
unless(isMarkedImplicit()))
|
|
|
|
.bind("ctor"),
|
|
|
|
&explicitImplicitChecker);
|
2015-08-12 00:44:36 +03:00
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
astMatcher.addMatcher(varDecl(hasType(autoNonAutoableType())).bind("node"),
|
|
|
|
&noAutoTypeChecker);
|
2015-09-03 17:31:57 +03:00
|
|
|
|
2016-02-05 18:29:26 +03:00
|
|
|
astMatcher.addMatcher(
|
|
|
|
cxxConstructorDecl(isExplicitMoveConstructor()).bind("node"),
|
|
|
|
&noExplicitMoveConstructorChecker);
|
2015-09-29 19:29:23 +03:00
|
|
|
|
2016-02-05 18:29:26 +03:00
|
|
|
astMatcher.addMatcher(
|
|
|
|
cxxConstructExpr(
|
|
|
|
hasDeclaration(cxxConstructorDecl(isCompilerProvidedCopyConstructor(),
|
|
|
|
ofClass(hasRefCntMember()))))
|
|
|
|
.bind("node"),
|
|
|
|
&refCountedCopyConstructorChecker);
|
2016-07-19 09:59:22 +03:00
|
|
|
|
|
|
|
astMatcher.addMatcher(
|
|
|
|
callExpr(isAssertAssignmentTestFunc()).bind("funcCall"),
|
|
|
|
&assertAttributionChecker);
|
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,
|
|
|
|
};
|
|
|
|
|
2015-09-03 18:24:03 +03:00
|
|
|
// XXX Currently the Decl* in the AutomaticTemporaryMap is unused, but it
|
|
|
|
// probably will be used at some point in the future, in order to produce better
|
|
|
|
// error messages.
|
2016-02-05 18:29:26 +03:00
|
|
|
typedef DenseMap<const MaterializeTemporaryExpr *, const Decl *>
|
|
|
|
AutomaticTemporaryMap;
|
2015-09-03 18:24:03 +03:00
|
|
|
AutomaticTemporaryMap AutomaticTemporaries;
|
|
|
|
|
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;
|
|
|
|
|
2016-02-05 18:29:26 +03:00
|
|
|
if (const ParmVarDecl *D =
|
|
|
|
Result.Nodes.getNodeAs<ParmVarDecl>("parm_vardecl")) {
|
2016-06-02 22:50:37 +03:00
|
|
|
if (D->hasUnparsedDefaultArg() || D->hasUninstantiatedDefaultArg()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-03 18:24:03 +03:00
|
|
|
if (const Expr *Default = D->getDefaultArg()) {
|
2016-02-05 18:29:26 +03:00
|
|
|
if (const MaterializeTemporaryExpr *E =
|
|
|
|
dyn_cast<MaterializeTemporaryExpr>(Default)) {
|
2015-09-03 18:24:03 +03:00
|
|
|
// We have just found a ParmVarDecl which has, as its default argument,
|
|
|
|
// a MaterializeTemporaryExpr. We mark that MaterializeTemporaryExpr as
|
|
|
|
// automatic, by adding it to the AutomaticTemporaryMap.
|
|
|
|
// Reporting on this type will occur when the MaterializeTemporaryExpr
|
|
|
|
// is matched against.
|
|
|
|
AutomaticTemporaries[E] = D;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
2015-09-03 18:24:03 +03:00
|
|
|
} else if (const MaterializeTemporaryExpr *E =
|
2015-08-19 05:18:01 +03:00
|
|
|
Result.Nodes.getNodeAs<MaterializeTemporaryExpr>("node")) {
|
2015-09-03 18:24:03 +03:00
|
|
|
// Temporaries can actually have varying storage durations, due to temporary
|
|
|
|
// lifetime extension. We consider the allocation variety of this temporary
|
|
|
|
// to be the same as the allocation variety of its lifetime.
|
|
|
|
|
|
|
|
// XXX We maybe should mark these lifetimes as being due to a temporary
|
|
|
|
// which has had its lifetime extended, to improve the error messages.
|
|
|
|
switch (E->getStorageDuration()) {
|
2016-02-05 18:29:26 +03:00
|
|
|
case SD_FullExpression: {
|
|
|
|
// Check if this temporary is allocated as a default argument!
|
|
|
|
// if it is, we want to pretend that it is automatic.
|
|
|
|
AutomaticTemporaryMap::iterator AutomaticTemporary =
|
|
|
|
AutomaticTemporaries.find(E);
|
|
|
|
if (AutomaticTemporary != AutomaticTemporaries.end()) {
|
|
|
|
Variety = AV_Automatic;
|
|
|
|
} else {
|
|
|
|
Variety = AV_Temporary;
|
2015-09-03 18:24:03 +03:00
|
|
|
}
|
2016-02-05 18:29:26 +03:00
|
|
|
} break;
|
2015-09-03 18:24:03 +03:00
|
|
|
case SD_Automatic:
|
|
|
|
Variety = AV_Automatic;
|
|
|
|
break;
|
|
|
|
case SD_Thread:
|
|
|
|
case SD_Static:
|
|
|
|
Variety = AV_Global;
|
|
|
|
break;
|
|
|
|
case SD_Dynamic:
|
|
|
|
assert(false && "I don't think that this ever should occur...");
|
|
|
|
Variety = AV_Heap;
|
|
|
|
break;
|
|
|
|
}
|
2015-08-07 00:05:19 +03:00
|
|
|
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");
|
2015-09-03 18:24:03 +03:00
|
|
|
unsigned NonTemporaryID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "variable of type %0 is not valid in a temporary");
|
2015-08-07 00:05:19 +03:00
|
|
|
|
|
|
|
unsigned StackNoteID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
2015-08-19 05:18:01 +03:00
|
|
|
DiagnosticIDs::Note,
|
|
|
|
"value incorrectly allocated in an automatic variable");
|
2015-08-07 00:05:19 +03:00
|
|
|
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);
|
2016-02-05 18:29:26 +03:00
|
|
|
NonTemporaryClass.reportErrorIfPresent(Diag, T, Loc, NonTemporaryID,
|
|
|
|
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(
|
2015-08-19 05:18:01 +03:00
|
|
|
DiagnosticIDs::Error,
|
|
|
|
"cannot pass an arithmetic expression of built-in types to %0");
|
2014-12-18 23:25:15 +03:00
|
|
|
const Expr *expr = Result.Nodes.getNodeAs<Expr>("node");
|
|
|
|
if (const CallExpr *call = Result.Nodes.getNodeAs<CallExpr>("call")) {
|
|
|
|
Diag.Report(expr->getLocStart(), errorID) << call->getDirectCallee();
|
2015-08-19 05:18:01 +03:00
|
|
|
} else if (const CXXConstructExpr *ctr =
|
|
|
|
Result.Nodes.getNodeAs<CXXConstructExpr>("call")) {
|
2014-12-18 23:25:15 +03:00
|
|
|
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(
|
2015-08-19 05:18:01 +03:00
|
|
|
DiagnosticIDs::Error,
|
|
|
|
"class %0 must have trivial constructors and destructors");
|
2014-12-23 02:15:42 +03:00
|
|
|
const CXXRecordDecl *node = Result.Nodes.getNodeAs<CXXRecordDecl>("node");
|
|
|
|
|
2015-12-10 19:26:27 +03:00
|
|
|
// We need to accept non-constexpr trivial constructors as well. This occurs
|
|
|
|
// when a struct contains pod members, which will not be initialized. As
|
|
|
|
// constexpr values are initialized, the constructor is non-constexpr.
|
|
|
|
bool badCtor = !(node->hasConstexprDefaultConstructor() ||
|
|
|
|
node->hasTrivialDefaultConstructor());
|
2014-12-23 02:15:42 +03:00
|
|
|
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) {
|
2015-08-19 05:18:01 +03:00
|
|
|
// mozilla::IsNaN is not usable in C, so there is no point in issuing these
|
|
|
|
// warnings.
|
2014-12-21 20:33:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
|
|
|
unsigned errorID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
2015-08-19 05:18:01 +03:00
|
|
|
DiagnosticIDs::Error, "comparing a floating point value to itself for "
|
|
|
|
"NaN checking can lead to incorrect results");
|
2014-12-21 20:33:25 +03:00
|
|
|
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
|
2015-08-19 05:18:01 +03:00
|
|
|
// The check below ensures that we are dealing with the correct AST subtree
|
|
|
|
// shape, and
|
2014-12-21 20:33:25 +03:00
|
|
|
// also that both of the found DeclRefExpr's point to the same declaration.
|
2015-08-19 05:18:01 +03:00
|
|
|
if (lhs->getFoundDecl() == rhs->getFoundDecl() && lhsExpr && rhsExpr &&
|
2014-12-21 20:33:25 +03:00
|
|
|
std::distance(lhsExpr->child_begin(), lhsExpr->child_end()) == 1 &&
|
|
|
|
std::distance(rhsExpr->child_begin(), rhsExpr->child_end()) == 1 &&
|
2015-08-19 05:18:01 +03:00
|
|
|
*lhsExpr->child_begin() == lhs && *rhsExpr->child_begin() == rhs) {
|
2014-12-21 20:33:25 +03:00
|
|
|
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");
|
2015-08-19 05:18:01 +03:00
|
|
|
const CXXMethodDecl *method =
|
|
|
|
dyn_cast<CXXMethodDecl>(member->getMemberDecl());
|
2014-12-25 22:44:02 +03:00
|
|
|
|
|
|
|
Diag.Report(node->getLocStart(), errorID) << func << method;
|
|
|
|
}
|
|
|
|
|
2015-04-11 03:28:34 +03:00
|
|
|
void DiagnosticsMatcher::RefCountedInsideLambdaChecker::run(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
2016-07-19 00:29:04 +03:00
|
|
|
static DenseSet<const CXXRecordDecl*> CheckedDecls;
|
|
|
|
|
2015-04-11 03:28:34 +03:00
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
|
|
|
unsigned errorID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
2015-08-19 05:18:01 +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");
|
2016-07-19 00:29:04 +03:00
|
|
|
|
|
|
|
const CXXRecordDecl *Lambda = Result.Nodes.getNodeAs<CXXRecordDecl>("decl");
|
|
|
|
|
|
|
|
if (const LambdaExpr *OuterLambda = Result.Nodes.getNodeAs<LambdaExpr>("lambdaExpr")) {
|
|
|
|
const CXXMethodDecl *OpCall = OuterLambda->getCallOperator();
|
|
|
|
QualType ReturnTy = OpCall->getReturnType();
|
|
|
|
if (const CXXRecordDecl *Record = ReturnTy->getAsCXXRecordDecl()) {
|
|
|
|
Lambda = Record;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Lambda || !Lambda->isLambda()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't report errors on the same declarations more than once.
|
|
|
|
if (CheckedDecls.count(Lambda)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CheckedDecls.insert(Lambda);
|
2015-09-01 00:59:31 +03:00
|
|
|
|
|
|
|
for (const LambdaCapture Capture : Lambda->captures()) {
|
2016-01-26 03:54:05 +03:00
|
|
|
if (Capture.capturesVariable() && Capture.getCaptureKind() != LCK_ByRef) {
|
2015-09-01 00:59:31 +03:00
|
|
|
QualType Pointee = Capture.getCapturedVar()->getType()->getPointeeType();
|
2015-04-11 03:28:34 +03:00
|
|
|
|
2015-09-01 00:59:31 +03:00
|
|
|
if (!Pointee.isNull() && isClassRefCounted(Pointee)) {
|
2016-02-05 18:29:26 +03:00
|
|
|
Diag.Report(Capture.getLocation(), errorID) << Capture.getCapturedVar()
|
|
|
|
<< Pointee;
|
2015-09-01 00:59:31 +03:00
|
|
|
Diag.Report(Capture.getLocation(), noteID);
|
2016-07-19 00:29:04 +03:00
|
|
|
return;
|
2015-09-01 00:59:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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");
|
2015-08-19 05:18:01 +03:00
|
|
|
const CXXConversionDecl *method =
|
|
|
|
Result.Nodes.getNodeAs<CXXConversionDecl>("node");
|
2015-04-11 06:05:46 +03:00
|
|
|
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();
|
2016-08-04 19:54:25 +03:00
|
|
|
const CXXRecordDecl *D = Result.Nodes.getNodeAs<CXXRecordDecl>("decl");
|
|
|
|
const FieldDecl *refCntMember = getClassRefCntMember(D);
|
|
|
|
const FieldDecl *foundRefCntBase = nullptr;
|
2015-07-30 05:01:58 +03:00
|
|
|
|
2016-08-04 19:54:25 +03:00
|
|
|
if (!D->hasDefinition())
|
|
|
|
return;
|
|
|
|
D = D->getDefinition();
|
|
|
|
|
|
|
|
// If we don't have an mRefCnt member, and we have less than 2 superclasses,
|
|
|
|
// we don't have to run this loop, as neither case will ever apply.
|
|
|
|
if (!refCntMember && D->getNumBases() < 2) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-30 05:01:58 +03:00
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
// Check every superclass for whether it has a base with a refcnt member, and
|
|
|
|
// warn for those which do
|
2016-08-04 19:54:25 +03:00
|
|
|
for (auto &base : D->bases()) {
|
|
|
|
// Determine if this base class has an mRefCnt member
|
|
|
|
const FieldDecl *baseRefCntMember = getBaseRefCntMember(base.getType());
|
|
|
|
|
2015-07-30 05:01:58 +03:00
|
|
|
if (baseRefCntMember) {
|
2016-08-04 19:54:25 +03:00
|
|
|
if (refCntMember) {
|
|
|
|
// We have an mRefCnt, and superclass has an mRefCnt
|
|
|
|
unsigned error = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error,
|
|
|
|
"Refcounted record %0 has multiple mRefCnt members");
|
|
|
|
unsigned note1 = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "Superclass %0 also has an mRefCnt member");
|
|
|
|
unsigned note2 = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note,
|
|
|
|
"Consider using the _INHERITED macros for AddRef and Release here");
|
|
|
|
|
|
|
|
Diag.Report(D->getLocStart(), error) << D;
|
|
|
|
Diag.Report(baseRefCntMember->getLocStart(), note1)
|
|
|
|
<< baseRefCntMember->getParent();
|
|
|
|
Diag.Report(refCntMember->getLocStart(), note2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundRefCntBase) {
|
|
|
|
unsigned error = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error,
|
|
|
|
"Refcounted record %0 has multiple superclasses with mRefCnt members");
|
|
|
|
unsigned note = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note,
|
|
|
|
"Superclass %0 has an mRefCnt member");
|
|
|
|
|
|
|
|
// superclass has mRefCnt, and another superclass also has an mRefCnt
|
|
|
|
Diag.Report(D->getLocStart(), error) << D;
|
|
|
|
Diag.Report(baseRefCntMember->getLocStart(), note)
|
2015-08-19 05:18:01 +03:00
|
|
|
<< baseRefCntMember->getParent();
|
2016-08-04 19:54:25 +03:00
|
|
|
Diag.Report(foundRefCntBase->getLocStart(), note)
|
|
|
|
<< foundRefCntBase->getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record that we've found a base with a mRefCnt member
|
|
|
|
foundRefCntBase = baseRefCntMember;
|
2015-07-30 05:01:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(
|
2015-08-19 05:18:01 +03:00
|
|
|
DiagnosticIDs::Error,
|
|
|
|
"%0 cannot be instantiated because %1 has a VTable");
|
2015-06-20 02:30:51 +03:00
|
|
|
unsigned noteID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "bad instantiation of %0 requested here");
|
|
|
|
|
|
|
|
const ClassTemplateSpecializationDecl *specialization =
|
2015-08-19 05:18:01 +03:00
|
|
|
Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>("node");
|
2015-06-20 02:30:51 +03:00
|
|
|
|
|
|
|
// Get the offending template argument
|
|
|
|
QualType offender;
|
|
|
|
const TemplateArgumentList &args =
|
2015-08-19 05:18:01 +03:00
|
|
|
specialization->getTemplateInstantiationArgs();
|
2015-06-20 02:30:51 +03:00
|
|
|
for (unsigned i = 0; i < args.size(); ++i) {
|
|
|
|
offender = args[i].getAsType();
|
|
|
|
if (typeHasVTable(offender)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
Diag.Report(specialization->getLocStart(), errorID) << specialization
|
|
|
|
<< offender;
|
|
|
|
Diag.Report(specialization->getPointOfInstantiation(), noteID)
|
|
|
|
<< specialization;
|
2015-06-20 02:30:51 +03:00
|
|
|
}
|
|
|
|
|
2016-04-15 05:15:20 +03:00
|
|
|
void DiagnosticsMatcher::NonMemMovableTemplateArgChecker::run(
|
2015-06-19 00:34:27 +03:00
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
|
|
|
unsigned errorID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
2015-08-19 05:18:01 +03:00
|
|
|
DiagnosticIDs::Error,
|
|
|
|
"Cannot instantiate %0 with non-memmovable template argument %1");
|
2015-06-19 00:34:27 +03:00
|
|
|
unsigned note1ID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Note, "instantiation of %0 requested here");
|
|
|
|
|
|
|
|
// Get the specialization
|
|
|
|
const ClassTemplateSpecializationDecl *specialization =
|
2015-08-19 05:18:01 +03:00
|
|
|
Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>("specialization");
|
2015-06-19 00:34:27 +03:00
|
|
|
SourceLocation requestLoc = specialization->getPointOfInstantiation();
|
|
|
|
|
|
|
|
// Report an error for every template argument which is non-memmovable
|
|
|
|
const TemplateArgumentList &args =
|
2015-08-19 05:18:01 +03:00
|
|
|
specialization->getTemplateInstantiationArgs();
|
2015-06-19 00:34:27 +03:00
|
|
|
for (unsigned i = 0; i < args.size(); ++i) {
|
|
|
|
QualType argType = args[i].getAsType();
|
2016-04-15 05:15:20 +03:00
|
|
|
if (NonMemMovable.hasEffectiveAnnotation(argType)) {
|
2015-08-19 05:18:01 +03:00
|
|
|
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
|
2015-06-19 00:34:27 +03:00
|
|
|
// be useful)
|
2015-08-19 05:18:01 +03:00
|
|
|
Diag.Report(requestLoc, note1ID) << specialization;
|
2015-09-10 18:23:53 +03:00
|
|
|
NonMemMovable.dumpAnnotationReason(Diag, argType, requestLoc);
|
2016-04-15 06:01:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiagnosticsMatcher::NonMemMovableMemberChecker::run(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
|
|
|
unsigned errorID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error,
|
|
|
|
"class %0 cannot have non-memmovable member %1 of type %2");
|
|
|
|
|
|
|
|
// Get the specialization
|
|
|
|
const CXXRecordDecl* Decl =
|
|
|
|
Result.Nodes.getNodeAs<CXXRecordDecl>("decl");
|
|
|
|
|
|
|
|
// Report an error for every member which is non-memmovable
|
|
|
|
for (const FieldDecl *Field : Decl->fields()) {
|
|
|
|
QualType Type = Field->getType();
|
|
|
|
if (NonMemMovable.hasEffectiveAnnotation(Type)) {
|
|
|
|
Diag.Report(Field->getLocation(), errorID) << Decl
|
|
|
|
<< Field
|
|
|
|
<< Type;
|
|
|
|
NonMemMovable.dumpAnnotationReason(Diag, Type, Decl->getLocation());
|
2015-06-19 00:34:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(
|
2015-08-19 05:18:01 +03:00
|
|
|
DiagnosticIDs::Note,
|
|
|
|
"consider adding the explicit keyword to the constructor");
|
2015-07-27 16:10:30 +03:00
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
// We've already checked everything in the matcher, so we just have to report
|
|
|
|
// the error.
|
2015-07-27 16:10:30 +03:00
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
const CXXConstructorDecl *Ctor =
|
|
|
|
Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor");
|
2015-07-27 16:10:30 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-09-03 17:31:57 +03:00
|
|
|
void DiagnosticsMatcher::NoExplicitMoveConstructorChecker::run(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
|
|
|
unsigned ErrorID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "Move constructors may not be marked explicit");
|
|
|
|
|
|
|
|
// Everything we needed to know was checked in the matcher - we just report
|
|
|
|
// the error here
|
|
|
|
const CXXConstructorDecl *D =
|
2016-02-05 18:29:26 +03:00
|
|
|
Result.Nodes.getNodeAs<CXXConstructorDecl>("node");
|
2015-09-03 17:31:57 +03:00
|
|
|
|
|
|
|
Diag.Report(D->getLocation(), ErrorID);
|
|
|
|
}
|
|
|
|
|
2015-09-29 19:29:23 +03:00
|
|
|
void DiagnosticsMatcher::RefCountedCopyConstructorChecker::run(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
|
|
|
unsigned ErrorID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "Invalid use of compiler-provided copy constructor "
|
|
|
|
"on refcounted type");
|
|
|
|
unsigned NoteID = Diag.getDiagnosticIDs()->getCustomDiagID(
|
2016-02-05 18:29:26 +03:00
|
|
|
DiagnosticIDs::Note,
|
|
|
|
"The default copy constructor also copies the "
|
|
|
|
"default mRefCnt property, leading to reference "
|
|
|
|
"count imbalance issues. Please provide your own "
|
|
|
|
"copy constructor which only copies the fields which "
|
|
|
|
"need to be copied");
|
2015-09-29 19:29:23 +03:00
|
|
|
|
|
|
|
// Everything we needed to know was checked in the matcher - we just report
|
|
|
|
// the error here
|
2016-02-05 18:29:26 +03:00
|
|
|
const CXXConstructExpr *E = Result.Nodes.getNodeAs<CXXConstructExpr>("node");
|
2015-09-29 19:29:23 +03:00
|
|
|
|
|
|
|
Diag.Report(E->getLocation(), ErrorID);
|
|
|
|
Diag.Report(E->getLocation(), NoteID);
|
|
|
|
}
|
|
|
|
|
2016-07-19 09:59:22 +03:00
|
|
|
void DiagnosticsMatcher::AssertAssignmentChecker::run(
|
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
DiagnosticsEngine &Diag = Result.Context->getDiagnostics();
|
|
|
|
unsigned assignInsteadOfComp = Diag.getDiagnosticIDs()->getCustomDiagID(
|
|
|
|
DiagnosticIDs::Error, "Forbidden assignment in assert expression");
|
|
|
|
const CallExpr *funcCall = Result.Nodes.getNodeAs<CallExpr>("funcCall");
|
|
|
|
|
|
|
|
if (funcCall && HasSideEffectAssignment(funcCall)) {
|
|
|
|
Diag.Report(funcCall->getLocStart(), assignInsteadOfComp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-24 06:12:25 +04:00
|
|
|
class MozCheckAction : public PluginASTAction {
|
|
|
|
public:
|
2015-08-19 05:18:01 +03:00
|
|
|
ASTConsumerPtr CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
StringRef fileName) override {
|
2014-12-10 18:46:10 +03:00
|
|
|
#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);
|
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
ASTConsumer *consumers[] = {checker, checker->getOtherConsumer()};
|
2013-04-12 07:20:02 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-08-19 05:18:01 +03:00
|
|
|
static FrontendPluginRegistry::Add<MozCheckAction> X("moz-check",
|
|
|
|
"check moz action");
|
2016-02-26 15:26:15 +03:00
|
|
|
// Export the registry on Windows.
|
|
|
|
#ifdef LLVM_EXPORT_REGISTRY
|
|
|
|
LLVM_EXPORT_REGISTRY(FrontendPluginRegistry)
|
|
|
|
#endif
|