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

Implementation of Clang utilities and AST operations. More...

#include "common/ClangUtils.h"
#include "common/DiagnosticLogFormat.h"
#include <algorithm>
#include <array>
#include <cctype>
#include <clang/Basic/DiagnosticIDs.h>
#include <clang/Basic/DiagnosticOptions.h>
#include <clang/Basic/SourceLocation.h>
#include <clang/Basic/SourceManager.h>
#include <cstdlib>
#include <clang/Frontend/ASTUnit.h>
#include <clang/Frontend/PCHContainerOperations.h>
#include <clang/Lex/HeaderSearchOptions.h>
#include <clang/Tooling/CompilationDatabase.h>
#include <clang/Tooling/JSONCompilationDatabase.h>
#include <clang/Tooling/Tooling.h>
#include <llvm/ADT/ScopeExit.h>
#include <llvm/ADT/StringExtras.h>
#include <llvm/Config/llvm-config.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/Path.h>
#include <llvm/Support/Program.h>
#include <llvm/Support/raw_ostream.h>
#include <map>
#include <optional>
#include <utility>
Include dependency graph for ClangUtils.cpp:

Go to the source code of this file.

Functions

static std::map< std::string, std::string > acav::extractModuleFileMappings (const std::string &compilationDb, const std::string &sourcePath)
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

Implementation of Clang utilities and AST operations.

Definition in file ClangUtils.cpp.

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.

◆ extractModuleFileMappings()

std::map< std::string, std::string > acav::extractModuleFileMappings ( const std::string & compilationDb,
const std::string & sourcePath )
static

Definition at line 807 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.