| 1 |
//===- ChromiumCheckModelTest.cpp -----------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// FIXME: Move this to clang/unittests/Analysis/FlowSensitive/Models. |
| 9 |
|
| 10 |
#include "clang/Analysis/FlowSensitive/Models/ChromiumCheckModel.h" |
| 11 |
#include "TestingSupport.h" |
| 12 |
#include "clang/AST/ASTContext.h" |
| 13 |
#include "clang/ASTMatchers/ASTMatchers.h" |
| 14 |
#include "clang/Analysis/CFG.h" |
| 15 |
#include "clang/Analysis/FlowSensitive/NoopLattice.h" |
| 16 |
#include "clang/Tooling/Tooling.h" |
| 17 |
#include "llvm/ADT/ArrayRef.h" |
| 18 |
#include "llvm/Support/Error.h" |
| 19 |
#include "llvm/Testing/Support/Error.h" |
| 20 |
#include "gmock/gmock.h" |
| 21 |
#include "gtest/gtest.h" |
| 22 |
#include <string> |
| 23 |
|
| 24 |
using namespace clang; |
| 25 |
using namespace dataflow; |
| 26 |
using namespace test; |
| 27 |
|
| 28 |
namespace { |
| 29 |
using ::testing::NotNull; |
| 30 |
using ::testing::UnorderedElementsAre; |
| 31 |
|
| 32 |
static constexpr char ChromiumCheckHeader[] = R"( |
| 33 |
namespace std { |
| 34 |
class ostream; |
| 35 |
} // namespace std |
| 36 |
|
| 37 |
namespace logging { |
| 38 |
class VoidifyStream { |
| 39 |
public: |
| 40 |
VoidifyStream() = default; |
| 41 |
void operator&(std::ostream&) {} |
| 42 |
}; |
| 43 |
|
| 44 |
class CheckError { |
| 45 |
public: |
| 46 |
static CheckError Check(const char* file, int line, const char* condition); |
| 47 |
static CheckError DCheck(const char* file, int line, const char* condition); |
| 48 |
static CheckError PCheck(const char* file, int line, const char* condition); |
| 49 |
static CheckError PCheck(const char* file, int line); |
| 50 |
static CheckError DPCheck(const char* file, int line, const char* condition); |
| 51 |
|
| 52 |
std::ostream& stream(); |
| 53 |
|
| 54 |
~CheckError(); |
| 55 |
|
| 56 |
CheckError(const CheckError& other) = delete; |
| 57 |
CheckError& operator=(const CheckError& other) = delete; |
| 58 |
CheckError(CheckError&& other) = default; |
| 59 |
CheckError& operator=(CheckError&& other) = default; |
| 60 |
}; |
| 61 |
|
| 62 |
} // namespace logging |
| 63 |
|
| 64 |
#define LAZY_CHECK_STREAM(stream, condition) \ |
| 65 |
!(condition) ? (void)0 : ::logging::VoidifyStream() & (stream) |
| 66 |
|
| 67 |
#define CHECK(condition) \ |
| 68 |
LAZY_CHECK_STREAM( \ |
| 69 |
::logging::CheckError::Check(__FILE__, __LINE__, #condition).stream(), \ |
| 70 |
!(condition)) |
| 71 |
|
| 72 |
#define PCHECK(condition) \ |
| 73 |
LAZY_CHECK_STREAM( \ |
| 74 |
::logging::CheckError::PCheck(__FILE__, __LINE__, #condition).stream(), \ |
| 75 |
!(condition)) |
| 76 |
|
| 77 |
#define DCHECK(condition) \ |
| 78 |
LAZY_CHECK_STREAM( \ |
| 79 |
::logging::CheckError::DCheck(__FILE__, __LINE__, #condition).stream(), \ |
| 80 |
!(condition)) |
| 81 |
|
| 82 |
#define DPCHECK(condition) \ |
| 83 |
LAZY_CHECK_STREAM( \ |
| 84 |
::logging::CheckError::DPCheck(__FILE__, __LINE__, #condition).stream(), \ |
| 85 |
!(condition)) |
| 86 |
)"; |
| 87 |
|
| 88 |
// A definition of the `CheckError` class that looks like the Chromium one, but |
| 89 |
// is actually something else. |
| 90 |
static constexpr char OtherCheckHeader[] = R"( |
| 91 |
namespace other { |
| 92 |
namespace logging { |
| 93 |
class CheckError { |
| 94 |
public: |
| 95 |
static CheckError Check(const char* file, int line, const char* condition); |
| 96 |
}; |
| 97 |
} // namespace logging |
| 98 |
} // namespace other |
| 99 |
)"; |
| 100 |
|
| 101 |
/// Replaces all occurrences of `Pattern` in `S` with `Replacement`. |
| 102 |
std::string ReplacePattern(std::string S, const std::string &Pattern, |
| 103 |
const std::string &Replacement) { |
| 104 |
size_t Pos = 0; |
| 105 |
Pos = S.find(Pattern, Pos); |
| 106 |
if (Pos != std::string::npos) |
| 107 |
S.replace(Pos, Pattern.size(), Replacement); |
| 108 |
return S; |
| 109 |
} |
| 110 |
|
| 111 |
template <typename Model> |
| 112 |
class ModelAdaptorAnalysis |
| 113 |
: public DataflowAnalysis<ModelAdaptorAnalysis<Model>, NoopLattice> { |
| 114 |
public: |
| 115 |
explicit ModelAdaptorAnalysis(ASTContext &Context) |
| 116 |
: DataflowAnalysis<ModelAdaptorAnalysis, NoopLattice>( |
| 117 |
Context, /*ApplyBuiltinTransfer=*/true) {} |
| 118 |
|
| 119 |
static NoopLattice initialElement() { return NoopLattice(); } |
| 120 |
|
| 121 |
void transfer(const CFGElement *E, NoopLattice &, Environment &Env) { |
| 122 |
M.transfer(E, Env); |
| 123 |
} |
| 124 |
|
| 125 |
private: |
| 126 |
Model M; |
| 127 |
}; |
| 128 |
|
| 129 |
template <typename Matcher> |
| 130 |
void runDataflow(llvm::StringRef Code, Matcher Match) { |
| 131 |
const tooling::FileContentMappings FileContents = { |
| 132 |
{"check.h", ChromiumCheckHeader}, {"othercheck.h", OtherCheckHeader}}; |
| 133 |
|
| 134 |
ASSERT_THAT_ERROR( |
| 135 |
checkDataflow<ModelAdaptorAnalysis<ChromiumCheckModel>>( |
| 136 |
AnalysisInputs<ModelAdaptorAnalysis<ChromiumCheckModel>>( |
| 137 |
Code, ast_matchers::hasName("target"), |
| 138 |
[](ASTContext &C, Environment &) { |
| 139 |
return ModelAdaptorAnalysis<ChromiumCheckModel>(C); |
| 140 |
}) |
| 141 |
.withASTBuildArgs({"-fsyntax-only", |
| 142 |
"-fno-delayed-template-parsing", "-std=c++17"}) |
| 143 |
.withASTBuildVirtualMappedFiles(std::move(FileContents)), |
| 144 |
/*VerifyResults=*/ |
| 145 |
[&Match](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> |
| 146 |
&Results, |
| 147 |
const AnalysisOutputs &AO) { Match(Results, AO.ASTCtx); }), |
| 148 |
llvm::Succeeded()); |
| 149 |
} |
| 150 |
|
| 151 |
TEST(ChromiumCheckModelTest, CheckSuccessImpliesConditionHolds) { |
| 152 |
auto Expectations = |
| 153 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 154 |
ASTContext &ASTCtx) { |
| 155 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p")); |
| 156 |
const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); |
| 157 |
|
| 158 |
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); |
| 159 |
ASSERT_THAT(FooDecl, NotNull()); |
| 160 |
|
| 161 |
auto *FooVal = cast<BoolValue>(Env.getValue(*FooDecl, SkipPast::None)); |
| 162 |
|
| 163 |
EXPECT_TRUE(Env.flowConditionImplies(*FooVal)); |
| 164 |
}; |
| 165 |
|
| 166 |
std::string Code = R"( |
| 167 |
#include "check.h" |
| 168 |
|
| 169 |
void target(bool Foo) { |
| 170 |
$check(Foo); |
| 171 |
bool X = true; |
| 172 |
(void)X; |
| 173 |
// [[p]] |
| 174 |
} |
| 175 |
)"; |
| 176 |
runDataflow(ReplacePattern(Code, "$check", "CHECK"), Expectations); |
| 177 |
runDataflow(ReplacePattern(Code, "$check", "DCHECK"), Expectations); |
| 178 |
runDataflow(ReplacePattern(Code, "$check", "PCHECK"), Expectations); |
| 179 |
runDataflow(ReplacePattern(Code, "$check", "DPCHECK"), Expectations); |
| 180 |
} |
| 181 |
|
| 182 |
TEST(ChromiumCheckModelTest, UnrelatedCheckIgnored) { |
| 183 |
auto Expectations = |
| 184 |
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, |
| 185 |
ASTContext &ASTCtx) { |
| 186 |
ASSERT_THAT(Results.keys(), UnorderedElementsAre("p")); |
| 187 |
const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); |
| 188 |
|
| 189 |
const ValueDecl *FooDecl = findValueDecl(ASTCtx, "Foo"); |
| 190 |
ASSERT_THAT(FooDecl, NotNull()); |
| 191 |
|
| 192 |
auto *FooVal = cast<BoolValue>(Env.getValue(*FooDecl, SkipPast::None)); |
| 193 |
|
| 194 |
EXPECT_FALSE(Env.flowConditionImplies(*FooVal)); |
| 195 |
}; |
| 196 |
|
| 197 |
std::string Code = R"( |
| 198 |
#include "othercheck.h" |
| 199 |
|
| 200 |
void target(bool Foo) { |
| 201 |
if (!Foo) { |
| 202 |
(void)other::logging::CheckError::Check(__FILE__, __LINE__, "Foo"); |
| 203 |
} |
| 204 |
bool X = true; |
| 205 |
(void)X; |
| 206 |
// [[p]] |
| 207 |
} |
| 208 |
)"; |
| 209 |
runDataflow(Code, Expectations); |
| 210 |
} |
| 211 |
} // namespace |