blob: e55597f90ed5fb80af38b4a4bdd1b1fb07d0b83e [file] [log] [blame]
Haojian Wu11e9bd22016-05-31 09:31:51 +00001//===-- IncludeFixerContext.h - Include fixer context -----------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Haojian Wu11e9bd22016-05-31 09:31:51 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_INCLUDEFIXERCONTEXT_H
10#define LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_INCLUDEFIXERCONTEXT_H
11
Haojian Wu627ca962016-07-08 09:10:29 +000012#include "find-all-symbols/SymbolInfo.h"
13#include "clang/Tooling/Core/Replacement.h"
Haojian Wu11e9bd22016-05-31 09:31:51 +000014#include <string>
15#include <vector>
16
17namespace clang {
18namespace include_fixer {
19
Dmitri Gribenko282dc722019-08-22 11:32:57 +000020/// A context for a file being processed. It includes all query
Haojian Wuc99f7282016-08-09 08:26:19 +000021/// information, e.g. symbols being queried in database, all header candidates.
Haojian Wu627ca962016-07-08 09:10:29 +000022class IncludeFixerContext {
23public:
Haojian Wu68c34a02016-07-13 16:43:54 +000024 struct HeaderInfo {
Dmitri Gribenko282dc722019-08-22 11:32:57 +000025 /// The header where QualifiedName comes from.
Haojian Wu68c34a02016-07-13 16:43:54 +000026 std::string Header;
Dmitri Gribenko282dc722019-08-22 11:32:57 +000027 /// A symbol name with completed namespace qualifiers which will
Haojian Wu68c34a02016-07-13 16:43:54 +000028 /// replace the original symbol.
29 std::string QualifiedName;
30 };
Haojian Wu5d9482d2016-07-08 14:28:43 +000031
Haojian Wu9e4bd0c2016-07-19 14:49:04 +000032 struct QuerySymbolInfo {
Dmitri Gribenko282dc722019-08-22 11:32:57 +000033 /// The raw symbol name being queried in database. This name might
Haojian Wu9e4bd0c2016-07-19 14:49:04 +000034 /// miss some namespace qualifiers, and will be replaced by a fully
35 /// qualified one.
36 std::string RawIdentifier;
37
Dmitri Gribenko282dc722019-08-22 11:32:57 +000038 /// The qualifiers of the scope in which SymbolIdentifier lookup
Haojian Wu9e4bd0c2016-07-19 14:49:04 +000039 /// occurs. It is represented as a sequence of names and scope resolution
40 /// operatiors ::, ending with a scope resolution operator (e.g. a::b::).
41 /// Empty if SymbolIdentifier is not in a specific scope.
42 std::string ScopedQualifiers;
43
Dmitri Gribenko282dc722019-08-22 11:32:57 +000044 /// The replacement range of RawIdentifier.
Haojian Wu9e4bd0c2016-07-19 14:49:04 +000045 tooling::Range Range;
46 };
47
48 IncludeFixerContext() = default;
Haojian Wuc99f7282016-08-09 08:26:19 +000049 IncludeFixerContext(StringRef FilePath,
50 std::vector<QuerySymbolInfo> QuerySymbols,
Haojian Wu20dba052016-07-20 09:00:22 +000051 std::vector<find_all_symbols::SymbolInfo> Symbols);
Haojian Wu9e4bd0c2016-07-19 14:49:04 +000052
Dmitri Gribenko282dc722019-08-22 11:32:57 +000053 /// Get symbol name.
Haojian Wu9e4bd0c2016-07-19 14:49:04 +000054 llvm::StringRef getSymbolIdentifier() const {
Haojian Wu62aee522016-07-21 13:47:09 +000055 return QuerySymbolInfos.front().RawIdentifier;
Haojian Wu9e4bd0c2016-07-19 14:49:04 +000056 }
Haojian Wu627ca962016-07-08 09:10:29 +000057
Dmitri Gribenko282dc722019-08-22 11:32:57 +000058 /// Get replacement range of the symbol.
Haojian Wu62aee522016-07-21 13:47:09 +000059 tooling::Range getSymbolRange() const {
60 return QuerySymbolInfos.front().Range;
61 }
Haojian Wu627ca962016-07-08 09:10:29 +000062
Dmitri Gribenko282dc722019-08-22 11:32:57 +000063 /// Get the file path to the file being processed.
Haojian Wuc99f7282016-08-09 08:26:19 +000064 StringRef getFilePath() const { return FilePath; }
65
Dmitri Gribenko282dc722019-08-22 11:32:57 +000066 /// Get header information.
Haojian Wu68c34a02016-07-13 16:43:54 +000067 const std::vector<HeaderInfo> &getHeaderInfos() const { return HeaderInfos; }
Haojian Wu627ca962016-07-08 09:10:29 +000068
Dmitri Gribenko282dc722019-08-22 11:32:57 +000069 /// Get information of symbols being querid.
Haojian Wu62aee522016-07-21 13:47:09 +000070 const std::vector<QuerySymbolInfo> &getQuerySymbolInfos() const {
71 return QuerySymbolInfos;
72 }
73
Haojian Wu627ca962016-07-08 09:10:29 +000074private:
75 friend struct llvm::yaml::MappingTraits<IncludeFixerContext>;
76
Dmitri Gribenko282dc722019-08-22 11:32:57 +000077 /// The file path to the file being processed.
Haojian Wuc99f7282016-08-09 08:26:19 +000078 std::string FilePath;
79
Dmitri Gribenko282dc722019-08-22 11:32:57 +000080 /// All instances of an unidentified symbol being queried.
Haojian Wu62aee522016-07-21 13:47:09 +000081 std::vector<QuerySymbolInfo> QuerySymbolInfos;
82
Dmitri Gribenko282dc722019-08-22 11:32:57 +000083 /// The symbol candidates which match SymbolIdentifier. The symbols are
Haojian Wu627ca962016-07-08 09:10:29 +000084 /// sorted in a descending order based on the popularity info in SymbolInfo.
85 std::vector<find_all_symbols::SymbolInfo> MatchedSymbols;
86
Dmitri Gribenko282dc722019-08-22 11:32:57 +000087 /// The header information.
Haojian Wu68c34a02016-07-13 16:43:54 +000088 std::vector<HeaderInfo> HeaderInfos;
Haojian Wu11e9bd22016-05-31 09:31:51 +000089};
90
91} // namespace include_fixer
92} // namespace clang
93
94#endif // LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_INCLUDEFIXERCONTEXT_H