Skip to content

Commit 2e439df

Browse files
committed
Merge remote-tracking branch 'origin/main' into no_extra_atoms
2 parents 516b7a1 + 3975661 commit 2e439df

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

clang/include/clang/3C/ConstraintVariables.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ class PointerVariableConstraint : public ConstraintVariable {
325325
// String representing declared bounds expression.
326326
std::string BoundsAnnotationStr;
327327

328+
// TODO can we move this to an optional instead of the -1?
328329
// Does this variable represent a generic type? Which one (or -1 for none)?
329330
// Generic types can be used with fewer restrictions, so this field is used
330331
// stop assignments with generic variables from forcing constraint variables

clang/include/clang/3C/TypeVariableAnalysis.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class TypeVariableEntry {
3333
}
3434

3535
bool getIsConsistent() const;
36+
// Note: undefined behaviour if `getIsConsistent` is false
3637
QualType getType();
38+
// Note: undefined behaviour if `getIsConsistent` is false
3739
std::set<ConstraintVariable *> &getConstraintVariables();
3840
ConstraintVariable *getTypeParamConsVar();
3941

@@ -53,6 +55,7 @@ class TypeVariableEntry {
5355

5456
// Collection of constraint variables generated for all uses of the type
5557
// variable. Also should not be used when IsConsistent is false.
58+
// TODO: accessor methods don't enforce this?
5659
std::set<ConstraintVariable *> ArgConsVars;
5760

5861
// A single constraint variable for solving the checked type of the type
@@ -65,6 +68,7 @@ class TypeVariableEntry {
6568
// typed parameter. The values in the map are another maps from type variable
6669
// index in the called function's parameter list to the type the type variable
6770
// becomes (or null if it is not used consistently).
71+
// TODO: use a better map implementation?
6872
typedef std::map<CallExpr *, std::map<unsigned int, TypeVariableEntry>>
6973
TypeVariableMapT;
7074

@@ -95,8 +99,7 @@ class TypeVarVisitor : public RecursiveASTVisitor<TypeVarVisitor>,
9599
ConstraintResolver CR;
96100
TypeVariableMapT TVMap;
97101

98-
void insertBinding(CallExpr *CE, const int TyIdx, QualType Ty, CVarSet &CVs,
99-
bool ForceInconsistent = false);
102+
void insertBinding(CallExpr *CE, const int TyIdx, QualType Ty, CVarSet &CVs);
100103
};
101104

102105
bool typeArgsProvided(CallExpr *Call);

clang/lib/3C/TypeVariableAnalysis.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ bool TypeVarVisitor::VisitCastExpr(CastExpr *CE) {
7272
if (CHKCBindTemporaryExpr *TempE = dyn_cast<CHKCBindTemporaryExpr>(SubExpr))
7373
SubExpr = TempE->getSubExpr();
7474

75-
if (auto *Call = dyn_cast<CallExpr>(SubExpr))
75+
if (auto *Call = dyn_cast<CallExpr>(SubExpr)) {
7676
if (auto *FD = dyn_cast_or_null<FunctionDecl>(Call->getCalleeDecl())) {
7777
FunctionDecl *FDef = getDefinition(FD);
7878
if (FDef == nullptr)
@@ -87,6 +87,7 @@ bool TypeVarVisitor::VisitCastExpr(CastExpr *CE) {
8787
}
8888
}
8989
}
90+
}
9091
return true;
9192
}
9293

@@ -96,11 +97,6 @@ bool TypeVarVisitor::VisitCallExpr(CallExpr *CE) {
9697
if (FDef == nullptr)
9798
FDef = FD;
9899
if (auto *FVCon = Info.getFuncConstraint(FDef, Context)) {
99-
// if we need to rewrite it but can't (macro, etc), it isn't safe
100-
bool ForcedInconsistent =
101-
!typeArgsProvided(CE) &&
102-
(!Rewriter::isRewritable(CE->getExprLoc()) ||
103-
!canWrite(PersistentSourceLoc::mkPSL(CE, *Context).getFileName()));
104100
// Visit each function argument, and if it use a type variable, insert it
105101
// into the type variable binding map.
106102
unsigned int I = 0;
@@ -113,7 +109,7 @@ bool TypeVarVisitor::VisitCallExpr(CallExpr *CE) {
113109
Expr *Uncast = A->IgnoreImpCasts();
114110
std::set<ConstraintVariable *> CVs =
115111
CR.getExprConstraintVarsSet(Uncast);
116-
insertBinding(CE, TyIdx, Uncast->getType(), CVs, ForcedInconsistent);
112+
insertBinding(CE, TyIdx, Uncast->getType(), CVs);
117113
}
118114
++I;
119115
}
@@ -165,8 +161,13 @@ bool TypeVarVisitor::VisitCallExpr(CallExpr *CE) {
165161
// the exact type variable is identified by the call expression where it is
166162
// used and the index of the type variable type in the function declaration.
167163
void TypeVarVisitor::insertBinding(CallExpr *CE, const int TyIdx,
168-
clang::QualType Ty, CVarSet &CVs,
169-
bool ForceInconsistent) {
164+
clang::QualType Ty, CVarSet &CVs) {
165+
// if we need to rewrite it but can't (macro, etc), it isn't safe
166+
bool ForceInconsistent =
167+
!typeArgsProvided(CE) &&
168+
(!Rewriter::isRewritable(CE->getExprLoc()) ||
169+
!canWrite(PersistentSourceLoc::mkPSL(CE, *Context).getFileName()));
170+
170171
assert(TyIdx >= 0 &&
171172
"Creating a type variable binding without a type variable.");
172173
auto &CallTypeVarMap = TVMap[CE];

clang/test/3C/canwrite_constraints.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,17 @@ void unwritable_func_with_itype_and_bounds(int *p
7171
// expected-warning@+1 {{Declaration in non-writable file}}
7272
_Itype_for_any(T) void my_generic_function(void *p : itype(_Ptr<T>));
7373

74+
// expected-warning@+1 {{Declaration in non-writable file}}
75+
_Itype_for_any(T) void *my_generic_return(void) : itype(_Ptr<T>);
76+
7477
void unwritable_type_argument() {
7578
int i;
79+
int *b; // expected-warning {{Declaration in non-writable file}}
7680
// This warning relates to the atom representing the temporary pointer of
7781
// `&i`. https://github.com/correctcomputation/checkedc-clang/issues/618 would
7882
// make 3C smarter to avoid the need to constrain the temporary pointer.
7983
// expected-warning@+1 {{Expression in non-writable file}}
8084
my_generic_function(&i);
85+
86+
b = my_generic_return(); // expected-warning {{Expression in non-writable file}}
8187
}

0 commit comments

Comments
 (0)