1 /**
2 Copyright: Copyright (c) 2017-2021, 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 libclang_ast.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) @trusted {
19     import clang.c.Index : CXDiagnosticSeverity;
20 
21     if (!tu.isValid)
22         return true;
23 
24     auto dia = tu.diagnostics;
25 
26     foreach (diag; dia) {
27         auto severity = diag.severity;
28 
29         final switch (severity) with (CXDiagnosticSeverity) {
30         case ignored:
31         case note:
32         case warning:
33             break;
34         case error:
35         case fatal:
36             return true;
37         }
38     }
39     return false;
40 }
41 
42 /** Log diagnostic error messages to std.logger.
43  *
44  * TODO Change to a template with a sink as parameter.
45  */
46 void logDiagnostic(ref TranslationUnit tu) @trusted {
47     import logger = std.experimental.logger;
48     import clang.c.Index : CXDiagnosticSeverity;
49 
50     auto dia = tu.diagnostics;
51 
52     foreach (diag; dia) {
53         auto severity = diag.severity;
54 
55         final switch (severity) with (CXDiagnosticSeverity) {
56         case ignored:
57             logger.info(diag.format);
58             break;
59         case note:
60             logger.info(diag.format);
61             break;
62         case warning:
63             logger.warning(diag.format);
64             break;
65         case error:
66             logger.error(diag.format);
67             break;
68         case fatal:
69             logger.error(diag.format);
70             break;
71         }
72     }
73 }