Exemplo n.º 1
0
static void check(Analyser& analyser, CastExpr const *expr)
{
    if (expr->getSubExpr()->isNullPointerConstant(
            *analyser.context(), Expr::NPC_ValueDependentIsNotNull)) {
        return;                                                       // RETURN
    }
    if (expr->getCastKind() != CK_BitCast &&
        expr->getCastKind() != CK_LValueBitCast &&
        expr->getCastKind() != CK_IntegralToPointer) {
        return;                                                       // RETURN
    }
    CanQualType source(getType(expr->getSubExpr()->getType()));
    CanQualType target(getType(expr->getType()));
    std::string tt = static_cast<QualType>(target).getAsString();
    if ((source != target &&
         tt != "char" &&
         tt != "unsigned char" &&
         tt != "signed char" &&
         tt != "void") ||
        (expr->getType()->isPointerType() !=
         expr->getSubExpr()->getType()->isPointerType())) {
        analyser.report(expr, check_name, "AL01",
                        "Possible strict-aliasing violation")
            << expr->getSourceRange();
    }
}
Exemplo n.º 2
0
static void
process(Analyser& analyser, Expr const* expr, Decl const* decl)
{
    if (const FunctionDecl* function =
            decl ? llvm::dyn_cast<FunctionDecl>(decl) : 0) {
        function = function->getCanonicalDecl();
        std::string name;
        PrintingPolicy policy(analyser.context()->getLangOpts());
        function->getNameForDiagnostic(name, policy, true);

        std::ostringstream out;
        out << name << "(";
        for (FunctionDecl::param_const_iterator it(function->param_begin()),
             end(function->param_end());
             it != end;
             ++it) {
            if (it != function->param_begin()) {
                out << ", ";
            }
            ParmVarDecl const* param(*it);
            out << param->getType().getAsString();
            if (param->isParameterPack()) {
                out << "...";
            }
        }
        out << ")";
        CXXMethodDecl const* method(llvm::dyn_cast<CXXMethodDecl>(function));
        if (method && !method->isStatic()) {
            if (method->getTypeQualifiers() & Qualifiers::Const) {
                out << " const";
            }
            if (method->getTypeQualifiers() & Qualifiers::Volatile) {
                out << " volatile";
            }
            if (method->getTypeQualifiers() & Qualifiers::Restrict) {
                out << " restrict";
            }
        }

        name += out.str();

        //-dk:TODO analyser.report(expr, check_name, "function decl: '%0'")
        //-dk:TODO     << expr->getSourceRange()
        //-dk:TODO     << out.str()
            ;
    }
    else {
        analyser.report(expr, check_name, "UF01", "Unresolved function call")
            << expr->getSourceRange();
    }
}
Exemplo n.º 3
0
static bool is_addition(Analyser& analyser,
                        Expr const* str,
                        Expr const* value,
                        BinaryOperatorKind op)
{
    if (StringLiteral const* lit =
            llvm::dyn_cast<StringLiteral>(str->IgnoreParenCasts())) {
        llvm::APSInt length(32, false);
        llvm::APSInt zero(32, false);
        length = lit->getByteLength();
        zero = 0u;
        value = value->IgnoreParenCasts();
        llvm::APSInt result;
        return !value->isIntegerConstantExpr(result, *analyser.context()) ||
               (op == BO_Add && (result < zero || length < result)) ||
               (op == BO_Sub && (zero < result || length + result < zero));
    }
    return false;
}