Skip to content

Commit 7b18bf9

Browse files
Skip cast insertion in unwritable files with a warning.
See #454.
1 parent 54d1bfd commit 7b18bf9

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

clang/lib/3C/CastPlacement.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,21 @@ CastPlacementVisitor::getCastString(ConstraintVariable *Dst,
177177

178178
void CastPlacementVisitor::surroundByCast(ConstraintVariable *Dst,
179179
CastNeeded CastKind, Expr *E) {
180+
PersistentSourceLoc PSL = PersistentSourceLoc::mkPSL(E, *Context);
181+
if (!canWrite(PSL.getFileName())) {
182+
// 3C has known bugs that can cause attempted cast insertion in
183+
// unwritable files in common use cases. Until they are fixed, report a
184+
// warning rather than letting the main "unwritable change" error trigger
185+
// later.
186+
clang::DiagnosticsEngine &DE = Writer.getSourceMgr().getDiagnostics();
187+
unsigned ErrorId = DE.getCustomDiagID(
188+
DiagnosticsEngine::Warning,
189+
"3C internal error: tried to insert a cast into an unwritable file "
190+
"(https://github.com/correctcomputation/checkedc-clang/issues/454)");
191+
DE.Report(E->getBeginLoc(), ErrorId);
192+
return;
193+
}
194+
180195
auto CastStrs = getCastString(Dst, CastKind);
181196

182197
// If E is already a cast expression, we will try to rewrite the cast instead

clang/test/3C/canwrite_constraints.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,17 @@ int *foo_var = ((void *)0);
1919
// dedicated test for it.
2020
inline void no_op() {}
2121
// CHECK_HIGHER: inline void no_op() _Checked {}
22+
23+
// Test the unwritable cast internal warning
24+
// (https://github.com/correctcomputation/checkedc-clang/issues/454) using the
25+
// known bug with itypes and function pointers
26+
// (https://github.com/correctcomputation/checkedc-clang/issues/423) as an
27+
// example.
28+
void unwritable_cast(void ((*g)(int *q)) : itype(_Ptr<void(_Ptr<int>)>)) {
29+
// expected-warning@+1 {{Declaration in non-writable file}}
30+
int *p = 0;
31+
// Now 3C thinks it needs to insert _Assume_bounds_cast<_Ptr<int>> around `p`
32+
// because it forgets that it is allowed to use the original type of `g`.
33+
// expected-warning@+1 {{3C internal error: tried to insert a cast into an unwritable file}}
34+
(*g)(p);
35+
}

0 commit comments

Comments
 (0)