Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitadd33e4

Browse files
committed
Improve clang error reporting
1 parent7651422 commitadd33e4

File tree

6 files changed

+170
-25
lines changed

6 files changed

+170
-25
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*
25+
*/
26+
packagejdk.internal.clang;
27+
28+
importjava.util.Arrays;
29+
importjava.util.Map;
30+
importjava.util.NoSuchElementException;
31+
importjava.util.function.Function;
32+
33+
importstaticjava.util.stream.Collectors.toMap;
34+
importstaticjdk.internal.clang.libclang.Index_h.CXError_ASTReadError;
35+
importstaticjdk.internal.clang.libclang.Index_h.CXError_Crashed;
36+
importstaticjdk.internal.clang.libclang.Index_h.CXError_Failure;
37+
importstaticjdk.internal.clang.libclang.Index_h.CXError_InvalidArguments;
38+
importstaticjdk.internal.clang.libclang.Index_h.CXError_Success;
39+
40+
publicenumErrorCode {
41+
Success(CXError_Success),
42+
Failue(CXError_Failure),
43+
Crashed(CXError_Crashed),
44+
InvalidArguments(CXError_InvalidArguments),
45+
ASTReadError(CXError_ASTReadError);
46+
47+
privatefinalintcode;
48+
49+
ErrorCode(intcode) {
50+
this.code =code;
51+
}
52+
53+
publicintcode() {
54+
returncode;
55+
}
56+
57+
privatestaticfinalMap<Integer,ErrorCode>lookup =Arrays.stream(values())
58+
.collect(toMap(ErrorCode::code,Function.identity()));
59+
60+
publicstaticErrorCodevalueOf(intcode) {
61+
returnlookup.computeIfAbsent(code,k -> {thrownewNoSuchElementException("No ErrorCode with code: " +k); });
62+
}
63+
}

‎src/jdk.incubator.jextract/share/classes/jdk/internal/clang/Index.java‎

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@
3030
importjdk.incubator.foreign.MemorySegment;
3131
importjdk.internal.clang.libclang.Index_h;
3232

33+
importjava.lang.invoke.VarHandle;
3334
importjava.nio.file.Path;
3435
importjava.util.ArrayList;
3536
importjava.util.List;
3637
importjava.util.function.Consumer;
3738

