ACAV f0ba4b7c9529
Abstract Syntax Tree (AST) visualization tool for C, C++, and Objective-C
Loading...
Searching...
No Matches
ClangUtils.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
26#pragma once
27
28#include <clang/Basic/Diagnostic.h>
29#include <functional>
30#include <memory>
31#include <string>
32#include <vector>
33
34// Forward declarations
35namespace clang {
36class ASTUnit;
37class DiagnosticsEngine;
38} // namespace clang
39
40namespace acav {
41
43 clang::DiagnosticsEngine::Level level = clang::DiagnosticsEngine::Note;
44 std::string message;
45 std::string file;
46 unsigned line = 0;
47 unsigned column = 0;
48};
49
50using DiagnosticCallback = std::function<void(const DiagnosticMessage &)>;
51
61std::string getClangResourceDir(const std::string &overrideResourceDir = "");
62
69std::vector<std::string>
70buildToolchainAdjustedCommandLine(const std::vector<std::string> &commandLine,
71 const std::string &clangResourceDir,
72 std::string &diagnostic);
73
83std::unique_ptr<clang::ASTUnit>
84createAstFromCDB(const std::string &compilationDatabase,
85 const std::string &sourcePath, std::string &errorMessage,
86 const DiagnosticCallback &diagnosticCallback = nullptr,
87 const std::string &clangResourceDirOverride = "");
88
90bool saveAst(clang::ASTUnit &astUnit, const std::string &outputPath,
91 std::string &errorMessage);
92
100std::unique_ptr<clang::ASTUnit>
101loadAstFromFile(const std::string &astFilePath, std::string &errorMessage,
102 const std::string &compilationDbPath = "",
103 const std::string &sourcePath = "",
104 const DiagnosticCallback &diagnosticCallback = nullptr);
105
112std::vector<std::string>
113getSourceFilesFromCompilationDatabase(const std::string &compDbPath,
114 std::string &errorMessage);
115
116} // namespace acav
std::vector< std::string > getSourceFilesFromCompilationDatabase(const std::string &compDbPath, std::string &errorMessage)
Extract source file paths from a compilation database.
std::unique_ptr< clang::ASTUnit > createAstFromCDB(const std::string &compilationDatabase, const std::string &sourcePath, std::string &errorMessage, const DiagnosticCallback &diagnosticCallback=nullptr, const std::string &clangResourceDirOverride="")
Create AST from a given compilation database This function provides a easy way to generate clang AST ...
bool saveAst(clang::ASTUnit &astUnit, const std::string &outputPath, std::string &errorMessage)
Save ast to local file.
std::string getClangResourceDir(const std::string &overrideResourceDir="")
Get clang resource directory.
std::unique_ptr< clang::ASTUnit > loadAstFromFile(const std::string &astFilePath, std::string &errorMessage, const std::string &compilationDbPath="", const std::string &sourcePath="", const DiagnosticCallback &diagnosticCallback=nullptr)
Load AST from local file.
std::vector< std::string > buildToolchainAdjustedCommandLine(const std::vector< std::string > &commandLine, const std::string &clangResourceDir, std::string &diagnostic)
Normalize a Clang command line for ACAV's embedded Clang.