forked from OSchip/llvm-project
Revert "cpp11-migrate: Write header replacements to disk"
This reverts commit 187428. It broke the windows bots. http://bb.pgr.jp/builders/ninja-clang-i686-msc17-R/builds/3450 llvm-svn: 187447
This commit is contained in:
parent
4c80bfbd53
commit
0ff671b3be
|
@ -23,26 +23,16 @@
|
|||
#include "llvm/Support/Path.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
#include "llvm/Support/system_error.h"
|
||||
#include <algorithm>
|
||||
|
||||
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<clang::DiagnosticOptions> 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<Replacements> 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<Replacements>::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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<clang::tooling::Replacements> 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;
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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 <vector>
|
||||
#include <string>
|
||||
|
||||
/// \brief A replacement struct to store the transform ID and the replacement.
|
||||
struct TransformReplacements {
|
||||
std::string TransformID;
|
||||
std::vector<clang::tooling::Replacement> GeneratedReplacements;
|
||||
};
|
||||
|
||||
/// \brief The top-level YAML document that contains the name of the file and
|
||||
/// the TransformReplacements.
|
||||
struct TransformDocument {
|
||||
std::string FileName;
|
||||
std::vector<TransformReplacements> 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<std::string> {
|
||||
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<clang::tooling::Replacement> {
|
||||
/// \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<NormalizedReplacement, clang::tooling::Replacement>
|
||||
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<TransformReplacements> {
|
||||
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<TransformDocument> {
|
||||
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
|
|
@ -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)
|
||||
|
|
|
@ -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<TransformDocument &>(
|
||||
HeaderI->getValue().getTransformReplacementsDoc());
|
||||
HeaderStream << HeaderI->second.FileOverride;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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
|
|
@ -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"
|
||||
...
|
|
@ -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();
|
||||
}
|
|
@ -11,7 +11,6 @@ add_extra_unittest(Cpp11MigrateTests
|
|||
ReformattingTest.cpp
|
||||
IncludeExcludeTest.cpp
|
||||
PerfSupportTest.cpp
|
||||
ReplacementsYamlTest.cpp
|
||||
TransformTest.cpp
|
||||
UniqueHeaderNameTest.cpp
|
||||
)
|
||||
|
|
|
@ -30,10 +30,8 @@ TEST(SourceOverridesTest, Interface) {
|
|||
Replacements Replaces;
|
||||
unsigned ReplacementLength =
|
||||
strlen("std::vector<such_a_long_name_for_a_type>::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"
|
||||
|
|
|
@ -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 <fstream>
|
||||
|
||||
// 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(
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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
|
Loading…
Reference in New Issue