1 /**
2 Copyright: Copyright (c) 2017, Joakim Brännström. All rights reserved.
3 License: MPL-2
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 
6 This Source Code Form is subject to the terms of the Mozilla Public License,
7 v.2.0. If a copy of the MPL was not distributed with this file, You can obtain
8 one at http://mozilla.org/MPL/2.0/.
9 */
10 module cpptooling.analyzer.clang.check_parse_result;
11 
12 import clang.TranslationUnit : TranslationUnit;
13 
14 /** Check the context for diagnositc errors.
15  *
16  * Returns: True if errors where found.
17  */
18 bool hasParseErrors(ref TranslationUnit tu) @safe {
19     import clang.c.Index : CXDiagnosticSeverity;
20 
21     if (!tu.isValid)
22         return true;
23 
24     auto dia = tu.diagnostics;
25 
26     auto rval = () @trusted{
27         foreach (diag; dia) {
28             auto severity = diag.severity;
29 
30             final switch (severity) with (CXDiagnosticSeverity) {
31             case ignored:
32             case note:
33             case warning:
34                 break;
35             case error:
36             case fatal:
37                 return true;
38             }
39         }
40         return false;
41     }();
42 
43     return rval;
44 }
45 
46 /** Log diagnostic error messages to std.logger.
47  *
48  * TODO Change to a template with a sink as parameter.
49  */
50 void logDiagnostic(ref TranslationUnit tu) @safe {
51     import logger = std.experimental.logger;
52 
53     import clang.c.Index : CXDiagnosticSeverity;
54 
55     auto dia = tu.diagnostics;
56 
57     () @trusted{
58         foreach (diag; dia) {
59             auto severity = diag.severity;
60 
61             final switch (severity) with (CXDiagnosticSeverity) {
62             case ignored:
63                 logger.info(diag.format);
64                 break;
65             case note:
66                 logger.info(diag.format);
67                 break;
68             case warning:
69                 logger.warning(diag.format);
70                 break;
71             case error:
72                 logger.error(diag.format);
73                 break;
74             case fatal:
75                 logger.error(diag.format);
76                 break;
77             }
78         }
79     }();
80 }