Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang][dataflow] Add reverse null checker #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "NoEscapeCheck.h"
#include "NonZeroEnumToBoolConversionCheck.h"
#include "NotNullTerminatedResultCheck.h"
#include "NullCheckAfterDereferenceCheck.h"
#include "OptionalValueConversionCheck.h"
#include "ParentVirtualCallCheck.h"
#include "PosixReturnCheck.h"
Expand Down Expand Up @@ -180,6 +181,8 @@ class BugproneModule : public ClangTidyModule {
CheckFactories.registerCheck<PosixReturnCheck>("bugprone-posix-return");
CheckFactories.registerCheck<ReservedIdentifierCheck>(
"bugprone-reserved-identifier");
CheckFactories.registerCheck<NullCheckAfterDereferenceCheck>(
"bugprone-null-check-after-dereference");
CheckFactories.registerCheck<SharedPtrArrayMismatchCheck>(
"bugprone-shared-ptr-array-mismatch");
CheckFactories.registerCheck<SignalHandlerCheck>("bugprone-signal-handler");
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ add_clang_library(clangTidyBugproneModule
NoEscapeCheck.cpp
NonZeroEnumToBoolConversionCheck.cpp
NotNullTerminatedResultCheck.cpp
NullCheckAfterDereferenceCheck.cpp
OptionalValueConversionCheck.cpp
ParentVirtualCallCheck.cpp
PosixReturnCheck.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
//===--- NullCheckAfterDereferenceCheck.cpp - clang-tidy-------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "NullCheckAfterDereferenceCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Analysis/CFG.h"
#include "clang/Analysis/FlowSensitive/ControlFlowContext.h"
#include "clang/Analysis/FlowSensitive/DataflowAnalysisContext.h"
#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
#include "clang/Analysis/FlowSensitive/DataflowLattice.h"
#include "clang/Analysis/FlowSensitive/Models/NullPointerAnalysisModel.h"
#include "clang/Analysis/FlowSensitive/WatchedLiteralsSolver.h"
#include "clang/Basic/SourceLocation.h"
#include "llvm/ADT/Any.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Error.h"
#include <memory>
#include <vector>

namespace clang::tidy::bugprone {

using ast_matchers::MatchFinder;
using dataflow::NullPointerAnalysisModel;
using dataflow::NullCheckAfterDereferenceDiagnoser;

static constexpr llvm::StringLiteral FuncID("fun");

struct ExpandedResult {
SourceLocation WarningLoc;
std::optional<SourceLocation> DerefLoc;
};

using ExpandedResultType = std::pair<std::vector<ExpandedResult>,
std::vector<ExpandedResult>>;

static std::optional<ExpandedResultType> analyzeFunction(
const FunctionDecl &FuncDecl) {
using dataflow::ControlFlowContext;
using dataflow::DataflowAnalysisState;
using llvm::Expected;

ASTContext &ASTCtx = FuncDecl.getASTContext();

if (FuncDecl.getBody() == nullptr) {
return std::nullopt;
}

Expected<ControlFlowContext> Context =
ControlFlowContext::build(FuncDecl, *FuncDecl.getBody(), ASTCtx);
if (!Context)
return std::nullopt;

dataflow::DataflowAnalysisContext AnalysisContext(
std::make_unique<dataflow::WatchedLiteralsSolver>());
dataflow::Environment Env(AnalysisContext, FuncDecl);
NullPointerAnalysisModel Analysis(ASTCtx);
NullCheckAfterDereferenceDiagnoser Diagnoser;
NullCheckAfterDereferenceDiagnoser::ResultType Diagnostics;

using LatticeState = DataflowAnalysisState<NullPointerAnalysisModel::Lattice>;
using DetailMaybeLatticeStates = std::vector<std::optional<LatticeState>>;

auto DiagnoserImpl = [&ASTCtx, &Diagnoser, &Diagnostics](
const CFGElement &Elt,
const LatticeState &S) mutable -> void {
auto EltDiagnostics = Diagnoser.diagnose(ASTCtx, &Elt, S.Env);
llvm::move(EltDiagnostics.first,
std::back_inserter(Diagnostics.first));
llvm::move(EltDiagnostics.second,
std::back_inserter(Diagnostics.second));
};

Expected<DetailMaybeLatticeStates>
BlockToOutputState = dataflow::runDataflowAnalysis(
*Context, Analysis, Env, DiagnoserImpl);

if (llvm::Error E = BlockToOutputState.takeError()) {
llvm::dbgs() << "Dataflow analysis failed: " << llvm::toString(std::move(E))
<< ".\n";
Comment on lines +87 to +88

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be worthwhile if the user was notified that the data-flow analysis of the file or the function failed even if they are not running with --debug flag passed. This could go to ::errs(). Not a real formatted diagnostic, but at least visible even if someone didn't explicitly go out of their way to want to see it?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this information would be useful for the majority of cases, especially not as an on-by-default message. I could implement a checker option to show this message, but at that point the user can also run -dataflow-log and see more information than what we can provide.

return std::nullopt;
}

ExpandedResultType ExpandedDiagnostics;

llvm::transform(Diagnostics.first,
std::back_inserter(ExpandedDiagnostics.first),
[&](SourceLocation WarningLoc) -> ExpandedResult {
if (auto Val = Diagnoser.WarningLocToVal[WarningLoc];
auto DerefExpr = Diagnoser.ValToDerefLoc[Val]) {
return {WarningLoc, DerefExpr->getBeginLoc()};
}

return {WarningLoc, std::nullopt};
});

llvm::transform(Diagnostics.second,
std::back_inserter(ExpandedDiagnostics.second),
[&](SourceLocation WarningLoc) -> ExpandedResult {
if (auto Val = Diagnoser.WarningLocToVal[WarningLoc];
auto DerefExpr = Diagnoser.ValToDerefLoc[Val]) {
return {WarningLoc, DerefExpr->getBeginLoc()};
}

return {WarningLoc, std::nullopt};
});

return ExpandedDiagnostics;
}

void NullCheckAfterDereferenceCheck::registerMatchers(MatchFinder *Finder) {
using namespace ast_matchers;

auto hasPointerValue =
hasDescendant(NullPointerAnalysisModel::ptrValueMatcher());
Finder->addMatcher(
decl(anyOf(functionDecl(unless(isExpansionInSystemHeader()),
// FIXME: Remove the filter below when lambdas are
// well supported by the check.
unless(hasDeclContext(cxxRecordDecl(isLambda()))),
hasBody(hasPointerValue)),
cxxConstructorDecl(hasAnyConstructorInitializer(
withInitializer(hasPointerValue)))))
.bind(FuncID),
this);
}

void NullCheckAfterDereferenceCheck::check(
const MatchFinder::MatchResult &Result) {
if (Result.SourceManager->getDiagnostics().hasUncompilableErrorOccurred())
return;

const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>(FuncID);
assert(FuncDecl && "invalid FuncDecl matcher");
if (FuncDecl->isTemplated())
return;

if (const auto Diagnostics = analyzeFunction(*FuncDecl)) {
const auto& [CheckWhenNullLocations, CheckAfterDereferenceLocations] =
*Diagnostics;

for (const auto [WarningLoc, DerefLoc] : CheckAfterDereferenceLocations) {
diag(WarningLoc,
"pointer value is checked even though "
"it cannot be null at this point");

if (DerefLoc) {
diag(*DerefLoc,
"one of the locations where the pointer's value cannot be null",
DiagnosticIDs::Note);
}
}

for (const auto [WarningLoc, DerefLoc] : CheckWhenNullLocations) {
diag(WarningLoc,
"pointer value is checked but it can only be null at this point");

if (DerefLoc) {
diag(*DerefLoc,
"one of the locations where the pointer's value can only be null",
DiagnosticIDs::Note);
}
}
}
}

} // namespace clang::tidy::bugprone
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===--- NullCheckAfterDereferenceCheck.h - clang-tidy ----------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NULLCHECKAFTERDEREFERENCECHECK_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NULLCHECKAFTERDEREFERENCECHECK_H

#include "../ClangTidyCheck.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"

namespace clang::tidy::bugprone {

/// Finds checks for pointer nullability after a pointer has already been
/// dereferenced, using the data-flow framework.
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/null-check-after-dereference.html
class NullCheckAfterDereferenceCheck : public ClangTidyCheck {
public:
NullCheckAfterDereferenceCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}

// The data-flow framework does not support C because of AST differences.
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
return LangOpts.CPlusPlus;
}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
};

} // namespace clang::tidy::bugprone

#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NULLCHECKAFTERDEREFERENCECHECK_H
3 changes: 2 additions & 1 deletion clang-tools-extra/clangd/TidyProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,10 @@ TidyProvider disableUnusableChecks(llvm::ArrayRef<std::string> ExtraBadChecks) {
"-bugprone-use-after-move",
// Alias for bugprone-use-after-move.
"-hicpp-invalid-access-moved",
// Check uses dataflow analysis, which might hang/crash unexpectedly on
// Checks use dataflow analysis, which might hang/crash unexpectedly on
// incomplete code.
"-bugprone-unchecked-optional-access",
"-bugprone-null-check-after-dereference",

// ----- Performance problems -----

Expand Down
Loading