[include-fixer] Pull out Context implementation code to a cpp file.

llvm-svn: 274845
This commit is contained in:
Haojian Wu 2016-07-08 13:11:38 +00:00
parent a625af3feb
commit 783d431ad1
3 changed files with 65 additions and 40 deletions

View File

@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
add_clang_library(clangIncludeFixer
IncludeFixer.cpp
IncludeFixerContext.cpp
InMemorySymbolIndex.cpp
SymbolIndexManager.cpp
YamlSymbolIndex.cpp

View File

@ -0,0 +1,61 @@
//===-- IncludeFixerContext.cpp - Include fixer context ---------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "IncludeFixerContext.h"
#include <algorithm>
namespace clang {
namespace include_fixer {
IncludeFixerContext::IncludeFixerContext(
llvm::StringRef Name, llvm::StringRef ScopeQualifiers,
const std::vector<find_all_symbols::SymbolInfo> Symbols,
tooling::Range Range)
: SymbolIdentifier(Name), SymbolScopedQualifiers(ScopeQualifiers),
MatchedSymbols(Symbols), SymbolRange(Range) {
// Deduplicate headers, so that we don't want to suggest the same header
// twice.
for (const auto &Symbol : MatchedSymbols)
Headers.push_back(Symbol.getFilePath());
Headers.erase(std::unique(Headers.begin(), Headers.end(),
[](const std::string &A, const std::string &B) {
return A == B;
}),
Headers.end());
}
tooling::Replacement
IncludeFixerContext::createSymbolReplacement(llvm::StringRef FilePath,
size_t Idx) {
assert(Idx < MatchedSymbols.size());
std::string QualifiedName = MatchedSymbols[Idx].getQualifiedName();
// For nested classes, the qualified name constructed from database misses
// some stripped qualifiers, because when we search a symbol in database,
// we strip qualifiers from the end until we find a result. So append the
// missing stripped qualifiers here.
//
// Get stripped qualifiers.
llvm::SmallVector<llvm::StringRef, 8> SymbolQualifiers;
getSymbolIdentifier().split(SymbolQualifiers, "::");
std::string StrippedQualifiers;
while (!SymbolQualifiers.empty() &&
!llvm::StringRef(QualifiedName).endswith(SymbolQualifiers.back())) {
StrippedQualifiers = "::" + SymbolQualifiers.back().str();
SymbolQualifiers.pop_back();
}
// Append the missing stripped qualifiers.
std::string FullyQualifiedName = QualifiedName + StrippedQualifiers;
auto pos = FullyQualifiedName.find(SymbolScopedQualifiers);
return {FilePath, SymbolRange.getOffset(), SymbolRange.getLength(),
FullyQualifiedName.substr(
pos == std::string::npos ? 0 : SymbolScopedQualifiers.size())};
}
} // include_fixer
} // clang

View File

@ -12,7 +12,6 @@
#include "find-all-symbols/SymbolInfo.h"
#include "clang/Tooling/Core/Replacement.h"
#include <algorithm>
#include <string>
#include <vector>
@ -22,51 +21,15 @@ namespace include_fixer {
/// \brief A context for the symbol being queried.
class IncludeFixerContext {
public:
IncludeFixerContext() {}
IncludeFixerContext() = default;
IncludeFixerContext(llvm::StringRef Name, llvm::StringRef ScopeQualifiers,
const std::vector<find_all_symbols::SymbolInfo> Symbols,
tooling::Range Range)
: SymbolIdentifier(Name), SymbolScopedQualifiers(ScopeQualifiers),
MatchedSymbols(Symbols), SymbolRange(Range) {
// Deduplicate headers, so that we don't want to suggest the same header
// twice.
for (const auto &Symbol : MatchedSymbols)
Headers.push_back(Symbol.getFilePath());
Headers.erase(std::unique(Headers.begin(), Headers.end(),
[](const std::string &A, const std::string &B) {
return A == B;
}),
Headers.end());
}
tooling::Range Range);
/// \brief Create a replacement for adding missing namespace qualifiers to the
/// symbol.
tooling::Replacement createSymbolReplacement(llvm::StringRef FilePath,
size_t Idx = 0) {
assert(Idx < MatchedSymbols.size());
std::string QualifiedName = MatchedSymbols[Idx].getQualifiedName();
// For nested classes, the qualified name constructed from database misses
// some stripped qualifiers, because when we search a symbol in database,
// we strip qualifiers from the end until we find a result. So append the
// missing stripped qualifiers here.
//
// Get stripped qualifiers.
llvm::SmallVector<llvm::StringRef, 8> SymbolQualifiers;
getSymbolIdentifier().split(SymbolQualifiers, "::");
std::string StrippedQualifiers;
while (!SymbolQualifiers.empty() &&
!llvm::StringRef(QualifiedName).endswith(SymbolQualifiers.back())) {
StrippedQualifiers= "::" + SymbolQualifiers.back().str();
SymbolQualifiers.pop_back();
}
// Append the missing stripped qualifiers.
std::string FullyQualifiedName = QualifiedName + StrippedQualifiers;
auto pos = FullyQualifiedName.find(SymbolScopedQualifiers);
return {FilePath, SymbolRange.getOffset(), SymbolRange.getLength(),
FullyQualifiedName.substr(
pos == std::string::npos ? 0 : SymbolScopedQualifiers.size())};
}
size_t Idx = 0);
/// \brief Get symbol name.
llvm::StringRef getSymbolIdentifier() const { return SymbolIdentifier; }