ACAV f0ba4b7c9529
Abstract Syntax Tree (AST) visualization tool for C, C++, and Objective-C
Loading...
Searching...
No Matches
ClangUtils.h File Reference

Utilities for interacting with Clang at runtime This includes runtime detection of Clang paths and AST operations. More...

#include <clang/Basic/Diagnostic.h>
#include <functional>
#include <memory>
#include <string>
#include <vector>
Include dependency graph for ClangUtils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  acav::DiagnosticMessage

Typedefs

using acav::DiagnosticCallback = std::function<void(const DiagnosticMessage &)>

Functions

std::string acav::getClangResourceDir (const std::string &overrideResourceDir="")
 Get clang resource directory.
std::vector< std::string > acav::buildToolchainAdjustedCommandLine (const std::vector< std::string > &commandLine, const std::string &clangResourceDir, std::string &diagnostic)
 Normalize a Clang command line for ACAV's embedded Clang.
std::unique_ptr< clang::ASTUnit > acav::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 for a given file and it's compile command.
bool acav::saveAst (clang::ASTUnit &astUnit, const std::string &outputPath, std::string &errorMessage)
 Save ast to local file.
std::unique_ptr< clang::ASTUnit > acav::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 > acav::getSourceFilesFromCompilationDatabase (const std::string &compDbPath, std::string &errorMessage)
 Extract source file paths from a compilation database.

Detailed Description

Utilities for interacting with Clang at runtime This includes runtime detection of Clang paths and AST operations.

Definition in file ClangUtils.h.

Typedef Documentation

◆ DiagnosticCallback

using acav::DiagnosticCallback = std::function<void(const DiagnosticMessage &)>

Definition at line 50 of file ClangUtils.h.

Function Documentation

◆ buildToolchainAdjustedCommandLine()

std::vector< std::string > acav::buildToolchainAdjustedCommandLine ( const std::vector< std::string > & commandLine,
const std::string & clangResourceDir,
std::string & diagnostic )

Normalize a Clang command line for ACAV's embedded Clang.

This removes stale resource-dir flags, forces ACAV's resource dir, and on macOS adds the active SDK sysroot when the compile command does not already specify one. Standard library, system include, and framework discovery is left to Clang's driver/tooling APIs.

Definition at line 387 of file ClangUtils.cpp.

◆ createAstFromCDB()

std::unique_ptr< clang::ASTUnit > acav::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 for a given file and it's compile command.

There are several steps:

  1. Load compilation database and handle response file
  2. Acquire the compile command for the given file
  3. Inject the clang resource directory to compile command
  4. Get clang::ASTUnit using CreateASTUnitFromCommandLine
    Returns
    unique_ptr of type ASTUnit. nullptr if any error occured

Definition at line 454 of file ClangUtils.cpp.

◆ getClangResourceDir()

std::string acav::getClangResourceDir ( const std::string & overrideResourceDir = "")

Get clang resource directory.

Discovery order:

  1. Use overrideResourceDir if provided
  2. Check for bundled directory: ../lib/clang/<version>/ relative to exe
  3. Fallback: query system clang++ -print-resource-dir
Parameters
overrideResourceDirOptional explicit path to use
Returns
clang resource directory path if found, empty string on failure

Definition at line 312 of file ClangUtils.cpp.

◆ getSourceFilesFromCompilationDatabase()

std::vector< std::string > acav::getSourceFilesFromCompilationDatabase ( const std::string & compDbPath,
std::string & errorMessage )

Extract source file paths from a compilation database.

Parameters
compDbPathPath to compile_commands.json
errorMessageOutput parameter for error details
Returns
List of source file paths, or empty vector on error

Thread-safe: Can be called from any thread

Definition at line 779 of file ClangUtils.cpp.

◆ loadAstFromFile()

std::unique_ptr< clang::ASTUnit > acav::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.

Parameters
astFilePathPath to the .ast file
errorMessageOutput parameter for error details
compilationDbPathPath to compile_commands.json for C++20 module resolution. If empty, module resolution may fail for module imports.
sourcePathSource file path to extract module mappings for. Required if compilationDbPath is provided.

Definition at line 645 of file ClangUtils.cpp.

◆ saveAst()

bool acav::saveAst ( clang::ASTUnit & astUnit,
const std::string & outputPath,
std::string & errorMessage )

Save ast to local file.

Definition at line 634 of file ClangUtils.cpp.