From 8d73de9eac0e7bbfc913a0243b087925d6fe3d37 Mon Sep 17 00:00:00 2001 From: Etienne Bergeron <etienneb@google.com> Date: Mon, 16 May 2016 14:34:20 +0000 Subject: [PATCH] [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 --- .../clang-tidy/utils/DeclRefExprUtils.cpp | 2 +- .../utils/HeaderFileExtensionsUtils.cpp | 1 + .../utils/HeaderFileExtensionsUtils.h | 3 --- .../clang-tidy/utils/HeaderGuard.cpp | 21 ++++--------------- .../clang-tidy/utils/IncludeInserter.cpp | 8 +++---- .../clang-tidy/utils/IncludeInserter.h | 4 ++-- .../clang-tidy/utils/IncludeSorter.cpp | 17 ++++----------- .../clang-tidy/utils/IncludeSorter.h | 6 +----- .../clang-tidy/utils/TypeTraits.cpp | 3 ++- 9 files changed, 19 insertions(+), 46 deletions(-) diff --git a/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp b/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp index 3c78349bb7fc..78291ddc9f79 100644 --- a/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp +++ b/clang-tools-extra/clang-tidy/utils/DeclRefExprUtils.cpp @@ -15,7 +15,7 @@ namespace clang { namespace tidy { -namespace utils { +namespace utils { namespace decl_ref_expr { using namespace ::clang::ast_matchers; diff --git a/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.cpp b/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.cpp index 749ce6c66739..557f119b5d49 100644 --- a/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.cpp +++ b/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.cpp @@ -9,6 +9,7 @@ #include "HeaderFileExtensionsUtils.h" #include "clang/Basic/CharInfo.h" +#include "llvm/Support/Path.h" namespace clang { namespace tidy { diff --git a/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.h b/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.h index 4b0f3b552b6d..8ce3c57f1cfc 100644 --- a/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.h +++ b/clang-tools-extra/clang-tidy/utils/HeaderFileExtensionsUtils.h @@ -10,13 +10,10 @@ #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 -#include <string> - #include "clang/Basic/SourceLocation.h" #include "clang/Basic/SourceManager.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/SmallSet.h" -#include "llvm/Support/Path.h" namespace clang { namespace tidy { diff --git a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp index 09e705f4a0e2..6eb241cbd428 100644 --- a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp +++ b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp @@ -19,29 +19,16 @@ namespace tidy { namespace utils { /// \brief canonicalize a path by removing ./ and ../ components. -// FIXME: Consider moving this to llvm::sys::path. static std::string cleanPath(StringRef Path) { - SmallString<256> NewPath; - for (auto I = llvm::sys::path::begin(Path), E = llvm::sys::path::end(Path); - I != E; ++I) { - 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(); + SmallString<256> Result = Path; + llvm::sys::path::remove_dots(Result, true); + return Result.str(); } namespace { class HeaderGuardPPCallbacks : public PPCallbacks { public: - explicit HeaderGuardPPCallbacks(Preprocessor *PP, HeaderGuardCheck *Check) + HeaderGuardPPCallbacks(Preprocessor *PP, HeaderGuardCheck *Check) : PP(PP), Check(Check) {} void FileChanged(SourceLocation Loc, FileChangeReason Reason, diff --git a/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp b/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp index 16fb58c0e531..3a3188996641 100644 --- a/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp +++ b/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp @@ -67,9 +67,9 @@ IncludeInserter::CreateIncludeInsertion(FileID FileID, StringRef Header, return IncludeSorterByFile[FileID]->CreateIncludeInsertion(Header, IsAngled); } -void IncludeInserter::AddInclude(StringRef file_name, bool IsAngled, +void IncludeInserter::AddInclude(StringRef FileName, bool IsAngled, SourceLocation HashLocation, - SourceLocation end_location) { + SourceLocation EndLocation) { FileID FileID = SourceMgr.getFileID(HashLocation); if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) { IncludeSorterByFile.insert(std::make_pair( @@ -77,8 +77,8 @@ void IncludeInserter::AddInclude(StringRef file_name, bool IsAngled, &SourceMgr, &LangOpts, FileID, SourceMgr.getFilename(HashLocation), Style))); } - IncludeSorterByFile[FileID]->AddInclude(file_name, IsAngled, HashLocation, - end_location); + IncludeSorterByFile[FileID]->AddInclude(FileName, IsAngled, HashLocation, + EndLocation); } } // namespace utils diff --git a/clang-tools-extra/clang-tidy/utils/IncludeInserter.h b/clang-tools-extra/clang-tidy/utils/IncludeInserter.h index 9d6754f8f491..23bba966f6a7 100644 --- a/clang-tools-extra/clang-tidy/utils/IncludeInserter.h +++ b/clang-tools-extra/clang-tidy/utils/IncludeInserter.h @@ -60,8 +60,8 @@ public: CreateIncludeInsertion(FileID FileID, llvm::StringRef Header, bool IsAngled); private: - void AddInclude(StringRef file_name, bool IsAngled, - SourceLocation hash_location, SourceLocation end_location); + void AddInclude(StringRef FileName, bool IsAngled, + SourceLocation HashLocation, SourceLocation EndLocation); llvm::DenseMap<FileID, std::unique_ptr<IncludeSorter>> IncludeSorterByFile; llvm::DenseMap<FileID, std::set<std::string>> InsertedHeaders; diff --git a/clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp b/clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp index 954f92d43b0f..8be88724908e 100644 --- a/clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp +++ b/clang-tools-extra/clang-tidy/utils/IncludeSorter.cpp @@ -12,7 +12,7 @@ namespace clang { namespace tidy { -namespace utils { +namespace utils { namespace { @@ -254,7 +254,8 @@ std::vector<FixItHint> IncludeSorter::GetEdits() { // Otherwise report the current block edit and start a new block. } else { if (CurrentEndLine) { - Fixes.push_back(CreateFixIt(CurrentRange, CurrentText)); + Fixes.push_back( + FixItHint::CreateReplacement(CurrentRange, CurrentText)); } CurrentEndLine = LineEdit.first; @@ -264,7 +265,7 @@ std::vector<FixItHint> IncludeSorter::GetEdits() { } // Finally, report the current block edit if there is one. if (CurrentEndLine) { - Fixes.push_back(CreateFixIt(CurrentRange, CurrentText)); + Fixes.push_back(FixItHint::CreateReplacement(CurrentRange, CurrentText)); } // Reset the remaining internal state. @@ -273,16 +274,6 @@ std::vector<FixItHint> IncludeSorter::GetEdits() { 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::parseIncludeStyle(const std::string &Value) { return Value == "llvm" ? IS_LLVM : IS_Google; diff --git a/clang-tools-extra/clang-tidy/utils/IncludeSorter.h b/clang-tools-extra/clang-tidy/utils/IncludeSorter.h index 4939ffeec5c8..7e941872a0c0 100644 --- a/clang-tools-extra/clang-tidy/utils/IncludeSorter.h +++ b/clang-tools-extra/clang-tidy/utils/IncludeSorter.h @@ -15,7 +15,7 @@ namespace clang { namespace tidy { -namespace utils { +namespace utils { // Class used by IncludeSorterCallback and IncludeInserterCallback to record the // names of the inclusions in a given source file being processed and generate @@ -66,10 +66,6 @@ public: private: 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 LangOptions *LangOpts; const IncludeStyle Style; diff --git a/clang-tools-extra/clang-tidy/utils/TypeTraits.cpp b/clang-tools-extra/clang-tidy/utils/TypeTraits.cpp index dfa8b67aeb2d..2fb4005d7a8c 100644 --- a/clang-tools-extra/clang-tidy/utils/TypeTraits.cpp +++ b/clang-tools-extra/clang-tidy/utils/TypeTraits.cpp @@ -77,7 +77,8 @@ bool recordIsTriviallyDefaultConstructible(const RecordDecl &RecordDecl, } // Based on QualType::isTrivial. -bool isTriviallyDefaultConstructible(QualType Type, const ASTContext &Context) { +bool isTriviallyDefaultConstructible(QualType Type, + const ASTContext &Context) { if (Type.isNull()) return false;