clang 19.0.0git
Public Member Functions | List of all members
clang::tooling::FileMatchTrie Class Reference

A trie to efficiently match against the entries of the compilation database in order of matching suffix length. More...

#include "clang/Tooling/FileMatchTrie.h"

Public Member Functions

 FileMatchTrie ()
 
 FileMatchTrie (PathComparator *Comparator)
 Construct a new FileMatchTrie with the given PathComparator.
 
 ~FileMatchTrie ()
 
void insert (StringRef NewPath)
 Insert a new absolute path. Relative paths are ignored.
 
StringRef findEquivalent (StringRef FileName, raw_ostream &Error) const
 Finds the corresponding file in this trie.
 

Detailed Description

A trie to efficiently match against the entries of the compilation database in order of matching suffix length.

When a clang tool is supposed to operate on a specific file, we have to find the corresponding file in the compilation database. Although entries in the compilation database are keyed by filename, a simple string match is insufficient because of symlinks. Commonly, a project hierarchy looks like this: /<project-root>/src/<path>/<somefile>.cc (used as input for the tool) /<project-root>/build/<symlink-to-src>/<path>/<somefile>.cc (stored in DB)

Furthermore, there might be symlinks inside the source folder or inside the database, so that the same source file is translated with different build options.

For a given input file, the FileMatchTrie finds its entries in order of matching suffix length. For each suffix length, there might be one or more entries in the database. For each of those entries, it calls llvm::sys::fs::equivalent() (injected as PathComparator). There might be zero or more entries with the same matching suffix length that are equivalent to the input file. Three cases are distinguished: 0 equivalent files: Continue with the next suffix length. 1 equivalent file: Best match found, return it. >1 equivalent files: Match is ambiguous, return error.

Definition at line 56 of file FileMatchTrie.h.

Constructor & Destructor Documentation

◆ FileMatchTrie() [1/2]

FileMatchTrie::FileMatchTrie ( )

Definition at line 182 of file FileMatchTrie.cpp.

◆ FileMatchTrie() [2/2]

FileMatchTrie::FileMatchTrie ( PathComparator Comparator)

Construct a new FileMatchTrie with the given PathComparator.

The FileMatchTrie takes ownership of 'Comparator'. Used for testing.

Definition at line 185 of file FileMatchTrie.cpp.

◆ ~FileMatchTrie()

FileMatchTrie::~FileMatchTrie ( )

Definition at line 188 of file FileMatchTrie.cpp.

Member Function Documentation

◆ findEquivalent()

StringRef FileMatchTrie::findEquivalent ( StringRef  FileName,
raw_ostream &  Error 
) const

Finds the corresponding file in this trie.

Returns file name stored in this trie that is equivalent to 'FileName' according to 'Comparator', if it can be uniquely identified. If there are no matches an empty StringRef is returned. If there are ambiguous matches, an empty StringRef is returned and a corresponding message written to 'Error'.

Definition at line 196 of file FileMatchTrie.cpp.

References clang::FileName, clang::tooling::FileMatchTrieNode::findEquivalent(), and clang::Result.

Referenced by clang::tooling::JSONCompilationDatabase::getCompileCommands().

◆ insert()

void FileMatchTrie::insert ( StringRef  NewPath)

Insert a new absolute path. Relative paths are ignored.

Definition at line 192 of file FileMatchTrie.cpp.

References clang::tooling::FileMatchTrieNode::insert().


The documentation for this class was generated from the following files: