From 3093a63f7a1ca5545a1f56e5bcdb996ff37647bd Mon Sep 17 00:00:00 2001 From: Eugene Zelenko Date: Wed, 15 Feb 2017 22:17:02 +0000 Subject: [PATCH] [Support] Fix some Clang-tidy modernize and Include What You Use warnings; other minor fixes (NFC). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@295243 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/MD5.h | 23 ++++++++----- include/llvm/Support/RWMutex.h | 25 ++++++-------- include/llvm/Support/SMLoc.h | 4 +-- include/llvm/Support/SourceMgr.h | 57 ++++++++++++++++--------------- include/llvm/Support/UniqueLock.h | 18 +++++----- lib/Support/MD5.cpp | 11 +++--- lib/Support/RWMutex.cpp | 19 +++++------ lib/Support/SourceMgr.cpp | 27 ++++++++++----- 8 files changed, 101 insertions(+), 83 deletions(-) diff --git a/include/llvm/Support/MD5.h b/include/llvm/Support/MD5.h index eb181bfe8a5..190cf4cbc52 100644 --- a/include/llvm/Support/MD5.h +++ b/include/llvm/Support/MD5.h @@ -1,4 +1,4 @@ -/* +/* -*- C++ -*- * This code is derived from (original license follows): * * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. @@ -29,19 +29,25 @@ #define LLVM_SUPPORT_MD5_H #include "llvm/ADT/SmallString.h" -#include "llvm/Support/DataTypes.h" +#include "llvm/ADT/StringRef.h" #include "llvm/Support/Endian.h" #include +#include namespace llvm { + template class ArrayRef; class MD5 { // Any 32-bit or wider unsigned integer data type will do. typedef uint32_t MD5_u32plus; - MD5_u32plus a, b, c, d; - MD5_u32plus hi, lo; + MD5_u32plus a = 0x67452301; + MD5_u32plus b = 0xefcdab89; + MD5_u32plus c = 0x98badcfe; + MD5_u32plus d = 0x10325476; + MD5_u32plus hi = 0; + MD5_u32plus lo = 0; uint8_t buffer[64]; MD5_u32plus block[16]; @@ -72,16 +78,17 @@ private: /// Helper to compute and return lower 64 bits of the given string's MD5 hash. inline uint64_t MD5Hash(StringRef Str) { + using namespace support; + MD5 Hash; Hash.update(Str); - llvm::MD5::MD5Result Result; + MD5::MD5Result Result; Hash.final(Result); // Return the least significant 8 bytes. Our MD5 implementation returns the // result in little endian, so we may need to swap bytes. - using namespace llvm::support; return endian::read(Result); } -} +} // end namespace llvm -#endif +#endif // LLVM_SUPPORT_MD5_H diff --git a/include/llvm/Support/RWMutex.h b/include/llvm/Support/RWMutex.h index e4736b8e24e..62b097d66af 100644 --- a/include/llvm/Support/RWMutex.h +++ b/include/llvm/Support/RWMutex.h @@ -14,7 +14,7 @@ #ifndef LLVM_SUPPORT_RWMUTEX_H #define LLVM_SUPPORT_RWMUTEX_H -#include "llvm/Support/Compiler.h" +#include "llvm/Config/config.h" #include "llvm/Support/Threading.h" #include @@ -32,6 +32,13 @@ namespace sys { /// @brief Default Constructor. explicit RWMutexImpl(); + /// @} + /// @name Do Not Implement + /// @{ + RWMutexImpl(const RWMutexImpl & original) = delete; + RWMutexImpl &operator=(const RWMutexImpl &) = delete; + /// @} + /// Releases and removes the lock /// @brief Destructor ~RWMutexImpl(); @@ -70,16 +77,8 @@ namespace sys { /// @{ private: #if defined(LLVM_ENABLE_THREADS) && LLVM_ENABLE_THREADS != 0 - void* data_; ///< We don't know what the data will be + void* data_ = nullptr; ///< We don't know what the data will be #endif - - /// @} - /// @name Do Not Implement - /// @{ - private: - RWMutexImpl(const RWMutexImpl & original) = delete; - void operator=(const RWMutexImpl &) = delete; - /// @} }; /// SmartMutex - An R/W mutex with a compile time constant parameter that @@ -93,6 +92,8 @@ namespace sys { public: explicit SmartRWMutex() = default; + SmartRWMutex(const SmartRWMutex & original) = delete; + SmartRWMutex &operator=(const SmartRWMutex &) = delete; bool lock_shared() { if (!mt_only || llvm_is_multithreaded()) @@ -136,10 +137,6 @@ namespace sys { --writers; return true; } - - private: - SmartRWMutex(const SmartRWMutex & original); - void operator=(const SmartRWMutex &); }; typedef SmartRWMutex RWMutex; diff --git a/include/llvm/Support/SMLoc.h b/include/llvm/Support/SMLoc.h index eb3a1ba7db5..5b8be550554 100644 --- a/include/llvm/Support/SMLoc.h +++ b/include/llvm/Support/SMLoc.h @@ -22,10 +22,10 @@ namespace llvm { /// Represents a location in source code. class SMLoc { - const char *Ptr; + const char *Ptr = nullptr; public: - SMLoc() : Ptr(nullptr) {} + SMLoc() = default; bool isValid() const { return Ptr != nullptr; } diff --git a/include/llvm/Support/SourceMgr.h b/include/llvm/Support/SourceMgr.h index bc7478e0d70..cb90d968c44 100644 --- a/include/llvm/Support/SourceMgr.h +++ b/include/llvm/Support/SourceMgr.h @@ -17,18 +17,24 @@ #define LLVM_SUPPORT_SOURCEMGR_H #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/None.h" +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Twine.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/SMLoc.h" +#include +#include +#include #include +#include +#include namespace llvm { - class SourceMgr; - class SMDiagnostic; - class SMFixIt; - class Twine; - class raw_ostream; + +class raw_ostream; +class SMDiagnostic; +class SMFixIt; /// This owns the files read by a parser, handles include stacks, /// and handles diagnostic wrangling. @@ -44,6 +50,7 @@ public: /// register a function pointer+context as a diagnostic handler. /// It gets called each time PrintMessage is invoked. typedef void (*DiagHandlerTy)(const SMDiagnostic &, void *Context); + private: struct SrcBuffer { /// The memory buffer for the file. @@ -61,18 +68,17 @@ private: /// This is a cache for line number queries, its implementation is really /// private to SourceMgr.cpp. - mutable void *LineNoCache; + mutable void *LineNoCache = nullptr; - DiagHandlerTy DiagHandler; - void *DiagContext; + DiagHandlerTy DiagHandler = nullptr; + void *DiagContext = nullptr; bool isValidBufferID(unsigned i) const { return i && i <= Buffers.size(); } - SourceMgr(const SourceMgr&) = delete; - void operator=(const SourceMgr&) = delete; public: - SourceMgr() - : LineNoCache(nullptr), DiagHandler(nullptr), DiagContext(nullptr) {} + SourceMgr() = default; + SourceMgr(const SourceMgr &) = delete; + SourceMgr &operator=(const SourceMgr &) = delete; ~SourceMgr(); void setIncludeDirs(const std::vector &Dirs) { @@ -190,7 +196,6 @@ public: void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const; }; - /// Represents a single fixit, a replacement of one range of text with another. class SMFixIt { SMRange Range; @@ -222,33 +227,31 @@ public: } }; - /// Instances of this class encapsulate one diagnostic report, allowing /// printing to a raw_ostream as a caret diagnostic. class SMDiagnostic { - const SourceMgr *SM; + const SourceMgr *SM = nullptr; SMLoc Loc; std::string Filename; - int LineNo, ColumnNo; - SourceMgr::DiagKind Kind; + int LineNo = 0; + int ColumnNo = 0; + SourceMgr::DiagKind Kind = SourceMgr::DK_Error; std::string Message, LineContents; - std::vector > Ranges; + std::vector> Ranges; SmallVector FixIts; public: // Null diagnostic. - SMDiagnostic() - : SM(nullptr), LineNo(0), ColumnNo(0), Kind(SourceMgr::DK_Error) {} + SMDiagnostic() = default; // Diagnostic with no location (e.g. file not found, command line arg error). SMDiagnostic(StringRef filename, SourceMgr::DiagKind Knd, StringRef Msg) - : SM(nullptr), Filename(filename), LineNo(-1), ColumnNo(-1), Kind(Knd), - Message(Msg) {} + : Filename(filename), LineNo(-1), ColumnNo(-1), Kind(Knd), Message(Msg) {} // Diagnostic with a location. SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN, int Line, int Col, SourceMgr::DiagKind Kind, StringRef Msg, StringRef LineStr, - ArrayRef > Ranges, + ArrayRef> Ranges, ArrayRef FixIts = None); const SourceMgr *getSourceMgr() const { return SM; } @@ -259,9 +262,7 @@ public: SourceMgr::DiagKind getKind() const { return Kind; } StringRef getMessage() const { return Message; } StringRef getLineContents() const { return LineContents; } - ArrayRef > getRanges() const { - return Ranges; - } + ArrayRef> getRanges() const { return Ranges; } void addFixIt(const SMFixIt &Hint) { FixIts.push_back(Hint); @@ -275,6 +276,6 @@ public: bool ShowKindLabel = true) const; }; -} // end llvm namespace +} // end namespace llvm -#endif +#endif // LLVM_SUPPORT_SOURCEMGR_H diff --git a/include/llvm/Support/UniqueLock.h b/include/llvm/Support/UniqueLock.h index 529284d3868..b4675f4b43a 100644 --- a/include/llvm/Support/UniqueLock.h +++ b/include/llvm/Support/UniqueLock.h @@ -1,4 +1,4 @@ -//===-- Support/UniqueLock.h - Acquire/Release Mutex In Scope ---*- C++ -*-===// +//===- Support/UniqueLock.h - Acquire/Release Mutex In Scope ----*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -15,9 +15,10 @@ #ifndef LLVM_SUPPORT_UNIQUE_LOCK_H #define LLVM_SUPPORT_UNIQUE_LOCK_H -#include "llvm/Support/Mutex.h" +#include namespace llvm { + /// A pared-down imitation of std::unique_lock from C++11. Contrary to the /// name, it's really more of a wrapper for a lock. It may or may not have /// an associated mutex, which is guaranteed to be locked upon creation @@ -26,14 +27,14 @@ namespace llvm { /// @brief Guard a section of code with a mutex. template class unique_lock { - MutexT *M; - bool locked; + MutexT *M = nullptr; + bool locked = false; - unique_lock(const unique_lock &) = delete; - void operator=(const unique_lock &) = delete; public: - unique_lock() : M(nullptr), locked(false) {} + unique_lock() = default; explicit unique_lock(MutexT &m) : M(&m), locked(true) { M->lock(); } + unique_lock(const unique_lock &) = delete; + unique_lock &operator=(const unique_lock &) = delete; void operator=(unique_lock &&o) { if (owns_lock()) @@ -62,6 +63,7 @@ namespace llvm { bool owns_lock() { return locked; } }; -} + +} // end namespace llvm #endif // LLVM_SUPPORT_UNIQUE_LOCK_H diff --git a/lib/Support/MD5.cpp b/lib/Support/MD5.cpp index 942571eab0f..809dbbce708 100644 --- a/lib/Support/MD5.cpp +++ b/lib/Support/MD5.cpp @@ -38,9 +38,13 @@ */ #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Endian.h" #include "llvm/Support/Format.h" #include "llvm/Support/MD5.h" #include "llvm/Support/raw_ostream.h" +#include +#include #include // The basic MD5 functions. @@ -68,7 +72,7 @@ ((MD5_u32plus) ptr[(n) * 4 + 3] << 24)) #define GET(n) (block[(n)]) -namespace llvm { +using namespace llvm; /// \brief This processes one or more 64-byte data blocks, but does NOT update ///the bit counters. There are no alignment requirements. @@ -179,9 +183,7 @@ const uint8_t *MD5::body(ArrayRef Data) { return ptr; } -MD5::MD5() - : a(0x67452301), b(0xefcdab89), c(0x98badcfe), d(0x10325476), hi(0), lo(0) { -} +MD5::MD5() = default; /// Incrementally add the bytes in \p Data to the hash. void MD5::update(ArrayRef Data) { @@ -275,4 +277,3 @@ std::array MD5::hash(ArrayRef Data) { memcpy(Arr.data(), Res, sizeof(Res)); return Arr; } -} diff --git a/lib/Support/RWMutex.cpp b/lib/Support/RWMutex.cpp index 3b6309cef21..6c9781c4e2d 100644 --- a/lib/Support/RWMutex.cpp +++ b/lib/Support/RWMutex.cpp @@ -13,7 +13,6 @@ #include "llvm/Config/config.h" #include "llvm/Support/RWMutex.h" -#include //===----------------------------------------------------------------------===// //=== WARNING: Implementation here must contain only TRULY operating system @@ -22,29 +21,31 @@ #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0 // Define all methods as no-ops if threading is explicitly disabled -namespace llvm { + +using namespace llvm; using namespace sys; -RWMutexImpl::RWMutexImpl() { } -RWMutexImpl::~RWMutexImpl() { } + +RWMutexImpl::RWMutexImpl() = default; +RWMutexImpl::~RWMutexImpl() = default; + bool RWMutexImpl::reader_acquire() { return true; } bool RWMutexImpl::reader_release() { return true; } bool RWMutexImpl::writer_acquire() { return true; } bool RWMutexImpl::writer_release() { return true; } -} + #else #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT) #include +#include #include -#include -namespace llvm { +using namespace llvm; using namespace sys; // Construct a RWMutex using pthread calls RWMutexImpl::RWMutexImpl() - : data_(nullptr) { // Declare the pthread_rwlock data structures pthread_rwlock_t* rwlock = @@ -113,8 +114,6 @@ RWMutexImpl::writer_release() return errorcode == 0; } -} - #elif defined(LLVM_ON_UNIX) #include "Unix/RWMutex.inc" #elif defined( LLVM_ON_WIN32) diff --git a/lib/Support/SourceMgr.cpp b/lib/Support/SourceMgr.cpp index 4cb9b2ff2cd..ca2391c10ff 100644 --- a/lib/Support/SourceMgr.cpp +++ b/lib/Support/SourceMgr.cpp @@ -13,30 +13,43 @@ // //===----------------------------------------------------------------------===// -#include "llvm/Support/SourceMgr.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringRef.h" #include "llvm/ADT/Twine.h" +#include "llvm/Support/ErrorOr.h" #include "llvm/Support/Locale.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Support/SMLoc.h" +#include "llvm/Support/SourceMgr.h" +#include +#include +#include +#include +#include +#include + using namespace llvm; static const size_t TabStop = 8; namespace { + struct LineNoCacheTy { const char *LastQuery; unsigned LastQueryBufferID; unsigned LineNoOfQuery; }; -} + +} // end anonymous namespace static LineNoCacheTy *getCache(void *Ptr) { return (LineNoCacheTy*)Ptr; } - SourceMgr::~SourceMgr() { // Delete the line # cache if allocated. if (LineNoCacheTy *Cache = getCache(LineNoCache)) @@ -132,12 +145,10 @@ void SourceMgr::PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const { << ":" << FindLineNumber(IncludeLoc, CurBuf) << ":\n"; } - SMDiagnostic SourceMgr::GetMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg, ArrayRef Ranges, ArrayRef FixIts) const { - // First thing to do: find the current buffer containing the specified // location to pull out the source line. SmallVector, 4> ColRanges; @@ -223,7 +234,7 @@ void SourceMgr::PrintMessage(raw_ostream &OS, SMLoc Loc, void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg, ArrayRef Ranges, ArrayRef FixIts, bool ShowColors) const { - PrintMessage(llvm::errs(), Loc, Kind, Msg, Ranges, FixIts, ShowColors); + PrintMessage(errs(), Loc, Kind, Msg, Ranges, FixIts, ShowColors); } //===----------------------------------------------------------------------===// @@ -233,7 +244,7 @@ void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, SMDiagnostic::SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN, int Line, int Col, SourceMgr::DiagKind Kind, StringRef Msg, StringRef LineStr, - ArrayRef > Ranges, + ArrayRef> Ranges, ArrayRef Hints) : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Kind(Kind), Message(Msg), LineContents(LineStr), Ranges(Ranges.vec()), @@ -286,7 +297,7 @@ static void buildFixItLine(std::string &CaretLine, std::string &FixItLine, // FIXME: This assertion is intended to catch unintended use of multibyte // characters in fixits. If we decide to do this, we'll have to track // separate byte widths for the source and fixit lines. - assert((size_t)llvm::sys::locale::columnWidth(I->getText()) == + assert((size_t)sys::locale::columnWidth(I->getText()) == I->getText().size()); // This relies on one byte per column in our fixit hints. -- 2.50.1