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 package jdk.internal.clang;
27 
28 import jdk.incubator.foreign.MemoryAddress;
29 import jdk.incubator.foreign.ResourceScope;
30 import jdk.incubator.foreign.SegmentAllocator;
31 import jdk.internal.clang.libclang.Index_h;
32 
33 import static jdk.internal.clang.LibClang.IMPLICIT_ALLOCATOR;
34 
35 public class Diagnostic {
36     final MemoryAddress ptr;
37 
38     // Various Diagnostic severity levels - from Clang enum CXDiagnosticSeverity
39 
40     /**
41      * A diagnostic that has been suppressed, e.g., by a command-line
42      * option.
43      */
44     public static final int CXDiagnostic_Ignored = Index_h.CXDiagnostic_Ignored();
45 
46     /**
47      * This diagnostic is a note that should be attached to the
48      * previous (non-note) diagnostic.
49      */
50     public static final int CXDiagnostic_Note    = Index_h.CXDiagnostic_Note();
51 
52     /**
53      * This diagnostic indicates suspicious code that may not be
54      * wrong.
55      */
56     public static final int CXDiagnostic_Warning = Index_h.CXDiagnostic_Warning();
57 
58     /**
59      * This diagnostic indicates that the code is ill-formed.
60      */
61     public static final int CXDiagnostic_Error   = Index_h.CXDiagnostic_Error();
62 
63     /**
64      * This diagnostic indicates that the code is ill-formed such
65      * that future parser recovery is unlikely to produce useful
66      * results.
67      */
68     public static final int CXDiagnostic_Fatal   = Index_h.CXDiagnostic_Fatal();
69 
70     Diagnostic(MemoryAddress ptr) {
71         this.ptr = ptr;
72     }
73 
74     public int severity() {
75         return Index_h.clang_getDiagnosticSeverity(ptr);
76     }
77 
78     public SourceLocation location() {
79         return new SourceLocation(Index_h.clang_getDiagnosticLocation(IMPLICIT_ALLOCATOR, ptr));
80     }
81 
82     public String spelling() {
83        return LibClang.CXStrToString(allocator -> Index_h.clang_getDiagnosticSpelling(allocator, ptr));
84     }
85 
86     public void dispose() {
87         Index_h.clang_disposeDiagnostic(ptr);
88     }
89 
90     @Override
91     public String toString() {
92         return LibClang.CXStrToString(allocator ->
93             Index_h.clang_formatDiagnostic(allocator, ptr,
94                 Index_h.clang_defaultDiagnosticDisplayOptions()));
95     }
96 }