1 //===-- CanonicalIncludes.h - remap #include header -------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // At indexing time, we decide which file to #included for a symbol. 10 // Usually this is the file with the canonical decl, but there are exceptions: 11 // - private headers may have pragmas pointing to the matching public header. 12 // (These are "IWYU" pragmas, named after the include-what-you-use tool). 13 // - the standard library is implemented in many files, without any pragmas. 14 // We have a lookup table for common standard library implementations. 15 // libstdc++ puts char_traits in bits/char_traits.h, but we #include <string>. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_CANONICALINCLUDES_H 20 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_CANONICALINCLUDES_H 21 22 #include "clang/Basic/FileEntry.h" 23 #include "clang/Basic/LangOptions.h" 24 #include "llvm/ADT/StringMap.h" 25 #include "llvm/ADT/StringRef.h" 26 27 namespace clang { 28 namespace clangd { 29 30 /// Maps a definition location onto an #include file, based on a set of filename 31 /// rules. 32 /// Only const methods (i.e. mapHeader) in this class are thread safe. 33 class CanonicalIncludes { 34 public: 35 /// Returns the overridden verbatim spelling for files in \p Header that can 36 /// be directly included (i.e., contains quotes "" or angled brackets <>), or 37 /// "" if the spelling could not be found. 38 llvm::StringRef mapHeader(llvm::StringRef HeaderPath) const; 39 40 /// Adds mapping for system headers and some special symbols (e.g. STL symbols 41 /// in <iosfwd> need to be mapped individually). Approximately, the following 42 /// system headers are handled: 43 /// - C++ standard library e.g. bits/basic_string.h$ -> <string> 44 /// - Posix library e.g. bits/pthreadtypes.h$ -> <pthread.h> 45 /// - Compiler extensions, e.g. include/avx512bwintrin.h$ -> <immintrin.h> 46 /// The mapping is hardcoded and hand-maintained, so it might not cover all 47 /// headers. 48 void addSystemHeadersMapping(const LangOptions &Language); 49 50 private: 51 /// A map from a suffix (one or components of a path) to a canonical path. 52 /// Used only for mapping standard headers. 53 const llvm::StringMap<llvm::StringRef> *StdSuffixHeaderMapping = nullptr; 54 }; 55 } // namespace clangd 56 } // namespace clang 57 58 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_CANONICALINCLUDES_H 59