clang  8.0.0svn
FileMatchTrie.h
Go to the documentation of this file.
1 //===- FileMatchTrie.h ------------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a match trie to find the matching file in a compilation
11 // database based on a given path in the presence of symlinks.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_TOOLING_FILEMATCHTRIE_H
16 #define LLVM_CLANG_TOOLING_FILEMATCHTRIE_H
17 
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/StringRef.h"
20 #include <memory>
21 
22 namespace clang {
23 namespace tooling {
24 
25 class FileMatchTrieNode;
26 
28  virtual ~PathComparator() = default;
29 
30  virtual bool equivalent(StringRef FileA, StringRef FileB) const = 0;
31 };
32 
33 /// A trie to efficiently match against the entries of the compilation
34 /// database in order of matching suffix length.
35 ///
36 /// When a clang tool is supposed to operate on a specific file, we have to
37 /// find the corresponding file in the compilation database. Although entries
38 /// in the compilation database are keyed by filename, a simple string match
39 /// is insufficient because of symlinks. Commonly, a project hierarchy looks
40 /// like this:
41 /// /<project-root>/src/<path>/<somefile>.cc (used as input for the tool)
42 /// /<project-root>/build/<symlink-to-src>/<path>/<somefile>.cc (stored in DB)
43 ///
44 /// Furthermore, there might be symlinks inside the source folder or inside the
45 /// database, so that the same source file is translated with different build
46 /// options.
47 ///
48 /// For a given input file, the \c FileMatchTrie finds its entries in order
49 /// of matching suffix length. For each suffix length, there might be one or
50 /// more entries in the database. For each of those entries, it calls
51 /// \c llvm::sys::fs::equivalent() (injected as \c PathComparator). There might
52 /// be zero or more entries with the same matching suffix length that are
53 /// equivalent to the input file. Three cases are distinguished:
54 /// 0 equivalent files: Continue with the next suffix length.
55 /// 1 equivalent file: Best match found, return it.
56 /// >1 equivalent files: Match is ambiguous, return error.
58 public:
59  FileMatchTrie();
60 
61  /// Construct a new \c FileMatchTrie with the given \c PathComparator.
62  ///
63  /// The \c FileMatchTrie takes ownership of 'Comparator'. Used for testing.
64  FileMatchTrie(PathComparator* Comparator);
65 
66  ~FileMatchTrie();
67 
68  /// Insert a new absolute path. Relative paths are ignored.
69  void insert(StringRef NewPath);
70 
71  /// Finds the corresponding file in this trie.
72  ///
73  /// Returns file name stored in this trie that is equivalent to 'FileName'
74  /// according to 'Comparator', if it can be uniquely identified. If there
75  /// are no matches an empty \c StringRef is returned. If there are ambiguous
76  /// matches, an empty \c StringRef is returned and a corresponding message
77  /// written to 'Error'.
78  StringRef findEquivalent(StringRef FileName,
79  raw_ostream &Error) const;
80 
81 private:
82  FileMatchTrieNode *Root;
83  std::unique_ptr<PathComparator> Comparator;
84 };
85 
86 } // namespace tooling
87 } // namespace clang
88 
89 #endif // LLVM_CLANG_TOOLING_FILEMATCHTRIE_H
virtual ~PathComparator()=default
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
A trie to efficiently match against the entries of the compilation database in order of matching suff...
Definition: FileMatchTrie.h:57
virtual bool equivalent(StringRef FileA, StringRef FileB) const =0
Dataflow Directional Tag Classes.
A node of the FileMatchTrie.