Skip to content

Skip cast insertion in unwritable files with a warning. #455

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

Merged
merged 1 commit into from
Feb 26, 2021
Merged
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
15 changes: 15 additions & 0 deletions clang/lib/3C/CastPlacement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,21 @@ CastPlacementVisitor::getCastString(ConstraintVariable *Dst,

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

auto CastStrs = getCastString(Dst, CastKind);

// If E is already a cast expression, we will try to rewrite the cast instead
Expand Down
14 changes: 14 additions & 0 deletions clang/test/3C/canwrite_constraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,17 @@ int *foo_var = ((void *)0);
// dedicated test for it.
inline void no_op() {}
// CHECK_HIGHER: inline void no_op() _Checked {}

// Test the unwritable cast internal warning
// (https://github.com/correctcomputation/checkedc-clang/issues/454) using the
// known bug with itypes and function pointers
// (https://github.com/correctcomputation/checkedc-clang/issues/423) as an
// example.
void unwritable_cast(void ((*g)(int *q)) : itype(_Ptr<void(_Ptr<int>)>)) {
// expected-warning@+1 {{Declaration in non-writable file}}
int *p = 0;
// Now 3C thinks it needs to insert _Assume_bounds_cast<_Ptr<int>> around `p`
// because it forgets that it is allowed to use the original type of `g`.
// expected-warning@+1 {{3C internal error: tried to insert a cast into an unwritable file}}
(*g)(p);
}