Skip to content

Commit 76e5061

Browse files
Add -allow-rewrite-failures option to downgrade rewrite errors to warnings. (#461)
While we're here: - Add note about -allow-unwritable-changes to the unwritable change error, and change the existing note to have no location for consistency with the others. - Fix a highly visible typo in a `3c` error message.
1 parent 443c757 commit 76e5061

9 files changed

+67
-21
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ struct _3COptions {
7575

7676
bool DumpUnwritableChanges;
7777
bool AllowUnwritableChanges;
78+
79+
bool AllowRewriteFailures;
7880
};
7981

8082
// The main interface exposed by the 3C to interact with the tool.

clang/include/clang/3C/3CGlobalOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern bool WarnRootCause;
3232
extern bool WarnAllRootCause;
3333
extern bool DumpUnwritableChanges;
3434
extern bool AllowUnwritableChanges;
35+
extern bool AllowRewriteFailures;
3536

3637
#ifdef FIVE_C
3738
extern bool RemoveItypes;

clang/lib/3C/3C.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ std::set<std::string> FilePaths;
5858
bool VerifyDiagnosticOutput;
5959
bool DumpUnwritableChanges;
6060
bool AllowUnwritableChanges;
61+
bool AllowRewriteFailures;
6162

6263
#ifdef FIVE_C
6364
bool RemoveItypes;
@@ -235,6 +236,7 @@ _3CInterface::_3CInterface(const struct _3COptions &CCopt,
235236
VerifyDiagnosticOutput = CCopt.VerifyDiagnosticOutput;
236237
DumpUnwritableChanges = CCopt.DumpUnwritableChanges;
237238
AllowUnwritableChanges = CCopt.AllowUnwritableChanges;
239+
AllowRewriteFailures = CCopt.AllowRewriteFailures;
238240

239241
#ifdef FIVE_C
240242
RemoveItypes = CCopt.RemoveItypes;

clang/lib/3C/RewriteUtils.cpp

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,26 @@ void rewriteSourceRange(Rewriter &R, const CharSourceRange &Range,
162162
// crashing with an assert fail.
163163
if (!RewriteSuccess) {
164164
clang::DiagnosticsEngine &DE = R.getSourceMgr().getDiagnostics();
165-
unsigned ErrorId =
166-
DE.getCustomDiagID(
167-
ErrFail ? DiagnosticsEngine::Error : DiagnosticsEngine::Warning,
168-
"Unable to rewrite converted source range. Intended rewriting: \"%0\"");
169-
auto ErrorBuilder = DE.Report(Range.getBegin(), ErrorId);
170-
ErrorBuilder.AddSourceRange(R.getSourceMgr().getExpansionRange(Range));
171-
ErrorBuilder.AddString(NewText);
165+
bool ReportError = ErrFail && !AllowRewriteFailures;
166+
{
167+
// Put this in a block because Clang only allows one DiagnosticBuilder to
168+
// exist at a time.
169+
unsigned ErrorId = DE.getCustomDiagID(
170+
ReportError ? DiagnosticsEngine::Error : DiagnosticsEngine::Warning,
171+
"Unable to rewrite converted source range. Intended rewriting: \"%0\"");
172+
auto ErrorBuilder = DE.Report(Range.getBegin(), ErrorId);
173+
ErrorBuilder.AddSourceRange(R.getSourceMgr().getExpansionRange(Range));
174+
ErrorBuilder.AddString(NewText);
175+
}
176+
if (ReportError) {
177+
unsigned NoteId = DE.getCustomDiagID(
178+
DiagnosticsEngine::Note,
179+
"you can use the -allow-rewrite-failures option to temporarily "
180+
"downgrade this error to a warning");
181+
// If we pass the location here, the macro call stack gets dumped again,
182+
// which looks silly.
183+
DE.Report(NoteId);
184+
}
172185
}
173186
}
174187

@@ -187,18 +200,28 @@ static void emit(Rewriter &R, ASTContext &C) {
187200
DiagnosticsEngine::Level UnwritableChangeDiagnosticLevel =
188201
AllowUnwritableChanges ? DiagnosticsEngine::Warning
189202
: DiagnosticsEngine::Error;
190-
auto MaybeDumpUnwritableChange = [&]() {
203+
auto PrintExtraUnwritableChangeInfo = [&]() {
204+
DiagnosticsEngine &DE = C.getDiagnostics();
205+
// With -dump-unwritable-changes and not -allow-unwritable-changes, we
206+
// want the -allow-unwritable-changes note before the dump.
207+
if (!DumpUnwritableChanges) {
208+
unsigned DumpNoteId = DE.getCustomDiagID(
209+
DiagnosticsEngine::Note,
210+
"use the -dump-unwritable-changes option to see the new version "
211+
"of the file");
212+
DE.Report(DumpNoteId);
213+
}
214+
if (!AllowUnwritableChanges) {
215+
unsigned AllowNoteId = DE.getCustomDiagID(
216+
DiagnosticsEngine::Note,
217+
"you can use the -allow-unwritable-changes option to temporarily "
218+
"downgrade this error to a warning");
219+
DE.Report(AllowNoteId);
220+
}
191221
if (DumpUnwritableChanges) {
192222
errs() << "=== Beginning of new version of " << FE->getName() << " ===\n";
193223
Buffer->second.write(errs());
194224
errs() << "=== End of new version of " << FE->getName() << " ===\n";
195-
} else {
196-
DiagnosticsEngine &DE = C.getDiagnostics();
197-
unsigned ID = DE.getCustomDiagID(
198-
DiagnosticsEngine::Note,
199-
"use the -dump-unwritable-changes option to see the new version "
200-
"of the file");
201-
DE.Report(SM.translateFileLineCol(FE, 1, 1), ID);
202225
}
203226
};
204227

@@ -213,7 +236,7 @@ static void emit(Rewriter &R, ASTContext &C) {
213236
"is not allowed to write to the file "
214237
"(https://github.com/correctcomputation/checkedc-clang/issues/387)");
215238
DE.Report(SM.translateFileLineCol(FE, 1, 1), ID);
216-
MaybeDumpUnwritableChange();
239+
PrintExtraUnwritableChangeInfo();
217240
continue;
218241
}
219242

@@ -230,7 +253,7 @@ static void emit(Rewriter &R, ASTContext &C) {
230253
"but is not the main file and thus cannot be written in stdout "
231254
"mode");
232255
DE.Report(SM.translateFileLineCol(FE, 1, 1), ID);
233-
MaybeDumpUnwritableChange();
256+
PrintExtraUnwritableChangeInfo();
234257
}
235258
continue;
236259
}

clang/test/3C/base_subdir/canwrite_constraints_symlink.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
// RUN: cd %t.base && 3c -addcr -verify canwrite_constraints_symlink.c --
2424

2525
// expected-error@base_subdir_partial_defn.h:1 {{3C internal error: 3C generated changes to this file even though it is not allowed to write to the file}}
26-
// expected-note@base_subdir_partial_defn.h:1 {{-dump-unwritable-changes}}
26+
// expected-note@*:* {{-dump-unwritable-changes}}
27+
// expected-note@*:* {{-allow-unwritable-changes}}
2728

2829
void
2930
#include "base_subdir_partial_defn.h"

clang/test/3C/base_subdir/canwrite_constraints_unimplemented.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
// RUN: cd %S && 3c -addcr -verify %s --
99

1010
// expected-error@../base_subdir_partial_defn.h:1 {{3C internal error: 3C generated changes to this file even though it is not allowed to write to the file}}
11-
// expected-note@../base_subdir_partial_defn.h:1 {{-dump-unwritable-changes}}
11+
// expected-note@*:* {{-dump-unwritable-changes}}
12+
// expected-note@*:* {{-allow-unwritable-changes}}
1213

1314
// The "../base_subdir_partial_defn.h" path is testing two former base dir
1415
// matching bugs from

clang/test/3C/macro_rewrite_error.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#define args ();
44
typedef int (*a) args // expected-error {{Unable to rewrite converted source range. Intended rewriting: "typedef _Ptr<int (void)> a"}}
55
a b;
6+
// expected-note@*:* {{-allow-rewrite-failures}}
67

78
#define MIDDLE x; int *
89
int MIDDLE y; // expected-error {{Unable to rewrite converted source range. Intended rewriting: "_Ptr<int> y = ((void *)0)"}}
10+
// expected-note@*:* {{-allow-rewrite-failures}}

clang/test/3C/stdout_mode_write_other.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
// RUN: 3c -base-dir=%S -addcr -verify %s --
66

77
// expected-error@base_subdir_partial_defn.h:1 {{3C generated changes to this file, which is under the base dir but is not the main file and thus cannot be written in stdout mode}}
8-
// expected-note@base_subdir_partial_defn.h:1 {{-dump-unwritable-changes}}
8+
// expected-note@*:* {{-dump-unwritable-changes}}
9+
// expected-note@*:* {{-allow-unwritable-changes}}
910

1011
void
1112
#include "base_subdir_partial_defn.h"

clang/tools/3c/3CStandalone.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,18 @@ static cl::opt<bool> OptAllowUnwritableChanges(
226226
"may be removed in the future."),
227227
cl::init(false), cl::cat(_3CCategory));
228228

