From 7b18bf978d841f4e4629ef777120a30c35b3b9e6 Mon Sep 17 00:00:00 2001 From: "Matt McCutchen (Correct Computation)" Date: Thu, 25 Feb 2021 17:16:11 -0500 Subject: [PATCH] Skip cast insertion in unwritable files with a warning. See #454. --- clang/lib/3C/CastPlacement.cpp | 15 +++++++++++++++ clang/test/3C/canwrite_constraints.h | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/clang/lib/3C/CastPlacement.cpp b/clang/lib/3C/CastPlacement.cpp index 3f88f366474a..fd1db6499ed7 100644 --- a/clang/lib/3C/CastPlacement.cpp +++ b/clang/lib/3C/CastPlacement.cpp @@ -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 diff --git a/clang/test/3C/canwrite_constraints.h b/clang/test/3C/canwrite_constraints.h index 53e1346d8320..ffd4d706c6b0 100644 --- a/clang/test/3C/canwrite_constraints.h +++ b/clang/test/3C/canwrite_constraints.h @@ -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)>)) { + // expected-warning@+1 {{Declaration in non-writable file}} + int *p = 0; + // Now 3C thinks it needs to insert _Assume_bounds_cast<_Ptr> 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); +}