summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clangd/IncludeFixer.h
diff options
context:
space:
mode:
authorEric Liu <ioeric@google.com>2019-01-28 14:01:55 +0000
committerEric Liu <ioeric@google.com>2019-01-28 14:01:55 +0000
commitdd66277c36af442460c60dc993b41b831ee28973 (patch)
treea553ff33620d144f505a9b64f6bd0233727c6327 /clang-tools-extra/clangd/IncludeFixer.h
parent6d5348cca557fe16675bdcafe504103c530f2aab (diff)
downloadbcm5719-llvm-dd66277c36af442460c60dc993b41b831ee28973.tar.gz
bcm5719-llvm-dd66277c36af442460c60dc993b41b831ee28973.zip
[clangd] Suggest adding missing includes for incomplete type diagnostics.
Summary: This enables clangd to intercept compiler diagnostics and attach fixes (e.g. by querying index). This patch adds missing includes for incomplete types e.g. member access into class with only forward declaration. This would allow adding missing includes for user-typed symbol names that are missing declarations (e.g. typos) in the future. Reviewers: sammccall Reviewed By: sammccall Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, mgrang, arphaman, kadircet, cfe-commits Differential Revision: https://reviews.llvm.org/D56903 llvm-svn: 352361
Diffstat (limited to 'clang-tools-extra/clangd/IncludeFixer.h')
-rw-r--r--clang-tools-extra/clangd/IncludeFixer.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/clang-tools-extra/clangd/IncludeFixer.h b/clang-tools-extra/clangd/IncludeFixer.h
new file mode 100644
index 00000000000..740710cf4ea
--- /dev/null
+++ b/clang-tools-extra/clangd/IncludeFixer.h
@@ -0,0 +1,54 @@
+//===--- IncludeFixer.h ------------------------------------------*- C++-*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INCLUDE_FIXER_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INCLUDE_FIXER_H
+
+#include "Diagnostics.h"
+#include "Headers.h"
+#include "index/Index.h"
+#include "clang/AST/Type.h"
+#include "clang/Basic/Diagnostic.h"
+#include "llvm/ADT/StringRef.h"
+#include <memory>
+
+namespace clang {
+namespace clangd {
+
+/// Attempts to recover from error diagnostics by suggesting include insertion
+/// fixes. For example, member access into incomplete type can be fixes by
+/// include headers with the definition.
+class IncludeFixer {
+public:
+ IncludeFixer(llvm::StringRef File, std::shared_ptr<IncludeInserter> Inserter,
+ const SymbolIndex &Index, unsigned IndexRequestLimit)
+ : File(File), Inserter(std::move(Inserter)), Index(Index),
+ IndexRequestLimit(IndexRequestLimit) {}
+
+ /// Returns include insertions that can potentially recover the diagnostic.
+ std::vector<Fix> fix(DiagnosticsEngine::Level DiagLevel,
+ const clang::Diagnostic &Info) const;
+
+private:
+ /// Attempts to recover diagnostic caused by an incomplete type \p T.
+ std::vector<Fix> fixIncompleteType(const Type &T) const;
+
+ /// Generates header insertion fixes for \p Sym.
+ std::vector<Fix> fixesForSymbol(const Symbol &Sym) const;
+
+ std::string File;
+ std::shared_ptr<IncludeInserter> Inserter;
+ const SymbolIndex &Index;
+ const unsigned IndexRequestLimit; // Make at most 5 index requests.
+ mutable unsigned IndexRequestCount = 0;
+};
+
+} // namespace clangd
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INCLUDE_FIXER_H
OpenPOWER on IntegriCloud