229+
static cl::opt<bool> OptAllowRewriteFailures(
230+
"allow-rewrite-failures",
231+
cl::desc("When 3c fails to make a rewrite to a source file (typically "
232+
"because of macros), issue a warning instead of an error. This "
233+
"option is intended to be used temporarily until you change your "
234+
"code to allow 3c to work or you report the problem to the 3C "
235+
"team to get it fixed; the option may be removed in the future. "
236+
"Note that some kinds of rewrite failures currently generate "
237+
"warnings regardless of this option, due to known bugs that "
238+
"affect common use cases."),
239+
cl::init(false), cl::cat(_3CCategory));
240+
229241
#ifdef FIVE_C
230242
static cl::opt<bool> OptRemoveItypes(
231243
"remove-itypes",
@@ -272,6 +284,7 @@ int main(int argc, const char **argv) {
272284
CcOptions.VerifyDiagnosticOutput = OptVerifyDiagnosticOutput;
273285
CcOptions.DumpUnwritableChanges = OptDumpUnwritableChanges;
274286
CcOptions.AllowUnwritableChanges = OptAllowUnwritableChanges;
287+
CcOptions.AllowRewriteFailures = OptAllowRewriteFailures;
275288

276289
#ifdef FIVE_C
277290
CcOptions.RemoveItypes = OptRemoveItypes;
@@ -335,7 +348,7 @@ int main(int argc, const char **argv) {
335348

336349
// Write all the converted files back.
337350
if (!_3CInterface.writeAllConvertedFilesToDisk()) {
338-
errs() << "Failure occurred while trying to rewrite converted files back."
351+
errs() << "Failure occurred while trying to rewrite converted files back. "
339352
"Exiting.\n";
340353
return 1;
341354
}

0 commit comments

Comments
 (0)