diff options
author | Jordan Rupprecht <rupprecht@google.com> | 2019-05-14 21:58:59 +0000 |
---|---|---|
committer | Jordan Rupprecht <rupprecht@google.com> | 2019-05-14 21:58:59 +0000 |
commit | 46054fed6aeeabea27b9ba4a3ef81ab5ff9b9645 (patch) | |
tree | d12279f80b5729d0324f066002c838baa736fbd2 /clangd/Headers.h | |
parent | 5026a9a16d10a2edf09be54c7225f49b5789c69e (diff) | |
parent | 0eb1ac6d1df856f065717226ef34d00679a211fe (diff) |
Creating branches/google/stable and tags/google/stable/2019-05-14 from r360103upstream/stable
git-svn-id: https://llvm.org/svn/llvm-project/clang-tools-extra/branches/google/stable@360714 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'clangd/Headers.h')
-rw-r--r-- | clangd/Headers.h | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/clangd/Headers.h b/clangd/Headers.h index 6f0eb807..f2eaf081 100644 --- a/clangd/Headers.h +++ b/clangd/Headers.h @@ -1,9 +1,8 @@ //===--- Headers.h - Include headers -----------------------------*- C++-*-===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// @@ -13,10 +12,12 @@ #include "Path.h" #include "Protocol.h" #include "SourceCode.h" +#include "index/Symbol.h" #include "clang/Format/Format.h" #include "clang/Lex/HeaderSearch.h" #include "clang/Lex/PPCallbacks.h" #include "clang/Tooling/Inclusions/HeaderIncludes.h" +#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" #include "llvm/Support/Error.h" @@ -38,6 +39,15 @@ struct HeaderFile { bool valid() const; }; +/// Creates a `HeaderFile` from \p Header which can be either a URI or a literal +/// include. +llvm::Expected<HeaderFile> toHeaderFile(llvm::StringRef Header, + llvm::StringRef HintPath); + +// Returns include headers for \p Sym sorted by popularity. If two headers are +// equally popular, prefer the shorter one. +llvm::SmallVector<llvm::StringRef, 1> getRankedIncludes(const Symbol &Sym); + // An #include directive that we found in the main file. struct Inclusion { Range R; // Inclusion range. @@ -109,9 +119,12 @@ collectIncludeStructureCallback(const SourceManager &SM, IncludeStructure *Out); // Calculates insertion edit for including a new header in a file. class IncludeInserter { public: + // If \p HeaderSearchInfo is nullptr (e.g. when compile command is + // infeasible), this will only try to insert verbatim headers, and + // include path of non-verbatim header will not be shortened. IncludeInserter(StringRef FileName, StringRef Code, const format::FormatStyle &Style, StringRef BuildDir, - HeaderSearch &HeaderSearchInfo) + HeaderSearch *HeaderSearchInfo) : FileName(FileName), Code(Code), BuildDir(BuildDir), HeaderSearchInfo(HeaderSearchInfo), Inserter(FileName, Code, Style.IncludeStyle) {} @@ -124,25 +137,22 @@ public: /// in \p Inclusions (including those included via different paths). /// - \p DeclaringHeader or \p InsertedHeader is the same as \p File. /// - /// \param DeclaringHeader is the original header corresponding to \p + /// \param DeclaringHeader is path of the original header corresponding to \p /// InsertedHeader e.g. the header that declares a symbol. /// \param InsertedHeader The preferred header to be inserted. This could be /// the same as DeclaringHeader but must be provided. - bool shouldInsertInclude(const HeaderFile &DeclaringHeader, + bool shouldInsertInclude(PathRef DeclaringHeader, const HeaderFile &InsertedHeader) const; /// Determines the preferred way to #include a file, taking into account the /// search path. Usually this will prefer a shorter representation like /// 'Foo/Bar.h' over a longer one like 'Baz/include/Foo/Bar.h'. /// - /// \param DeclaringHeader is the original header corresponding to \p - /// InsertedHeader e.g. the header that declares a symbol. /// \param InsertedHeader The preferred header to be inserted. This could be /// the same as DeclaringHeader but must be provided. /// /// \return A quoted "path" or <path> to be included. - std::string calculateIncludePath(const HeaderFile &DeclaringHeader, - const HeaderFile &InsertedHeader) const; + std::string calculateIncludePath(const HeaderFile &InsertedHeader) const; /// Calculates an edit that inserts \p VerbatimHeader into code. If the header /// is already included, this returns None. @@ -152,7 +162,7 @@ private: StringRef FileName; StringRef Code; StringRef BuildDir; - HeaderSearch &HeaderSearchInfo; + HeaderSearch *HeaderSearchInfo = nullptr; llvm::StringSet<> IncludedHeaders; // Both written and resolved. tooling::HeaderIncludes Inserter; // Computers insertion replacement. }; |