ACAV f0ba4b7c9529
Abstract Syntax Tree (AST) visualization tool for C, C++, and Objective-C
Loading...
Searching...
No Matches
DiagnosticLogFormat.h
Go to the documentation of this file.
1/*$!{
2* Aurora Clang AST Viewer (ACAV)
3*
4* Copyright (c) 2026 Min Liu
5* Copyright (c) 2026 Michael David Adams
6*
7* SPDX-License-Identifier: GPL-2.0-or-later
8*
9* This program is free software; you can redistribute it and/or modify
10* it under the terms of the GNU General Public License as published by
11* the Free Software Foundation; either version 2 of the License, or
12* (at your option) any later version.
13*
14* This program is distributed in the hope that it will be useful,
15* but WITHOUT ANY WARRANTY; without even the implied warranty of
16* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17* GNU General Public License for more details.
18*
19* You should have received a copy of the GNU General Public License along
20* with this program; if not, see <https://www.gnu.org/licenses/>.
21}$!*/
22
25#pragma once
26
27#include "common/ClangUtils.h"
28#include <clang/Basic/Diagnostic.h>
29#include <string>
30
31namespace acav::logfmt {
32
33inline std::string sanitizeField(std::string value) {
34 for (char &ch : value) {
35 if (ch == '\n' || ch == '\r' || ch == '\t') {
36 ch = ' ';
37 }
38 }
39 return value;
40}
41
42inline std::string toLevelString(clang::DiagnosticsEngine::Level level) {
43 switch (level) {
44 case clang::DiagnosticsEngine::Warning:
45 return "warning";
46 case clang::DiagnosticsEngine::Error:
47 return "error";
48 case clang::DiagnosticsEngine::Fatal:
49 return "fatal";
50 case clang::DiagnosticsEngine::Remark:
51 return "remark";
52 case clang::DiagnosticsEngine::Note:
53 return "note";
54 case clang::DiagnosticsEngine::Ignored:
55 default:
56 return "debug";
57 }
58}
59
60inline std::string formatDiagnosticLine(const std::string &level,
61 const std::string &file,
62 unsigned line,
63 unsigned column,
64 const std::string &message) {
65 std::string result = "@diag\t";
66 result += level;
67 result += '\t';
68 result += sanitizeField(file);
69 result += '\t';
70 if (line > 0) {
71 result += std::to_string(line);
72 }
73 result += '\t';
74 if (column > 0) {
75 result += std::to_string(column);
76 }
77 result += '\t';
78 result += sanitizeField(message);
79 return result;
80}
81
82inline std::string formatDiagnosticLine(clang::DiagnosticsEngine::Level level,
83 const std::string &file,
84 unsigned line,
85 unsigned column,
86 const std::string &message) {
87 return formatDiagnosticLine(toLevelString(level), file, line, column, message);
88}
89
90} // namespace acav::logfmt
Utilities for interacting with Clang at runtime This includes runtime detection of Clang paths and AS...