39+
importstaticjdk.internal.jextract.impl.LayoutUtils.C_POINTER;
40+
3841
publicclassIndeximplementsAutoCloseable {
3942
// Pointer to CXIndex
4043
privateMemoryAddressptr;
@@ -63,25 +66,40 @@ public static UnsavedFile of(Path path, String contents) {
6366
publicstaticclassParsingFailedExceptionextendsRuntimeException {
6467
privatestaticfinallongserialVersionUID = -1L;
6568
privatefinalPathsrcFile;
69+
privatefinalErrorCodecode;
6670

67-
publicParsingFailedException(PathsrcFile) {
68-
super("Failed to parse " +srcFile.toAbsolutePath().toString());
71+
publicParsingFailedException(PathsrcFile,ErrorCodecode) {
72+
super("Failed to parse " +srcFile.toAbsolutePath().toString() +": " +code);
6973
this.srcFile =srcFile;
74+
this.code =code;
7075
}
7176
}
7277

73-
publicTranslationUnitparseTU(Stringfile,intoptions,String...args)
78+
privatestaticfinalVarHandleVH_MemoryAddress =C_POINTER.varHandle(MemoryAddress.class);
79+
80+
publicTranslationUnitparseTU(Stringfile,Consumer<Diagnostic>dh,intoptions,String...args)
7481
throwsParsingFailedException {
7582
try (MemorySegmentsrc =Utils.toNativeString(file) ;
76-
MemorySegmentcargs =Utils.toNativeStringArray(args)) {
77-
MemoryAddresstu =Index_h.clang_parseTranslationUnit(
78-
ptr,src.baseAddress(),cargs ==null ?MemoryAddress.NULL :cargs.baseAddress(),args.length,MemoryAddress.NULL,0,options);
83+
MemorySegmentcargs =Utils.toNativeStringArray(args);
84+
MemorySegmentoutAddress =MemorySegment.allocateNative(C_POINTER)) {
85+
ErrorCodecode =ErrorCode.valueOf(Index_h.clang_parseTranslationUnit2(
86+
ptr,
87+
src.baseAddress(),
88+
cargs ==null ?MemoryAddress.NULL :cargs.baseAddress(),
89+
args.length,MemoryAddress.NULL,
90+
0,
91+
options,
92+
outAddress.baseAddress()));
93+
94+
MemoryAddresstu = (MemoryAddress)VH_MemoryAddress.get(outAddress.baseAddress());
95+
TranslationUnitrv =newTranslationUnit(tu);
96+
// even if we failed to parse, we might still have diagnostics
97+
rv.processDiagnostics(dh);
7998

80-
if (tu ==null ||tu ==MemoryAddress.NULL) {
81-
thrownewParsingFailedException(Path.of(file).toAbsolutePath());
99+
if (code !=ErrorCode.Success) {
100+
thrownewParsingFailedException(Path.of(file).toAbsolutePath(),code);
82101
}
83102

84-
TranslationUnitrv =newTranslationUnit(tu);
85103
translationUnits.add(rv);
86104
returnrv;
87105
}
@@ -98,14 +116,12 @@ private int defaultOptions(boolean detailedPreprocessorRecord) {
98116

99117
publicTranslationUnitparse(Stringfile,Consumer<Diagnostic>dh,booleandetailedPreprocessorRecord,String...args)
100118
throwsParsingFailedException {
101-
TranslationUnittu =parse(file,detailedPreprocessorRecord,args);
102-
tu.processDiagnostics(dh);
103-
returntu;
119+
returnparseTU(file,dh,defaultOptions(detailedPreprocessorRecord),args);
104120
}
105121

106122
publicTranslationUnitparse(Stringfile,booleandetailedPreprocessorRecord,String...args)
107123
throwsParsingFailedException {
108-
returnparseTU(file,defaultOptions(detailedPreprocessorRecord),args);
124+
returnparse(file,dh -> {},detailedPreprocessorRecord,args);
109125
}
110126

111127
@Override
@@ -122,4 +138,5 @@ public void dispose() {
122138
}
123139
ptr =MemoryAddress.NULL;
124140
}
141+
125142
}

‎src/jdk.incubator.jextract/share/classes/jdk/internal/clang/LibClang.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@
3131

3232
publicclassLibClang {
3333
privatestaticfinalbooleanDEBUG =Boolean.getBoolean("libclang.debug");
34+
privatestaticfinalbooleanNO_CRASH_RECOVERY =Boolean.getBoolean("libclang.disable_crash_recovery");
3435

3536
publicstaticIndexcreateIndex(booleanlocal) {
3637
Indexindex =newIndex(Index_h.clang_createIndex(local ?1 :0,0));
37-
Index_h.clang_toggleCrashRecovery(0);
38-
if (DEBUG) {
38+
Index_h.clang_toggleCrashRecovery(NO_CRASH_RECOVERY ?0 :1);
39+
if (DEBUG &&NO_CRASH_RECOVERY) {
3940
System.err.println("LibClang crash recovery disabled");
4041
}
4142
returnindex;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*
25+
*/
26+
packagejdk.internal.clang;
27+
28+
importjava.util.Arrays;
29+
importjava.util.Map;
30+
importjava.util.NoSuchElementException;
31+
importjava.util.function.Function;
32+
33+
importstaticjava.util.stream.Collectors.toMap;
34+
importstaticjdk.internal.clang.libclang.Index_h.CXSaveError_InvalidTU;
35+
importstaticjdk.internal.clang.libclang.Index_h.CXSaveError_None;
36+
importstaticjdk.internal.clang.libclang.Index_h.CXSaveError_TranslationErrors;
37+
importstaticjdk.internal.clang.libclang.Index_h.CXSaveError_Unknown;
38+
39+
publicenumSaveError {
40+
None(CXSaveError_None),
41+
Unknown(CXSaveError_Unknown),
42+
TranslationErrors(CXSaveError_TranslationErrors),
43+
InvalidTU(CXSaveError_InvalidTU);
44+
45+
privatefinalintcode;
46+
47+
SaveError(intcode) {
48+
this.code =code;
49+
}
50+
51+
publicintcode() {
52+
returncode;
53+
}
54+
55+
privatestaticfinalMap<Integer,SaveError>lookup =Arrays.stream(values())
56+
.collect(toMap(SaveError::code,Function.identity()));
57+
58+
publicstaticSaveErrorvalueOf(intcode) {
59+
returnlookup.computeIfAbsent(code,k -> {thrownewNoSuchElementException("No SaveError with code: " +k); });
60+
}
61+
}

‎src/jdk.incubator.jextract/share/classes/jdk/internal/clang/TranslationUnit.java‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ public Diagnostic[] getDiagnostics() {
6262

6363
publicfinalvoidsave(Pathpath)throwsTranslationUnitSaveException {
6464
try (MemorySegmentpathStr =Utils.toNativeString(path.toAbsolutePath().toString())) {
65-
intres =Index_h.clang_saveTranslationUnit(tu,
66-
pathStr.baseAddress(),0);
67-
if (res !=0) {
65+
SaveErrorres =SaveError.valueOf(Index_h.clang_saveTranslationUnit(tu,pathStr.baseAddress(),0));
66+
if (res !=SaveError.None) {
6867
thrownewTranslationUnitSaveException(path);
6968
}
7069
}
@@ -81,7 +80,7 @@ void processDiagnostics(Consumer<Diagnostic> dh) {
8180
staticlongCONTENTS_OFFSET =Index_h.CXUnsavedFile$LAYOUT.offset(MemoryLayout.PathElement.groupElement("Contents")) /8;
8281
staticlongLENGTH_OFFSET =Index_h.CXUnsavedFile$LAYOUT.offset(MemoryLayout.PathElement.groupElement("Length")) /8;
8382

84-
publicintreparse(Index.UnsavedFile...inMemoryFiles) {
83+
publicvoidreparse(Index.UnsavedFile...inMemoryFiles) {
8584
try (AllocationScopescope =newAllocationScope()) {
8685
MemorySegmentfiles =inMemoryFiles.length ==0 ?
8786
null :
@@ -92,9 +91,15 @@ public int reparse(Index.UnsavedFile... inMemoryFiles) {
9291
Utils.setPointer(start.addOffset(CONTENTS_OFFSET),scope.track(Utils.toNativeString(inMemoryFiles[i].contents)).baseAddress());
9392
Utils.setLong(start.addOffset(LENGTH_OFFSET),inMemoryFiles[i].contents.length());
9493
}
95-
returnIndex_h.clang_reparseTranslationUnit(tu,inMemoryFiles.length,
96-
files ==null ?MemoryAddress.NULL :files.baseAddress(),
97-
Index_h.clang_defaultReparseOptions(tu));
94+
ErrorCodecode =ErrorCode.valueOf(Index_h.clang_reparseTranslationUnit(
95+
tu,
96+
inMemoryFiles.length,
97+
files ==null ?MemoryAddress.NULL :files.baseAddress(),
98+
Index_h.clang_defaultReparseOptions(tu)));
99+
100+
if (code !=ErrorCode.Success) {
101+
thrownewIllegalStateException("Re-parsing failed: " +code);
102+
}
98103
}
99104
}
100105

‎src/jdk.incubator.jextract/share/classes/jdk/internal/jextract/impl/MacroParserImpl.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ Optional<Macro> eval(String macroName, String... tokens) {
8080
returnresult.success() ?
8181
Optional.of((Macro)result) :
8282
Optional.empty();
83-
}catch (Throwableex) {
84-
// This ate the NPE and cause skip of macros
85-
// Why are we expecting exception here? Simply be defensive?
83+
}catch (BadMacroExceptionex) {
8684
if (JextractTaskImpl.VERBOSE) {
8785
System.err.println("Failed to handle macro " +macroName);
8886
ex.printStackTrace(System.err);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp