Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
92d4136
[𝘀𝗽𝗿] initial version
fmayer Oct 10, 2025
2144274
nit
fmayer Oct 10, 2025
2d827ee
remove some mock headers
fmayer Oct 10, 2025
faddd7a
hopefully make MSVC happy
fmayer Oct 11, 2025
ccc6fad
[𝘀𝗽𝗿] changes introduced through rebase
fmayer Oct 13, 2025
e7b6851
rebase
fmayer Oct 13, 2025
09ccdf2
[𝘀𝗽𝗿] changes introduced through rebase
fmayer Oct 13, 2025
5b6b24a
rebase
fmayer Oct 13, 2025
186a8fb
[𝘀𝗽𝗿] changes introduced through rebase
fmayer Oct 13, 2025
075ec70
rebase
fmayer Oct 13, 2025
b63014f
[𝘀𝗽𝗿] changes introduced through rebase
fmayer Oct 13, 2025
28e5ea9
rebase
fmayer Oct 13, 2025
4f260a6
[𝘀𝗽𝗿] changes introduced through rebase
fmayer Oct 13, 2025
0a66800
rebase
fmayer Oct 13, 2025
93db1aa
[𝘀𝗽𝗿] changes introduced through rebase
fmayer Oct 13, 2025
4391a64
reb
fmayer Oct 13, 2025
ad75877
typos
fmayer Oct 14, 2025
19392e8
[𝘀𝗽𝗿] changes introduced through rebase
fmayer Oct 15, 2025
69ea21c
rebase
fmayer Oct 15, 2025
b772183
[𝘀𝗽𝗿] changes introduced through rebase
fmayer Oct 15, 2025
09a2606
rebase
fmayer Oct 15, 2025
bdb3cec
[𝘀𝗽𝗿] changes introduced through rebase
fmayer Oct 15, 2025
11f053a
rebase
fmayer Oct 15, 2025
8f42505
fix test
fmayer Oct 15, 2025
7d55b2b
move test
fmayer Oct 15, 2025
9871725
address comemnts
fmayer Oct 15, 2025
4222be7
[𝘀𝗽𝗿] changes introduced through rebase
kimsh02 Oct 16, 2025
0c764d1
rebase
fmayer Oct 16, 2025
eb94253
comments
fmayer Oct 17, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//===- UncheckedStatusOrAccessModel.h -------------------------------------===//
//
// 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 CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDSTATUSORACCESSMODEL_H
#define CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDSTATUSORACCESSMODEL_H

#include "clang/AST/Type.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Analysis/CFG.h"
#include "clang/Analysis/FlowSensitive/CFGMatchSwitch.h"
#include "clang/Analysis/FlowSensitive/DataflowAnalysis.h"
#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
#include "clang/Analysis/FlowSensitive/MatchSwitch.h"
#include "clang/Analysis/FlowSensitive/NoopLattice.h"
#include "clang/Analysis/FlowSensitive/StorageLocation.h"
#include "clang/Analysis/FlowSensitive/Value.h"
#include "clang/Basic/SourceLocation.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"

namespace clang::dataflow::statusor_model {

// The helper functions exported here are for use of downstream vendor
// extensions of this model.

// Match declaration of `absl::StatusOr<T>` and bind `T` to "T".
clang::ast_matchers::DeclarationMatcher statusOrClass();
// Match declaration of `absl::Status`.
clang::ast_matchers::DeclarationMatcher statusClass();
// Match declaration of `absl::internal_statusor::OperatorBase`.
clang::ast_matchers::DeclarationMatcher statusOrOperatorBaseClass();
clang::ast_matchers::TypeMatcher statusOrType();

// Get RecordStorageLocation for the `Status` contained in the `StatusOr`
RecordStorageLocation &locForStatus(RecordStorageLocation &StatusOrLoc);
// Get the StorageLocation for the OK boolean in the `Status`
StorageLocation &locForOk(RecordStorageLocation &StatusLoc);
// Get the OK boolean in the `Status`, and initialize it if necessary.
BoolValue &valForOk(RecordStorageLocation &StatusLoc, Environment &Env);
// Get synthetic fields for the types modelled by
// `UncheckedStatusOrAccessModel`.
llvm::StringMap<QualType> getSyntheticFields(QualType Ty, QualType StatusType,
const CXXRecordDecl &RD);

// Initialize the synthetic fields of the `StatusOr`.
// N.B. if it is already initialized, the value gets reset.
BoolValue &initializeStatusOr(RecordStorageLocation &StatusOrLoc,
Environment &Env);
// Initialize the synthetic fields of the `Status`.
// N.B. if it is already initialized, the value gets reset.
BoolValue &initializeStatus(RecordStorageLocation &StatusLoc, Environment &Env);

bool isRecordTypeWithName(QualType Type, llvm::StringRef TypeName);
// Return true if `Type` is instantiation of `absl::StatusOr<T>`
bool isStatusOrType(QualType Type);
// Return true if `Type` is `absl::Status`
bool isStatusType(QualType Type);

// Get `QualType` for `absl::Status`, or default-constructed
// QualType if it does not exist.
QualType findStatusType(const ASTContext &Ctx);

struct UncheckedStatusOrAccessModelOptions {};

// Dataflow analysis that discovers unsafe uses of StatusOr values.
class UncheckedStatusOrAccessModel
: public DataflowAnalysis<UncheckedStatusOrAccessModel, NoopLattice> {
public:
explicit UncheckedStatusOrAccessModel(ASTContext &Ctx, Environment &Env);

static Lattice initialElement() { return {}; }

void transfer(const CFGElement &Elt, Lattice &L, Environment &Env);

private:
CFGMatchSwitch<TransferState<Lattice>> TransferMatchSwitch;
};

using LatticeTransferState =
TransferState<UncheckedStatusOrAccessModel::Lattice>;

// Extend the Builder with the transfer functions for
// `UncheckedStatusOrAccessModel`. This is useful to write downstream models
// that extend the model.
CFGMatchSwitch<LatticeTransferState>
buildTransferMatchSwitch(ASTContext &Ctx,
CFGMatchSwitchBuilder<LatticeTransferState> Builder);

class UncheckedStatusOrAccessDiagnoser {
public:
explicit UncheckedStatusOrAccessDiagnoser(
UncheckedStatusOrAccessModelOptions Options = {});

llvm::SmallVector<SourceLocation> operator()(
const CFGElement &Elt, ASTContext &Ctx,
const TransferStateForDiagnostics<UncheckedStatusOrAccessModel::Lattice>
&State);

private:
CFGMatchSwitch<const Environment, llvm::SmallVector<SourceLocation>>
DiagnoseMatchSwitch;
};

} // namespace clang::dataflow::statusor_model

#endif // CLANG_ANALYSIS_FLOWSENSITIVE_MODELS_UNCHECKEDSTATUSORACCESSMODEL_H
1 change: 1 addition & 0 deletions clang/lib/Analysis/FlowSensitive/Models/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_clang_library(clangAnalysisFlowSensitiveModels
ChromiumCheckModel.cpp
UncheckedOptionalAccessModel.cpp
UncheckedStatusOrAccessModel.cpp

LINK_LIBS
clangAnalysis
Expand Down
Loading