diff --git a/clang-tools-extra/cpp11-migrate/Core/FileOverrides.cpp b/clang-tools-extra/cpp11-migrate/Core/FileOverrides.cpp index 83678e986b08..58372b60ce71 100644 --- a/clang-tools-extra/cpp11-migrate/Core/FileOverrides.cpp +++ b/clang-tools-extra/cpp11-migrate/Core/FileOverrides.cpp @@ -23,26 +23,16 @@ #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Support/system_error.h" -#include using namespace clang; using namespace clang::tooling; -void HeaderOverride::recordReplacements( - llvm::StringRef TransformID, const clang::tooling::Replacements &Replaces) { - TransformReplacements TR; - TR.TransformID = TransformID; - TR.GeneratedReplacements.resize(Replaces.size()); - std::copy(Replaces.begin(), Replaces.end(), TR.GeneratedReplacements.begin()); - TransformReplacementsDoc.Replacements.push_back(TR); -} - SourceOverrides::SourceOverrides(llvm::StringRef MainFileName, bool TrackChanges) : MainFileName(MainFileName), TrackChanges(TrackChanges) {} -void SourceOverrides::applyReplacements(tooling::Replacements &Replaces, - llvm::StringRef TransformName) { +void +SourceOverrides::applyReplacements(clang::tooling::Replacements &Replaces) { llvm::IntrusiveRefCntPtr DiagOpts( new DiagnosticOptions()); DiagnosticsEngine Diagnostics( @@ -50,12 +40,11 @@ void SourceOverrides::applyReplacements(tooling::Replacements &Replaces, DiagOpts.getPtr()); FileManager Files((FileSystemOptions())); SourceManager SM(Diagnostics, Files); - applyReplacements(Replaces, SM, TransformName); + applyReplacements(Replaces, SM); } -void SourceOverrides::applyReplacements(tooling::Replacements &Replaces, - SourceManager &SM, - llvm::StringRef TransformName) { +void SourceOverrides::applyReplacements(clang::tooling::Replacements &Replaces, + clang::SourceManager &SM) { applyOverrides(SM); Rewriter Rewrites(SM, LangOptions()); @@ -67,6 +56,12 @@ void SourceOverrides::applyReplacements(tooling::Replacements &Replaces, if (!Success) llvm::errs() << "error: failed to apply some replacements."; + applyRewrites(Rewrites); + if (TrackChanges) + adjustChangedRanges(Replaces); +} + +void SourceOverrides::applyRewrites(Rewriter &Rewrites) { std::string ResultBuf; for (Rewriter::buffer_iterator I = Rewrites.buffer_begin(), @@ -94,50 +89,36 @@ void SourceOverrides::applyReplacements(tooling::Replacements &Replaces, // will be stored as well for later output to disk. Applying replacements // in memory will always be necessary as the source goes down the transform // pipeline. + HeaderOverride &HeaderOv = Headers[FileName]; + HeaderOv.FileOverride.swap(ResultBuf); // "Create" HeaderOverride if not already existing - if (HeaderOv.getFileName().empty()) - HeaderOv = HeaderOverride(FileName); - - HeaderOv.swapContentOverride(ResultBuf); + if (HeaderOv.FileName.empty()) + HeaderOv.FileName = FileName; } - - // Separate replacements to header files - Replacements MainFileReplaces; - ReplacementsMap HeadersReplaces; - for (Replacements::const_iterator I = Replaces.begin(), E = Replaces.end(); - I != E; ++I) { - llvm::StringRef ReplacementFileName = I->getFilePath(); - - if (ReplacementFileName == MainFileName) { - MainFileReplaces.insert(*I); - continue; - } - - HeadersReplaces[ReplacementFileName].insert(*I); - } - - // Record all replacements to headers. - for (ReplacementsMap::const_iterator I = HeadersReplaces.begin(), - E = HeadersReplaces.end(); - I != E; ++I) { - HeaderOverride &HeaderOv = Headers[I->getKey()]; - HeaderOv.recordReplacements(TransformName, I->getValue()); - } - - if (TrackChanges) - adjustChangedRanges(MainFileReplaces, HeadersReplaces); } -void -SourceOverrides::adjustChangedRanges(const Replacements &MainFileReplaces, - const ReplacementsMap &HeadersReplaces) { - // Adjust the changed ranges for each individual file - MainFileChanges.adjustChangedRanges(MainFileReplaces); - for (ReplacementsMap::const_iterator I = HeadersReplaces.begin(), - E = HeadersReplaces.end(); +void SourceOverrides::adjustChangedRanges(const Replacements &Replaces) { + // Start by grouping replacements by file name + Replacements MainFileReplaces; + llvm::StringMap HeadersReplaces; + + for (Replacements::iterator I = Replaces.begin(), E = Replaces.end(); I != E; + ++I) { + llvm::StringRef ReplacementFileName = I->getFilePath(); + + if (ReplacementFileName == MainFileName) + MainFileReplaces.insert(*I); + else + HeadersReplaces[ReplacementFileName].insert(*I); + } + + // Then adjust the changed ranges for each individual file + MainFileChanges.adjustChangedRanges(Replaces); + for (llvm::StringMap::iterator I = HeadersReplaces.begin(), + E = HeadersReplaces.end(); I != E; ++I) { - Headers[I->getKey()].adjustChangedRanges(I->getValue()); + Headers[I->getKey()].Changes.adjustChangedRanges(I->getValue()); } } @@ -150,11 +131,11 @@ void SourceOverrides::applyOverrides(SourceManager &SM) const { for (HeaderOverrides::const_iterator I = Headers.begin(), E = Headers.end(); I != E; ++I) { - assert(!I->second.getContentOverride().empty() && + assert(!I->second.FileOverride.empty() && "Header override should not be empty!"); SM.overrideFileContents( - FM.getFile(I->second.getFileName()), - llvm::MemoryBuffer::getMemBuffer(I->second.getContentOverride())); + FM.getFile(I->second.FileName), + llvm::MemoryBuffer::getMemBuffer(I->second.FileOverride)); } } diff --git a/clang-tools-extra/cpp11-migrate/Core/FileOverrides.h b/clang-tools-extra/cpp11-migrate/Core/FileOverrides.h index 3bb1acb38232..2bb3daea7b0a 100644 --- a/clang-tools-extra/cpp11-migrate/Core/FileOverrides.h +++ b/clang-tools-extra/cpp11-migrate/Core/FileOverrides.h @@ -16,7 +16,6 @@ #ifndef CPP11_MIGRATE_FILE_OVERRIDES_H #define CPP11_MIGRATE_FILE_OVERRIDES_H -#include "Core/ReplacementsYaml.h" #include "clang/Tooling/Refactoring.h" #include "llvm/ADT/StringMap.h" @@ -59,52 +58,13 @@ private: }; /// \brief Container for storing override information for a single headers. -class HeaderOverride { -public: - /// \brief Constructors. - /// @{ +struct HeaderOverride { HeaderOverride() {} - HeaderOverride(llvm::StringRef FileName) { - TransformReplacementsDoc.FileName = FileName; - } - /// @} + HeaderOverride(llvm::StringRef FileName) : FileName(FileName) {} - /// \brief Getter for FileName. - llvm::StringRef getFileName() const { - return TransformReplacementsDoc.FileName; - } - - /// \brief Accessor for ContentOverride. - /// @{ - llvm::StringRef getContentOverride() const { return ContentOverride; } - void setContentOverride(const llvm::StringRef S) { ContentOverride = S; } - /// @} - - /// \brief Getter for Changes. - const ChangedRanges &getChanges() const { return Changes; } - - /// \brief Swaps the content of ContentOverride with \param S - void swapContentOverride(std::string &S) { ContentOverride.swap(S); } - - /// \brief Getter for TransformReplacementsDoc. - const TransformDocument &getTransformReplacementsDoc() const { - return TransformReplacementsDoc; - } - - /// \brief Stores the replacements made by a transform to the header this - /// object represents. - void recordReplacements(llvm::StringRef TransformID, - const clang::tooling::Replacements &Replaces); - - /// \brief Helper function to adjust the changed ranges. - void adjustChangedRanges(const clang::tooling::Replacements &Replaces) { - Changes.adjustChangedRanges(Replaces); - } - -private: - std::string ContentOverride; + std::string FileName; + std::string FileOverride; ChangedRanges Changes; - TransformDocument TransformReplacementsDoc; }; /// \brief Container mapping header file names to override information. @@ -140,10 +100,8 @@ public: /// \param Replaces The replacements to apply. /// \param SM A user provided SourceManager to be used when applying rewrites. void applyReplacements(clang::tooling::Replacements &Replaces, - clang::SourceManager &SM, - llvm::StringRef TransformName); - void applyReplacements(clang::tooling::Replacements &Replaces, - llvm::StringRef TransformName); + clang::SourceManager &SM); + void applyReplacements(clang::tooling::Replacements &Replaces); /// \brief Convenience function for applying this source's overrides to /// the given SourceManager. @@ -160,16 +118,13 @@ public: /// @} private: - typedef llvm::StringMap ReplacementsMap; - /// \brief Flatten the Rewriter buffers of \p Rewrite and store results as /// file content overrides. void applyRewrites(clang::Rewriter &Rewrite); /// \brief Adjust the changed ranges to reflect the parts of the files that /// have been replaced. - void adjustChangedRanges(const clang::tooling::Replacements &Replaces, - const ReplacementsMap &HeadersReplaces); + void adjustChangedRanges(const clang::tooling::Replacements &Replaces); const std::string MainFileName; std::string MainFileOverride; diff --git a/clang-tools-extra/cpp11-migrate/Core/Reformatting.cpp b/clang-tools-extra/cpp11-migrate/Core/Reformatting.cpp index e50206fc0f74..3fd77612bbfe 100644 --- a/clang-tools-extra/cpp11-migrate/Core/Reformatting.cpp +++ b/clang-tools-extra/cpp11-migrate/Core/Reformatting.cpp @@ -47,10 +47,10 @@ void Reformatter::reformatChanges(SourceOverrides &Overrides, I != E; ++I) { const HeaderOverride &Header = I->getValue(); const tooling::Replacements &HeaderReplaces = - reformatSingleFile(Header.getFileName(), Header.getChanges(), SM); + reformatSingleFile(Header.FileName, Header.Changes, SM); Replaces.insert(HeaderReplaces.begin(), HeaderReplaces.end()); } - Overrides.applyReplacements(Replaces, SM, "reformatter"); + Overrides.applyReplacements(Replaces, SM); } tooling::Replacements Reformatter::reformatSingleFile( diff --git a/clang-tools-extra/cpp11-migrate/Core/ReplacementsYaml.h b/clang-tools-extra/cpp11-migrate/Core/ReplacementsYaml.h deleted file mode 100644 index 0374aae9aaf3..000000000000 --- a/clang-tools-extra/cpp11-migrate/Core/ReplacementsYaml.h +++ /dev/null @@ -1,115 +0,0 @@ -//===-- Core/ReplacementsYaml.h ---------------------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -/// -/// \file -/// \brief This file provides structs to store the migrator specific header -/// replacements and the YAML traits for Replacements. -/// -//===----------------------------------------------------------------------===// - -#ifndef CPP11_MIGRATE_REPLACEMENTS_YAML_H -#define CPP11_MIGRATE_REPLACEMENTS_YAML_H - -#include "clang/Tooling/Refactoring.h" -#include "llvm/Support/YAMLTraits.h" -#include -#include - -/// \brief A replacement struct to store the transform ID and the replacement. -struct TransformReplacements { - std::string TransformID; - std::vector GeneratedReplacements; -}; - -/// \brief The top-level YAML document that contains the name of the file and -/// the TransformReplacements. -struct TransformDocument { - std::string FileName; - std::vector Replacements; -}; - -LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Replacement) -LLVM_YAML_IS_SEQUENCE_VECTOR(TransformReplacements) - -namespace llvm { -namespace yaml { - -/// \brief ScalarTraits to read/write std::string objects. -template <> -struct ScalarTraits { - static void output(const std::string &Val, void *, llvm::raw_ostream &Out) { - // We need to put quotes around the string to make sure special characters - // in the string is not treated as YAML tokens. - std::string NormalizedVal = std::string("\"") + Val + std::string("\""); - Out << NormalizedVal; - } - - static StringRef input(StringRef Scalar, void *, std::string &Val) { - Val = Scalar; - return StringRef(); - } -}; - -/// \brief Specialized MappingTraits for Repleacements to be converted to/from -/// a YAML File. -template <> -struct MappingTraits { - /// \brief Normalize clang::tooling::Replacement to provide direct access to - /// its members. - struct NormalizedReplacement { - NormalizedReplacement(const IO &) - : FilePath(""), Offset(0), Length(0), ReplacementText("") {} - - NormalizedReplacement(const IO &, const clang::tooling::Replacement &R) - : FilePath(R.getFilePath()), Offset(R.getOffset()), - Length(R.getLength()), ReplacementText(R.getReplacementText()) {} - - clang::tooling::Replacement denormalize(const IO &) { - return clang::tooling::Replacement(FilePath, Offset, Length, - ReplacementText); - } - - std::string FilePath; - unsigned int Offset; - unsigned int Length; - std::string ReplacementText; - }; - - static void mapping(IO &Io, clang::tooling::Replacement &R) { - MappingNormalization - Keys(Io, R); - Io.mapRequired("Offset", Keys->Offset); - Io.mapRequired("Length", Keys->Length); - Io.mapRequired("ReplacementText", Keys->ReplacementText); - } -}; - -/// \brief Specialized MappingTraits for TransformRepleacements to be converted -/// to/from a YAML File. -template <> -struct MappingTraits { - static void mapping(IO &Io, TransformReplacements &R) { - Io.mapRequired("TransformID", R.TransformID); - Io.mapRequired("Replacements", R.GeneratedReplacements); - } -}; - -/// \brief Specialized MappingTraits for TransformDocument to be converted -/// to/from a YAML File. -template <> -struct MappingTraits { - static void mapping(IO &Io, TransformDocument &TD) { - Io.mapRequired("FileName", TD.FileName); - Io.mapRequired("Transforms", TD.Replacements); - } -}; -} // end namespace yaml -} // end namespace llvm - -#endif // CPP11_MIGRATE_REPLACEMENTS_YAML_H diff --git a/clang-tools-extra/cpp11-migrate/Core/Transform.cpp b/clang-tools-extra/cpp11-migrate/Core/Transform.cpp index bd08716d9741..925206385024 100644 --- a/clang-tools-extra/cpp11-migrate/Core/Transform.cpp +++ b/clang-tools-extra/cpp11-migrate/Core/Transform.cpp @@ -118,7 +118,7 @@ bool Transform::handleBeginSource(CompilerInstance &CI, StringRef Filename) { void Transform::handleEndSource() { if (!getReplacements().empty()) { SourceOverrides &SO = Overrides->getOrCreate(CurrentSource); - SO.applyReplacements(getReplacements(), getName()); + SO.applyReplacements(getReplacements()); } if (Options().EnableTiming) diff --git a/clang-tools-extra/cpp11-migrate/tool/Cpp11Migrate.cpp b/clang-tools-extra/cpp11-migrate/tool/Cpp11Migrate.cpp index e2b06c447993..10370f959e22 100644 --- a/clang-tools-extra/cpp11-migrate/tool/Cpp11Migrate.cpp +++ b/clang-tools-extra/cpp11-migrate/tool/Cpp11Migrate.cpp @@ -325,40 +325,13 @@ int main(int argc, const char **argv) { for (HeaderOverrides::const_iterator HeaderI = Overrides.headers_begin(), HeaderE = Overrides.headers_end(); HeaderI != HeaderE; ++HeaderI) { - assert(!HeaderI->second.getContentOverride().empty() && + assert(!HeaderI->second.FileOverride.empty() && "A header override should not be empty"); std::string ErrorInfo; std::string HeaderFileName = HeaderI->getKey(); llvm::raw_fd_ostream HeaderStream(HeaderFileName.c_str(), ErrorInfo, llvm::sys::fs::F_Binary); - if (!ErrorInfo.empty()) { - llvm::errs() << "Error opening file: " << ErrorInfo << "\n"; - continue; - } - HeaderStream << HeaderI->second.getContentOverride(); - - // Replacements for header files need to be written in a YAML file for - // every transform and will be merged together with an external tool. - llvm::SmallString<128> ReplacementsHeaderName; - llvm::SmallString<64> Error; - bool Result = generateReplacementsFileName(I->getKey(), HeaderFileName, - ReplacementsHeaderName, Error); - if (!Result) { - llvm::errs() << "Failed to generate replacements filename:" << Error - << "\n"; - continue; - } - - llvm::raw_fd_ostream ReplacementsFile(ReplacementsHeaderName.c_str(), - ErrorInfo, - llvm::sys::fs::F_Binary); - if (!ErrorInfo.empty()) { - llvm::errs() << "Error opening file: " << ErrorInfo << "\n"; - continue; - } - llvm::yaml::Output YAML(ReplacementsFile); - YAML << const_cast( - HeaderI->getValue().getTransformReplacementsDoc()); + HeaderStream << HeaderI->second.FileOverride; } } diff --git a/clang-tools-extra/test/cpp11-migrate/HeaderReplacements/common.cpp b/clang-tools-extra/test/cpp11-migrate/HeaderReplacements/common.cpp deleted file mode 100644 index 9c4f0e543ce3..000000000000 --- a/clang-tools-extra/test/cpp11-migrate/HeaderReplacements/common.cpp +++ /dev/null @@ -1,16 +0,0 @@ -// This is just a dummy run command to keep lit happy. Tests for this file are -// in main.cpp -// RUN: true - -#include "common.h" - -void func1(int &I) { -} - -void func2() { - container C1; - container C2; - for (container::iterator I = C1.begin(), E = C1.end(); I != E; ++I) { - C2.push_back(*I); - } -} diff --git a/clang-tools-extra/test/cpp11-migrate/HeaderReplacements/common.h b/clang-tools-extra/test/cpp11-migrate/HeaderReplacements/common.h deleted file mode 100644 index 3bb98cc705c5..000000000000 --- a/clang-tools-extra/test/cpp11-migrate/HeaderReplacements/common.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef CPP11_MIGRATE_TEST_HEADER_REPLACEMENTS_COMMON_H -#define CPP11_MIGRATE_TEST_HEADER_REPLACEMENTS_COMMON_H - -struct container { - struct iterator { - int &operator*(); - const int &operator*() const; - iterator &operator++(); - bool operator!=(const iterator &other); - }; - - iterator begin(); - iterator end(); - void push_back(const int &); -}; - -void func1(int &I); -void func2(); - -void dostuff() { - container C; - for (container::iterator I = C.begin(), E = C.end(); I != E; ++I) { - func1(*I); - } -} - -#endif // CPP11_MIGRATE_TEST_HEADER_REPLACEMENTS_COMMON_H diff --git a/clang-tools-extra/test/cpp11-migrate/HeaderReplacements/common.h.yaml b/clang-tools-extra/test/cpp11-migrate/HeaderReplacements/common.h.yaml deleted file mode 100644 index 21bfa40d1b14..000000000000 --- a/clang-tools-extra/test/cpp11-migrate/HeaderReplacements/common.h.yaml +++ /dev/null @@ -1,12 +0,0 @@ ---- -FileName: "common.h" -Transforms: - - TransformID: "LoopConvert" - Replacements: - - Offset: 432 - Length: 61 - ReplacementText: "(auto & elem : C)" - - Offset: 506 - Length: 2 - ReplacementText: "elem" -... diff --git a/clang-tools-extra/test/cpp11-migrate/HeaderReplacements/main.cpp b/clang-tools-extra/test/cpp11-migrate/HeaderReplacements/main.cpp deleted file mode 100644 index ffbe34c708bf..000000000000 --- a/clang-tools-extra/test/cpp11-migrate/HeaderReplacements/main.cpp +++ /dev/null @@ -1,39 +0,0 @@ -// XFAIL: win32 -// This test fails on windows due to a bug in writing to header replacements. -// -// The following block tests the following: -// - Only 1 file is generated per translation unit and header file -// - Replacements are written in YAML that matches the expected YAML file -// RUN: rm -rf %t/Test -// RUN: mkdir -p %t/Test -// RUN: cp %S/main.cpp %S/common.cpp %S/common.h %t/Test -// RUN: cpp11-migrate -loop-convert -headers -include=%t/Test %t/Test/main.cpp %t/Test/common.cpp -- -// Check that only 1 file is generated per translation unit and header file. -// RUN: ls -1 %t/Test | grep -c "main.cpp_common.h_.*.yaml" | grep "^1$" -// RUN: ls -1 %t/Test | grep -c "common.cpp_common.h_.*.yaml" | grep "^1$" -// We need to remove the path from FileName in the generated YAML file because it will have a path in the temp directory -// RUN: sed -i -e 's/^\(FileName:\).*[\/\\]\(.*\)"$/\1 "\2"/g' %t/Test/main.cpp_common.h_*.yaml -// RUN: sed -i -e 's/^\(FileName:\).*[\/\\]\(.*\)"$/\1 "\2"/g' %t/Test/common.cpp_common.h_*.yaml -// RUN: diff -b %S/common.h.yaml %t/Test/main.cpp_common.h_*.yaml -// RUN: diff -b %S/common.h.yaml %t/Test/common.cpp_common.h_*.yaml -// -// The following block tests the following: -// - YAML files are written only when -headers is used -// RUN: rm -rf %t/Test -// RUN: mkdir -p %t/Test -// RUN: cp %S/main.cpp %S/common.cpp %S/common.h %t/Test -// RUN: cpp11-migrate -loop-convert -headers -include=%t/Test %t/Test/main.cpp -- -// RUN: cpp11-migrate -loop-convert %t/Test/common.cpp -- -// Check that only one YAML file is generated from main.cpp and common.h and not from common.cpp and common.h since -header is not specified -// RUN: ls -1 %t/Test | grep -c "main.cpp_common.h_.*.yaml" | grep "^1$" -// RUN: ls -1 %t/Test | not grep "common.cpp_common.h_.*.yaml" -// We need to remove the path from FileName in the generated YAML file because it will have a path in the temp directory -// RUN: sed -i -e 's/^\(FileName:\).*[\/\\]\(.*\)"$/\1 "\2"/g' %t/Test/main.cpp_common.h_*.yaml -// RUN: diff -b %S/common.h.yaml %t/Test/main.cpp_common.h_*.yaml - -#include "common.h" - -void test_header_replacement() { - dostuff(); - func2(); -} diff --git a/clang-tools-extra/unittests/cpp11-migrate/CMakeLists.txt b/clang-tools-extra/unittests/cpp11-migrate/CMakeLists.txt index 8da010f0dbd7..1f647d5dc194 100644 --- a/clang-tools-extra/unittests/cpp11-migrate/CMakeLists.txt +++ b/clang-tools-extra/unittests/cpp11-migrate/CMakeLists.txt @@ -11,7 +11,6 @@ add_extra_unittest(Cpp11MigrateTests ReformattingTest.cpp IncludeExcludeTest.cpp PerfSupportTest.cpp - ReplacementsYamlTest.cpp TransformTest.cpp UniqueHeaderNameTest.cpp ) diff --git a/clang-tools-extra/unittests/cpp11-migrate/FileOverridesTest.cpp b/clang-tools-extra/unittests/cpp11-migrate/FileOverridesTest.cpp index 382c5a8c737e..39ed662cbecf 100644 --- a/clang-tools-extra/unittests/cpp11-migrate/FileOverridesTest.cpp +++ b/clang-tools-extra/unittests/cpp11-migrate/FileOverridesTest.cpp @@ -30,10 +30,8 @@ TEST(SourceOverridesTest, Interface) { Replacements Replaces; unsigned ReplacementLength = strlen("std::vector::const_iterator"); - Replaces.insert( - Replacement(FileName, 0, ReplacementLength, "auto")); - Overrides.applyReplacements(Replaces, VFHelper.getNewSourceManager(), - "use-auto"); + Replaces.insert(Replacement(FileName, 0, ReplacementLength, "auto")); + Overrides.applyReplacements(Replaces, VFHelper.getNewSourceManager()); EXPECT_TRUE(Overrides.isSourceOverriden()); std::string ExpectedContent = "auto long_type =\n" diff --git a/clang-tools-extra/unittests/cpp11-migrate/IncludeExcludeTest.cpp b/clang-tools-extra/unittests/cpp11-migrate/IncludeExcludeTest.cpp index 6a206df60aab..7aea471c21a0 100644 --- a/clang-tools-extra/unittests/cpp11-migrate/IncludeExcludeTest.cpp +++ b/clang-tools-extra/unittests/cpp11-migrate/IncludeExcludeTest.cpp @@ -7,13 +7,24 @@ // //===----------------------------------------------------------------------===// -#include "Utility.h" #include "Core/IncludeExcludeInfo.h" #include "gtest/gtest.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" #include +// FIXME: copied from unittests/Support/Path.cpp +#define ASSERT_NO_ERROR(x) \ + if (llvm::error_code ASSERT_NO_ERROR_ec = x) { \ + llvm::SmallString<128> MessageStorage; \ + llvm::raw_svector_ostream Message(MessageStorage); \ + Message << #x ": did not return errc::success.\n" \ + << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ + << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ + GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ + } else { \ + } + TEST(IncludeExcludeTest, ParseString) { IncludeExcludeInfo IEManager; llvm::error_code Err = IEManager.readListFromString( diff --git a/clang-tools-extra/unittests/cpp11-migrate/ReformattingTest.cpp b/clang-tools-extra/unittests/cpp11-migrate/ReformattingTest.cpp index a5caa0a3d772..fcde6b81db41 100644 --- a/clang-tools-extra/unittests/cpp11-migrate/ReformattingTest.cpp +++ b/clang-tools-extra/unittests/cpp11-migrate/ReformattingTest.cpp @@ -39,8 +39,7 @@ TEST(Reformatter, SingleReformat) { FileName, Changes, VFHelper.getNewSourceManager()); SourceOverrides Overrides(FileName, /*TrackChanges=*/false); - Overrides.applyReplacements(Replaces, VFHelper.getNewSourceManager(), - "reformatter"); + Overrides.applyReplacements(Replaces, VFHelper.getNewSourceManager()); std::string Expected, Result; diff --git a/clang-tools-extra/unittests/cpp11-migrate/ReplacementsYamlTest.cpp b/clang-tools-extra/unittests/cpp11-migrate/ReplacementsYamlTest.cpp deleted file mode 100644 index 5d009f8812e7..000000000000 --- a/clang-tools-extra/unittests/cpp11-migrate/ReplacementsYamlTest.cpp +++ /dev/null @@ -1,84 +0,0 @@ -//===- unittests/cpp11-migrate/ReplacementsYamlTest.cpp -------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// Test for the Yaml files generated by transforms on header files. -// -//===----------------------------------------------------------------------===// - -#include "Utility.h" -#include "Core/FileOverrides.h" -#include "gtest/gtest.h" - -using namespace llvm; - -TEST(ReplacementsYamlTest, writeReadTest) { - using clang::tooling::Replacement; - - const std::string HeaderFileName = "/path/to/common.h"; - const std::string TransformID = "loop-convert"; - const unsigned int ReplacementOffset1 = 232; - const unsigned int ReplacementLength1 = 56; - const std::string ReplacementText1 = "(auto & elem : V)"; - const unsigned int ReplacementOffset2 = 301; - const unsigned int ReplacementLength2 = 2; - const std::string ReplacementText2 = "elem"; - - TransformReplacements TR; - TR.TransformID = TransformID; - TR.GeneratedReplacements - .push_back(Replacement(HeaderFileName, ReplacementOffset1, - ReplacementLength1, ReplacementText1)); - TR.GeneratedReplacements - .push_back(Replacement(HeaderFileName, ReplacementOffset2, - ReplacementLength2, ReplacementText2)); - - TransformDocument TD; - TD.FileName = HeaderFileName.c_str(); - TD.Replacements.push_back(TR); - - std::string YamlContent; - llvm::raw_string_ostream YamlContentStream(YamlContent); - - // Write to the YAML file. - { - yaml::Output YAML(YamlContentStream); - YAML << TD; - YamlContentStream.str(); - ASSERT_NE(YamlContent.length(), 0u); - } - - // Read from the YAML file and verify that what was written is exactly what - // we read back. - { - TransformDocument TDActual; - yaml::Input YAML(YamlContent); - YAML >> TDActual; - ASSERT_NO_ERROR(YAML.error()); - EXPECT_EQ(HeaderFileName, TDActual.FileName); - ASSERT_EQ(1u, TDActual.Replacements.size()); - - TransformReplacements TRActual = TDActual.Replacements[0]; - EXPECT_EQ(TransformID, TRActual.TransformID); - ASSERT_EQ(2u, TRActual.GeneratedReplacements.size()); - - EXPECT_EQ(ReplacementOffset1, - TRActual.GeneratedReplacements[0].getOffset()); - EXPECT_EQ(ReplacementLength1, - TRActual.GeneratedReplacements[0].getLength()); - EXPECT_EQ(ReplacementText1, - TRActual.GeneratedReplacements[0].getReplacementText().str()); - - EXPECT_EQ(ReplacementOffset2, - TRActual.GeneratedReplacements[1].getOffset()); - EXPECT_EQ(ReplacementLength2, - TRActual.GeneratedReplacements[1].getLength()); - EXPECT_EQ(ReplacementText2, - TRActual.GeneratedReplacements[1].getReplacementText().str()); - } -} diff --git a/clang-tools-extra/unittests/cpp11-migrate/Utility.h b/clang-tools-extra/unittests/cpp11-migrate/Utility.h deleted file mode 100644 index 54291d941a3a..000000000000 --- a/clang-tools-extra/unittests/cpp11-migrate/Utility.h +++ /dev/null @@ -1,25 +0,0 @@ -//=-- cpp11-migrate/Utility.h - Utility functions and macros-----*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef CPP11_MIGRATE_UNITTESTS_UTILITY_H -#define CPP11_MIGRATE_UNITTESTS_UTILITY_H - -// FIXME: copied from unittests/Support/Path.cpp -#define ASSERT_NO_ERROR(x) \ - if (llvm::error_code ASSERT_NO_ERROR_ec = x) { \ - llvm::SmallString<128> MessageStorage; \ - llvm::raw_svector_ostream Message(MessageStorage); \ - Message << #x ": did not return errc::success.\n" \ - << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ - << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ - GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ - } else { \ - } - -#endif // CPP11_MIGRATE_UNITTESTS_UTILITY_H