[clang-tidy] Cleanups utils files

Summary:
Cleanup some code by using appropriate APIs.
Some coding style cleanups.

There is no behavior changes.

 - Function `IncludeSorter::CreateFixIt` can be replaced by `FixItHint::CreateReplacement`.
 - Function `cleanPath` is a wrapper for `llvm::sys::path::remove_dots`.

Reviewers: alexfh

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D20279

llvm-svn: 269656
This commit is contained in:
Etienne Bergeron 2016-05-16 14:34:20 +00:00
parent 4b9c0d4dcf
commit 8d73de9eac
9 changed files with 19 additions and 46 deletions

View File

@ -9,6 +9,7 @@
#include "HeaderFileExtensionsUtils.h" #include "HeaderFileExtensionsUtils.h"
#include "clang/Basic/CharInfo.h" #include "clang/Basic/CharInfo.h"
#include "llvm/Support/Path.h"
namespace clang { namespace clang {
namespace tidy { namespace tidy {

View File

@ -10,13 +10,10 @@
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADER_FILE_EXTENSIONS_UTILS_H #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADER_FILE_EXTENSIONS_UTILS_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADER_FILE_EXTENSIONS_UTILS_H #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_HEADER_FILE_EXTENSIONS_UTILS_H
#include <string>
#include "clang/Basic/SourceLocation.h" #include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h" #include "clang/Basic/SourceManager.h"
#include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringRef.h"
#include "llvm/ADT/SmallSet.h" #include "llvm/ADT/SmallSet.h"
#include "llvm/Support/Path.h"
namespace clang { namespace clang {
namespace tidy { namespace tidy {

View File

@ -19,29 +19,16 @@ namespace tidy {
namespace utils { namespace utils {
/// \brief canonicalize a path by removing ./ and ../ components. /// \brief canonicalize a path by removing ./ and ../ components.
// FIXME: Consider moving this to llvm::sys::path.
static std::string cleanPath(StringRef Path) { static std::string cleanPath(StringRef Path) {
SmallString<256> NewPath; SmallString<256> Result = Path;
for (auto I = llvm::sys::path::begin(Path), E = llvm::sys::path::end(Path); llvm::sys::path::remove_dots(Result, true);
I != E; ++I) { return Result.str();
if (*I == ".")
continue;
if (*I == "..") {
// Drop the last component.
NewPath.resize(llvm::sys::path::parent_path(NewPath).size());
} else {
if (!NewPath.empty() && !NewPath.endswith("/"))
NewPath += '/';
NewPath += *I;
}
}
return NewPath.str();
} }
namespace { namespace {
class HeaderGuardPPCallbacks : public PPCallbacks { class HeaderGuardPPCallbacks : public PPCallbacks {
public: public:
explicit HeaderGuardPPCallbacks(Preprocessor *PP, HeaderGuardCheck *Check) HeaderGuardPPCallbacks(Preprocessor *PP, HeaderGuardCheck *Check)
: PP(PP), Check(Check) {} : PP(PP), Check(Check) {}
void FileChanged(SourceLocation Loc, FileChangeReason Reason, void FileChanged(SourceLocation Loc, FileChangeReason Reason,

View File

@ -67,9 +67,9 @@ IncludeInserter::CreateIncludeInsertion(FileID FileID, StringRef Header,
return IncludeSorterByFile[FileID]->CreateIncludeInsertion(Header, IsAngled); return IncludeSorterByFile[FileID]->CreateIncludeInsertion(Header, IsAngled);
} }
void IncludeInserter::AddInclude(StringRef file_name, bool IsAngled, void IncludeInserter::AddInclude(StringRef FileName, bool IsAngled,
SourceLocation HashLocation, SourceLocation HashLocation,
SourceLocation end_location) { SourceLocation EndLocation) {
FileID FileID = SourceMgr.getFileID(HashLocation); FileID FileID = SourceMgr.getFileID(HashLocation);
if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) { if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) {
IncludeSorterByFile.insert(std::make_pair( IncludeSorterByFile.insert(std::make_pair(
@ -77,8 +77,8 @@ void IncludeInserter::AddInclude(StringRef file_name, bool IsAngled,
&SourceMgr, &LangOpts, FileID, &SourceMgr, &LangOpts, FileID,
SourceMgr.getFilename(HashLocation), Style))); SourceMgr.getFilename(HashLocation), Style)));
} }
IncludeSorterByFile[FileID]->AddInclude(file_name, IsAngled, HashLocation, IncludeSorterByFile[FileID]->AddInclude(FileName, IsAngled, HashLocation,
end_location); EndLocation);
} }
} // namespace utils } // namespace utils

View File

@ -60,8 +60,8 @@ public:
CreateIncludeInsertion(FileID FileID, llvm::StringRef Header, bool IsAngled); CreateIncludeInsertion(FileID FileID, llvm::StringRef Header, bool IsAngled);
private: private:
void AddInclude(StringRef file_name, bool IsAngled, void AddInclude(StringRef FileName, bool IsAngled,
SourceLocation hash_location, SourceLocation end_location); SourceLocation HashLocation, SourceLocation EndLocation);
llvm::DenseMap<FileID, std::unique_ptr<IncludeSorter>> IncludeSorterByFile; llvm::DenseMap<FileID, std::unique_ptr<IncludeSorter>> IncludeSorterByFile;
llvm::DenseMap<FileID, std::set<std::string>> InsertedHeaders; llvm::DenseMap<FileID, std::set<std::string>> InsertedHeaders;

View File

@ -254,7 +254,8 @@ std::vector<FixItHint> IncludeSorter::GetEdits() {
// Otherwise report the current block edit and start a new block. // Otherwise report the current block edit and start a new block.
} else { } else {
if (CurrentEndLine) { if (CurrentEndLine) {
Fixes.push_back(CreateFixIt(CurrentRange, CurrentText)); Fixes.push_back(
FixItHint::CreateReplacement(CurrentRange, CurrentText));
} }
CurrentEndLine = LineEdit.first; CurrentEndLine = LineEdit.first;
@ -264,7 +265,7 @@ std::vector<FixItHint> IncludeSorter::GetEdits() {
} }
// Finally, report the current block edit if there is one. // Finally, report the current block edit if there is one.
if (CurrentEndLine) { if (CurrentEndLine) {
Fixes.push_back(CreateFixIt(CurrentRange, CurrentText)); Fixes.push_back(FixItHint::CreateReplacement(CurrentRange, CurrentText));
} }
// Reset the remaining internal state. // Reset the remaining internal state.
@ -273,16 +274,6 @@ std::vector<FixItHint> IncludeSorter::GetEdits() {
return Fixes; return Fixes;
} }
// Creates a fix-it for the given replacements.
// Takes the the source location that will be replaced, and the new text.
FixItHint IncludeSorter::CreateFixIt(SourceRange EditRange,
const std::string &NewText) {
FixItHint Fix;
Fix.RemoveRange = CharSourceRange::getCharRange(EditRange);
Fix.CodeToInsert = NewText;
return Fix;
}
IncludeSorter::IncludeStyle IncludeSorter::IncludeStyle
IncludeSorter::parseIncludeStyle(const std::string &Value) { IncludeSorter::parseIncludeStyle(const std::string &Value) {
return Value == "llvm" ? IS_LLVM : IS_Google; return Value == "llvm" ? IS_LLVM : IS_Google;

View File

@ -66,10 +66,6 @@ public:
private: private:
typedef SmallVector<SourceRange, 1> SourceRangeVector; typedef SmallVector<SourceRange, 1> SourceRangeVector;
// Creates a fix-it for the given replacements.
// Takes the the source location that will be replaced, and the new text.
FixItHint CreateFixIt(SourceRange EditRange, const std::string &NewText);
const SourceManager *SourceMgr; const SourceManager *SourceMgr;
const LangOptions *LangOpts; const LangOptions *LangOpts;
const IncludeStyle Style; const IncludeStyle Style;

View File

@ -77,7 +77,8 @@ bool recordIsTriviallyDefaultConstructible(const RecordDecl &RecordDecl,
} }
// Based on QualType::isTrivial. // Based on QualType::isTrivial.
bool isTriviallyDefaultConstructible(QualType Type, const ASTContext &Context) { bool isTriviallyDefaultConstructible(QualType Type,
const ASTContext &Context) {
if (Type.isNull()) if (Type.isNull())
return false; return false;