1 /**
2  * Copyright: Copyright (c) 2011 Jacob Carlborg. All rights reserved.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: Oct 6, 2011
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 
8 module clang.Diagnostic;
9 
10 import std.typecons : RefCounted;
11 
12 import clang.c.Index;
13 import clang.Util;
14 
15 struct Diagnostic {
16     mixin CX;
17 
18     /**
19      * Trusted: on the assumption that clang_formatDiagnostic is implemented by
20      * the competent LLVM team.
21      */
22     string format(uint options = clang_defaultDiagnosticDisplayOptions) @trusted {
23         return toD(clang_formatDiagnostic(cx, options));
24     }
25 
26     /**
27      * Trusted: on the assumption that the LLVM team is competent.
28      */
29     @property CXDiagnosticSeverity severity() @trusted {
30         return clang_getDiagnosticSeverity(cx);
31     }
32 
33     @property toString() {
34         return format;
35     }
36 }
37 
38 struct DiagnosticSet {
39     private struct Container {
40         CXDiagnosticSet set;
41 
42         ~this() {
43             if (set != null) {
44                 clang_disposeDiagnosticSet(set);
45             }
46         }
47     }
48 
49     private RefCounted!Container container;
50     private size_t begin;
51     private size_t end;
52 
53     private static RefCounted!Container makeContainer(CXDiagnosticSet set) {
54         RefCounted!Container result;
55         result.set = set;
56         return result;
57     }
58 
59     private this(RefCounted!Container container, size_t begin, size_t end) {
60         this.container = container;
61         this.begin = begin;
62         this.end = end;
63     }
64 
65     this(CXDiagnosticSet set) {
66         container = makeContainer(set);
67         begin = 0;
68         end = clang_getNumDiagnosticsInSet(container.set);
69     }
70 
71     @property bool empty() const {
72         return begin >= end;
73     }
74 
75     @property Diagnostic front() {
76         return Diagnostic(clang_getDiagnosticInSet(container.set, cast(uint) begin));
77     }
78 
79     @property Diagnostic back() {
80         return Diagnostic(clang_getDiagnosticInSet(container.set, cast(uint)(end - 1)));
81     }
82 
83     @property void popFront() {
84         ++begin;
85     }
86 
87     @property void popBack() {
88         --end;
89     }
90 
91     @property DiagnosticSet save() {
92         return this;
93     }
94 
95     @property size_t length() const {
96         return end - begin;
97     }
98 
99     Diagnostic opIndex(size_t index) {
100         return Diagnostic(clang_getDiagnosticInSet(container.set, cast(uint)(begin + index)));
101     }
102 
103     DiagnosticSet opSlice(size_t begin, size_t end) {
104         return DiagnosticSet(container, this.begin + begin, this.begin + end);
105     }
106 
107     size_t opDollar() const {
108         return length;
109     }
110 }