Skip to content

Commit 7e813d1

Browse files
Migrate the difftypes* tests to the diagnostic verifier.
This feels a little cleaner than manually scanning the stderr and lets us check the diagnostics more precisely, for what that's worth. We couldn't do this before #488 both because 3C exited before the diagnostics could be verified and because 3C didn't support custom verify prefixes. #488 solved the first problem, and this PR solves the second.
1 parent 54924ac commit 7e813d1

File tree

5 files changed

+57
-15
lines changed

5 files changed

+57
-15
lines changed

clang/lib/3C/3C.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ class _3CDiagnosticConsumer : public DiagnosticConsumer {
119119
public:
120120
_3CDiagnosticConsumer(DiagnosticsEngine &Engine)
121121
: UnderlyingConsumer(Engine.takeClient()) {
122+
// This code currently only supports the default LibTooling setup in which
123+
// the ClangTool has no global DiagnosticConsumer, so
124+
// CompilerInstance::createDiagnostics creates one owned by the
125+
// DiagnosticsEngine. If we needed to support the case in which the
126+
// DiagnosticConsumer isn't owned, we could use an "UnderlyingConsumerOwner"
127+
// pattern like VerifyDiagnosticConsumer does.
122128
assert(UnderlyingConsumer);
123129
}
124130

@@ -153,7 +159,17 @@ class _3CDiagnosticConsumer : public DiagnosticConsumer {
153159
return UnderlyingConsumer->getNumErrors() == 0;
154160
}
155161

156-
~_3CDiagnosticConsumer() { assert(CurrentState == S_Done); }
162+
~_3CDiagnosticConsumer() {
163+
// We considered asserting that the state is S_Done here, but if
164+
// ASTUnit::LoadFromCompilerInvocation fails and returns null, the
165+
// _3CDiagnosticConsumer may be destructed without reaching S_Done. However,
166+
// even without such an assertion, we're still well protected against
167+
// forgetting to finish verification and missing an error because if the
168+
// ASTUnit is null, _3CInterface::parseASTs will record a "non-diagnostic
169+
// error", while if the ASTUnit is non-null, it will get added to
170+
// _3CInterface::ASTs and determineExitCode will call finish3CAnalysis. (And
171+
// the _3CInterface destructor enforces that determineExitCode is called.)
172+
}
157173
};
158174

159175
// Based on LibTooling's ASTBuilderAction but does several custom things that we

clang/test/3C/difftypes1a.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
//RUN: rm -rf %t*
2-
//RUN: not 3c -base-dir=%S -output-dir=%t.checked %s %S/difftypes1b.c -- 2>%t.stderr
3-
//RUN: grep -q "merging failed" %t.stderr
1+
// Since the RUN commands in difftypes1a.c and difftypes1b.c process the two
2+
// files in different orders and the location where the error is reported
3+
// depends on the order, we need to use a different diagnostic verification
4+
// prefix (and set of corresponding comments) for each RUN command. Verification
5+
// is per translation unit, so the translation unit with no diagnostic needs
6+
// `expected-no-diagnostics`.
47

5-
// The desired behavior in this case is to fail, so other checks are omitted
8+
//RUN: rm -rf %t*
9+
//RUN: 3c -base-dir=%S -output-dir=%t.checked %s %S/difftypes1b.c -- -Xclang -verify=ab-expected
610

11+
// ab-expected-no-diagnostics
12+
// ba-expected-error@+1 {{merging failed for 'foo'}}
713
_Ptr<int> foo(int, char);

clang/test/3C/difftypes1b.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
//RUN: rm -rf %t*
2-
//RUN: not 3c -base-dir=%S -output-dir=%t.checked %s %S/difftypes1a.c -- 2>%t.stderr
3-
//RUN: grep -q "merging failed" %t.stderr
1+
// Since the RUN commands in difftypes1a.c and difftypes1b.c process the two
2+
// files in different orders and the location where the error is reported
3+
// depends on the order, we need to use a different diagnostic verification
4+
// prefix (and set of corresponding comments) for each RUN command. Verification
5+
// is per translation unit, so the translation unit with no diagnostic needs
6+
// `expected-no-diagnostics`.
47

5-
// The desired behavior in this case is to fail, so other checks are omitted
8+
//RUN: rm -rf %t*
9+
//RUN: 3c -base-dir=%S -output-dir=%t.checked %s %S/difftypes1a.c -- -Xclang -verify=ba-expected
610

11+
// ba-expected-no-diagnostics
12+
// ab-expected-error@+1 {{merging failed for 'foo'}}
713
int *foo(int, char *);

clang/test/3C/difftypes2a.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
// Since the RUN commands in difftypes2a.c and difftypes2b.c process the two
2+
// files in different orders and the location where the error is reported
3+
// depends on the order, we need to use a different diagnostic verification
4+
// prefix (and set of corresponding comments) for each RUN command. Verification
5+
// is per translation unit, so the translation unit with no diagnostic needs
6+
// `expected-no-diagnostics`.
7+
18
// RUN: rm -rf %t*
2-
// RUN: not 3c -base-dir=%S -output-dir=%t.checked %s %S/difftypes2b.c -- 2>%t.stderr
3-
// RUN: grep -q "merging failed" %t.stderr
9+
// RUN: 3c -base-dir=%S -output-dir=%t.checked %s %S/difftypes2b.c -- -Xclang -verify=ab-expected
410

511
// The desired behavior in this case is to fail, so other checks are omitted
612

713
// Test no body vs body
814

15+
// ab-expected-no-diagnostics
16+
// ba-expected-error@+1 {{merging failed for 'foo'}}
917
void foo(char *x);

clang/test/3C/difftypes2b.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
// RUN: rm -rf %t*
2-
// RUN: not 3c -base-dir=%S -output-dir=%t.checked %s %S/difftypes2a.c -- 2>%t.stderr
3-
// RUN: grep -q "merging failed" %t.stderr
1+
// Since the RUN commands in difftypes2a.c and difftypes2b.c process the two
2+
// files in different orders and the location where the error is reported
3+
// depends on the order, we need to use a different diagnostic verification
4+
// prefix (and set of corresponding comments) for each RUN command. Verification
5+
// is per translation unit, so the translation unit with no diagnostic needs
6+
// `expected-no-diagnostics`.
47

5-
// The desired behavior in this case is to fail, so other checks are omitted
8+
// RUN: rm -rf %t*
9+
// RUN: 3c -base-dir=%S -output-dir=%t.checked %s %S/difftypes2a.c -- -Xclang -verify=ba-expected
610

711
// Test body vs no body
812

13+
// ba-expected-no-diagnostics
14+
// ab-expected-error@+1 {{merging failed for 'foo'}}
915
void foo(char **y) {
1016
// this is the body
1117
}

0 commit comments

Comments
 (0)