[clang-tools-extra] r188274 - cpp11-migrate: Write header replacements to disk

Siraj, Tareq A tareq.a.siraj at intel.com
Tue Aug 13 08:40:06 PDT 2013


Yes, noticed it on IRC, doing a Darwin build to see what went wrong right now. Thanks.

--
Tareq A. Siraj 
  Software Developer
  Intel of Canada, Inc.

-----Original Message-----
From: Arnold Schwaighofer [mailto:aschwaighofer at apple.com] 
Sent: Tuesday, August 13, 2013 11:37 AM
To: Siraj, Tareq A
Cc: cfe-commits at cs.uiuc.edu
Subject: Re: [clang-tools-extra] r188274 - cpp11-migrate: Write header replacements to disk

Hi Tareq,

The commit 188274 is breaking the public bot

http://lab.llvm.org:8013/builders/clang-x86_64-darwin11-nobootstrap-RAincremental/builds/5700


can you please fix or revert.

Thanks,
Arnold


On Aug 13, 2013, at 9:58 AM, Tareq A. Siraj <tareq.a.siraj at intel.com> wrote:

> Author: tasiraj
> Date: Tue Aug 13 09:58:50 2013
> New Revision: 188274
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=188274&view=rev
> Log:
> cpp11-migrate: Write header replacements to disk
> 
> Another attempt to commit r187204 after windows related problems has 
> been fixed. Note that changes to this patch reflect the current 
> behavior of cpp11-migrate.
> 
> Header replacements are now written to disk in YAML format for an 
> external tool to merge. A unique file will be created in the same 
> directory as the header with all replacements that came from a source 
> file that included the header file. The YAML file will have:
> - Name of the header file
> - Name of the source file that included the header file
> - Transform ID that generated the replacement
> - Offset
> - Length
> - Replacement text
> 
> Any tool reading these replacements should read them using the 
> HeaderChangeDocument struct.
> 
> Differential Revision: http://llvm-reviews.chandlerc.com/D1369
> 
> 
> Added:
>    clang-tools-extra/trunk/cpp11-migrate/Core/ReplacementsYaml.h
>    clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/common.cpp
>    clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/common.h
>    clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/common.h.yaml
>    clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/main.cpp
>    clang-tools-extra/trunk/unittests/cpp11-migrate/ReplacementsYamlTest.cpp
>    clang-tools-extra/trunk/unittests/cpp11-migrate/Utility.h
> Modified:
>    clang-tools-extra/trunk/cpp11-migrate/Core/FileOverrides.cpp
>    clang-tools-extra/trunk/cpp11-migrate/Core/FileOverrides.h
>    clang-tools-extra/trunk/cpp11-migrate/Core/Reformatting.cpp
>    clang-tools-extra/trunk/cpp11-migrate/Core/Transform.cpp
>    clang-tools-extra/trunk/cpp11-migrate/tool/Cpp11Migrate.cpp
>    clang-tools-extra/trunk/unittests/cpp11-migrate/CMakeLists.txt
>    clang-tools-extra/trunk/unittests/cpp11-migrate/FileOverridesTest.cpp
>    clang-tools-extra/trunk/unittests/cpp11-migrate/IncludeExcludeTest.cpp
>    
> clang-tools-extra/trunk/unittests/cpp11-migrate/ReformattingTest.cpp
> 
> Modified: clang-tools-extra/trunk/cpp11-migrate/Core/FileOverrides.cpp
> URL: 
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/cpp11-migr
> ate/Core/FileOverrides.cpp?rev=188274&r1=188273&r2=188274&view=diff
> ======================================================================
> ========
> --- clang-tools-extra/trunk/cpp11-migrate/Core/FileOverrides.cpp 
> (original)
> +++ clang-tools-extra/trunk/cpp11-migrate/Core/FileOverrides.cpp Tue 
> +++ Aug 13 09:58:50 2013
> @@ -23,16 +23,25 @@
> #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) {
> +  HeaderChangeDoc.TransformID = TransformID;
> +  HeaderChangeDoc.Replacements.resize(Replaces.size());
> +  std::copy(Replaces.begin(), Replaces.end(),
> +            HeaderChangeDoc.Replacements.begin());
> +}
> +
> SourceOverrides::SourceOverrides(llvm::StringRef MainFileName,
>                                  bool TrackChanges)
>     : MainFileName(MainFileName), TrackChanges(TrackChanges) {}
> 
> -void
> -SourceOverrides::applyReplacements(clang::tooling::Replacements 
> &Replaces) {
> +void SourceOverrides::applyReplacements(tooling::Replacements &Replaces,
> +                                        llvm::StringRef 
> +TransformName) {
>   llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts(
>       new DiagnosticOptions());
>   DiagnosticsEngine Diagnostics(
> @@ -40,11 +49,12 @@ SourceOverrides::applyReplacements(clang
>       DiagOpts.getPtr());
>   FileManager Files((FileSystemOptions()));
>   SourceManager SM(Diagnostics, Files);
> -  applyReplacements(Replaces, SM);
> +  applyReplacements(Replaces, SM, TransformName);
> }
> 
> -void SourceOverrides::applyReplacements(clang::tooling::Replacements &Replaces,
> -                                        clang::SourceManager &SM) {
> +void SourceOverrides::applyReplacements(tooling::Replacements &Replaces,
> +                                        SourceManager &SM,
> +                                        llvm::StringRef 
> +TransformName) {
>   applyOverrides(SM);
> 
>   Rewriter Rewrites(SM, LangOptions()); @@ -56,12 +66,6 @@ void 
> SourceOverrides::applyReplacements(
>   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(), @@ 
> -89,36 +93,50 @@ void SourceOverrides::applyRewrites(Rewr
>     // 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.FileName.empty())
> -      HeaderOv.FileName = FileName;
> +    if (HeaderOv.getFileName().empty())
> +      HeaderOv = HeaderOverride(FileName, MainFileName);
> +
> +    HeaderOv.swapContentOverride(ResultBuf);
>   }
> -}
> 
> -void SourceOverrides::adjustChangedRanges(const Replacements 
> &Replaces) {
> -  // Start by grouping replacements by file name
> +  // Separate replacements to header files
>   Replacements MainFileReplaces;
> -  llvm::StringMap<Replacements> HeadersReplaces;
> -
> -  for (Replacements::iterator I = Replaces.begin(), E = Replaces.end(); I != E;
> -       ++I) {
> +  ReplacementsMap HeadersReplaces;
> +  for (Replacements::const_iterator I = Replaces.begin(), E = Replaces.end();
> +      I != E; ++I) {
>     llvm::StringRef ReplacementFileName = I->getFilePath();
> 
> -    if (ReplacementFileName == MainFileName)
> +    if (ReplacementFileName == MainFileName) {
>       MainFileReplaces.insert(*I);
> -    else
> -      HeadersReplaces[ReplacementFileName].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());
>   }
> 
> -  // Then adjust the changed ranges for each individual file
> -  MainFileChanges.adjustChangedRanges(Replaces);
> -  for (llvm::StringMap<Replacements>::iterator I = HeadersReplaces.begin(),
> -                                               E = HeadersReplaces.end();
> +  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();
>        I != E; ++I) {
> -    Headers[I->getKey()].Changes.adjustChangedRanges(I->getValue());
> +    Headers[I->getKey()].adjustChangedRanges(I->getValue());
>   }
> }
> 
> @@ -131,11 +149,11 @@ void SourceOverrides::applyOverrides(Sou
> 
>   for (HeaderOverrides::const_iterator I = Headers.begin(), E = Headers.end();
>        I != E; ++I) {
> -    assert(!I->second.FileOverride.empty() &&
> +    assert(!I->second.getContentOverride().empty() &&
>            "Header override should not be empty!");
>     SM.overrideFileContents(
> -        FM.getFile(I->second.FileName),
> -        llvm::MemoryBuffer::getMemBuffer(I->second.FileOverride));
> +        FM.getFile(I->second.getFileName()),
> +        
> + llvm::MemoryBuffer::getMemBuffer(I->second.getContentOverride()));
>   }
> }
> 
> 
> Modified: clang-tools-extra/trunk/cpp11-migrate/Core/FileOverrides.h
> URL: 
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/cpp11-migr
> ate/Core/FileOverrides.h?rev=188274&r1=188273&r2=188274&view=diff
> ======================================================================
> ========
> --- clang-tools-extra/trunk/cpp11-migrate/Core/FileOverrides.h 
> (original)
> +++ clang-tools-extra/trunk/cpp11-migrate/Core/FileOverrides.h Tue Aug 
> +++ 13 09:58:50 2013
> @@ -16,6 +16,7 @@
> #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"
> 
> @@ -58,13 +59,54 @@ private:
> };
> 
> /// \brief Container for storing override information for a single headers.
> -struct HeaderOverride {
> +class HeaderOverride {
> +public:
> +  /// \brief Constructors.
> +  /// @{
>   HeaderOverride() {}
> -  HeaderOverride(llvm::StringRef FileName) : FileName(FileName) {}
> +  HeaderOverride(llvm::StringRef HeaderFileName,
> +                 llvm::StringRef SourceFileName) {
> +    HeaderChangeDoc.HeaderFileName = HeaderFileName;
> +    HeaderChangeDoc.SourceFileName = SourceFileName;  }  /// @}
> +
> +  /// \brief Getter for FileName.
> +  llvm::StringRef getFileName() const {
> +    return HeaderChangeDoc.HeaderFileName;  }
> +
> +  /// \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; }
> 
> -  std::string FileName;
> -  std::string FileOverride;
> +  /// \brief Swaps the content of ContentOverride with \param S  void 
> + swapContentOverride(std::string &S) { ContentOverride.swap(S); }
> +
> +  /// \brief Getter for HeaderChangeDoc.
> +  const HeaderChangeDocument &getHeaderChangeDoc() const {
> +    return HeaderChangeDoc;
> +  }
> +
> +  /// \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;
>   ChangedRanges Changes;
> +  HeaderChangeDocument HeaderChangeDoc;
> };
> 
> /// \brief Container mapping header file names to override information.
> @@ -100,8 +142,10 @@ 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);
> -  void applyReplacements(clang::tooling::Replacements &Replaces);
> +                         clang::SourceManager &SM,
> +                         llvm::StringRef TransformName);  void 
> + applyReplacements(clang::tooling::Replacements &Replaces,
> +                         llvm::StringRef TransformName);
> 
>   /// \brief Convenience function for applying this source's overrides to
>   /// the given SourceManager.
> @@ -118,13 +162,16 @@ 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);
> +  void adjustChangedRanges(const clang::tooling::Replacements &Replaces,
> +                           const ReplacementsMap &HeadersReplaces);
> 
>   const std::string MainFileName;
>   std::string MainFileOverride;
> 
> Modified: clang-tools-extra/trunk/cpp11-migrate/Core/Reformatting.cpp
> URL: 
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/cpp11-migr
> ate/Core/Reformatting.cpp?rev=188274&r1=188273&r2=188274&view=diff
> ======================================================================
> ========
> --- clang-tools-extra/trunk/cpp11-migrate/Core/Reformatting.cpp 
> (original)
> +++ clang-tools-extra/trunk/cpp11-migrate/Core/Reformatting.cpp Tue 
> +++ Aug 13 09:58:50 2013
> @@ -47,10 +47,10 @@ void Reformatter::reformatChanges(Source
>        I != E; ++I) {
>     const HeaderOverride &Header = I->getValue();
>     const tooling::Replacements &HeaderReplaces =
> -        reformatSingleFile(Header.FileName, Header.Changes, SM);
> +        reformatSingleFile(Header.getFileName(), Header.getChanges(), 
> + SM);
>     Replaces.insert(HeaderReplaces.begin(), HeaderReplaces.end());
>   }
> -  Overrides.applyReplacements(Replaces, SM);
> +  Overrides.applyReplacements(Replaces, SM, "reformatter");
> }
> 
> tooling::Replacements Reformatter::reformatSingleFile(
> 
> Added: clang-tools-extra/trunk/cpp11-migrate/Core/ReplacementsYaml.h
> URL: 
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/cpp11-migr
> ate/Core/ReplacementsYaml.h?rev=188274&view=auto
> ======================================================================
> ========
> --- clang-tools-extra/trunk/cpp11-migrate/Core/ReplacementsYaml.h 
> (added)
> +++ clang-tools-extra/trunk/cpp11-migrate/Core/ReplacementsYaml.h Tue 
> +++ Aug 13 09:58:50 2013
> @@ -0,0 +1,102 @@
> +//===-- 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 The top-level YAML document that contains the details for 
> +the /// replacement.
> +struct HeaderChangeDocument {
> +  std::string TransformID;
> +  std::vector<clang::tooling::Replacement> Replacements;
> +  std::string HeaderFileName;
> +  std::string SourceFileName;
> +};
> +
> +LLVM_YAML_IS_SEQUENCE_VECTOR(clang::tooling::Replacement)
> +
> +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 HeaderChangeDocument to be 
> +converted /// to/from a YAML File.
> +template <>
> +struct MappingTraits<HeaderChangeDocument> {
> +  static void mapping(IO &Io, HeaderChangeDocument &TD) {
> +    Io.mapRequired("TransformID", TD.TransformID);
> +    Io.mapRequired("Replacements", TD.Replacements);
> +    Io.mapRequired("HeaderFileName", TD.HeaderFileName);
> +    Io.mapRequired("SourceFileName", TD.SourceFileName);
> +  }
> +};
> +} // end namespace yaml
> +} // end namespace llvm
> +
> +#endif // CPP11_MIGRATE_REPLACEMENTS_YAML_H
> 
> Modified: clang-tools-extra/trunk/cpp11-migrate/Core/Transform.cpp
> URL: 
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/cpp11-migr
> ate/Core/Transform.cpp?rev=188274&r1=188273&r2=188274&view=diff
> ======================================================================
> ========
> --- clang-tools-extra/trunk/cpp11-migrate/Core/Transform.cpp 
> (original)
> +++ clang-tools-extra/trunk/cpp11-migrate/Core/Transform.cpp Tue Aug 
> +++ 13 09:58:50 2013
> @@ -118,7 +118,7 @@ bool Transform::handleBeginSource(Compil
> void Transform::handleEndSource() {
>   if (!getReplacements().empty()) {
>     SourceOverrides &SO = Overrides->getOrCreate(CurrentSource);
> -    SO.applyReplacements(getReplacements());
> +    SO.applyReplacements(getReplacements(), getName());
>   }
> 
>   if (Options().EnableTiming)
> 
> Modified: clang-tools-extra/trunk/cpp11-migrate/tool/Cpp11Migrate.cpp
> URL: 
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/cpp11-migr
> ate/tool/Cpp11Migrate.cpp?rev=188274&r1=188273&r2=188274&view=diff
> ======================================================================
> ========
> --- clang-tools-extra/trunk/cpp11-migrate/tool/Cpp11Migrate.cpp 
> (original)
> +++ clang-tools-extra/trunk/cpp11-migrate/tool/Cpp11Migrate.cpp Tue 
> +++ Aug 13 09:58:50 2013
> @@ -325,13 +325,40 @@ 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.FileOverride.empty() &&
> +      assert(!HeaderI->second.getContentOverride().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);
> -      HeaderStream << HeaderI->second.FileOverride;
> +      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<HeaderChangeDocument &>(
> +                  HeaderI->getValue().getHeaderChangeDoc());
>     }
>   }
> 
> 
> Added: 
> clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/common.c
> pp
> URL: 
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/cpp11
> -migrate/HeaderReplacements/common.cpp?rev=188274&view=auto
> ======================================================================
> ========
> --- 
> clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/common.c
> pp (added)
> +++ clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/comm
> +++ on.cpp Tue Aug 13 09:58:50 2013
> @@ -0,0 +1,16 @@
> +// 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);
> +  }
> +}
> 
> Added: 
> clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/common.h
> URL: 
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/cpp11
> -migrate/HeaderReplacements/common.h?rev=188274&view=auto
> ======================================================================
> ========
> --- 
> clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/common.h 
> (added)
> +++ clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/comm
> +++ on.h Tue Aug 13 09:58:50 2013
> @@ -0,0 +1,27 @@
> +#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
> 
> Added: 
> clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/common.h
> .yaml
> URL: 
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/cpp11
> -migrate/HeaderReplacements/common.h.yaml?rev=188274&view=auto
> ======================================================================
> ========
> --- 
> clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/common.h
> .yaml (added)
> +++ clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/comm
> +++ on.h.yaml Tue Aug 13 09:58:50 2013
> @@ -0,0 +1,12 @@
> +---
> +TransformID:     "LoopConvert"
> +Replacements:
> +  - Offset:          432
> +    Length:          61
> +    ReplacementText: "(auto & elem : C)"
> +  - Offset:          506
> +    Length:          2
> +    ReplacementText: "elem"
> +HeaderFileName: "common.h"
> +SourceFileName: "main.cpp"
> +...
> 
> Added: 
> clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/main.cpp
> URL: 
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/cpp11
> -migrate/HeaderReplacements/main.cpp?rev=188274&view=auto
> ======================================================================
> ========
> --- 
> clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/main.cpp 
> (added)
> +++ clang-tools-extra/trunk/test/cpp11-migrate/HeaderReplacements/main
> +++ .cpp Tue Aug 13 09:58:50 2013
> @@ -0,0 +1,39 @@
> +// 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 FileNames in the generated YAML 
> +file because it will have a path in the temp directory // RUN: sed -i 
> +-e 's/^\(HeaderFileName:\|SourceFileName:\).*[\/\\]\(.*\)"$/\1 
> +"\2"/g' %t/Test/main.cpp_common.h_*.yaml // RUN: cp %S/common.h.yaml 
> +%t/Test/main.cpp_common.h.yaml // RUN: diff -b 
> +%t/Test/main.cpp_common.h.yaml %t/Test/main.cpp_common.h_*.yaml // 
> +RUN: sed -i -e 
> +'s/^\(HeaderFileName:\|SourceFileName:\).*[\/\\]\(.*\)"$/\1 "\2"/g' 
> +%t/Test/common.cpp_common.h_*.yaml
> +// RUN: cp %S/common.h.yaml %t/Test/common.cpp_common.h.yaml // RUN: 
> +sed -i -e 's/^SourceFileName: "main.cpp"$/SourceFileName: 
> +"common.cpp"/g' %t/Test/common.cpp_common.h.yaml // RUN: diff -b 
> +%t/Test/common.cpp_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/^\(HeaderFileName:\|SourceFileName:\).*[\/\\]\(.*\)"$/\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();
> +}
> 
> Modified: 
> clang-tools-extra/trunk/unittests/cpp11-migrate/CMakeLists.txt
> URL: 
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/
> cpp11-migrate/CMakeLists.txt?rev=188274&r1=188273&r2=188274&view=diff
> ======================================================================
> ========
> --- clang-tools-extra/trunk/unittests/cpp11-migrate/CMakeLists.txt 
> (original)
> +++ clang-tools-extra/trunk/unittests/cpp11-migrate/CMakeLists.txt Tue 
> +++ Aug 13 09:58:50 2013
> @@ -11,6 +11,7 @@ add_extra_unittest(Cpp11MigrateTests
>   ReformattingTest.cpp
>   IncludeExcludeTest.cpp
>   PerfSupportTest.cpp
> +  ReplacementsYamlTest.cpp
>   TransformTest.cpp
>   UniqueHeaderNameTest.cpp
>   )
> 
> Modified: 
> clang-tools-extra/trunk/unittests/cpp11-migrate/FileOverridesTest.cpp
> URL: 
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/
> cpp11-migrate/FileOverridesTest.cpp?rev=188274&r1=188273&r2=188274&vie
> w=diff 
> ======================================================================
> ========
> --- 
> clang-tools-extra/trunk/unittests/cpp11-migrate/FileOverridesTest.cpp 
> (original)
> +++ clang-tools-extra/trunk/unittests/cpp11-migrate/FileOverridesTest.
> +++ cpp Tue Aug 13 09:58:50 2013
> @@ -30,8 +30,10 @@ 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());
> +  Replaces.insert(
> +      Replacement(FileName, 0, ReplacementLength, "auto"));  
> + Overrides.applyReplacements(Replaces, VFHelper.getNewSourceManager(),
> +                              "use-auto");
>   EXPECT_TRUE(Overrides.isSourceOverriden());
> 
>   std::string ExpectedContent = "auto long_type =\n"
> 
> Modified: 
> clang-tools-extra/trunk/unittests/cpp11-migrate/IncludeExcludeTest.cpp
> URL: 
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/
> cpp11-migrate/IncludeExcludeTest.cpp?rev=188274&r1=188273&r2=188274&vi
> ew=diff 
> ======================================================================
> ========
> --- 
> clang-tools-extra/trunk/unittests/cpp11-migrate/IncludeExcludeTest.cpp 
> (original)
> +++ clang-tools-extra/trunk/unittests/cpp11-migrate/IncludeExcludeTest
> +++ .cpp Tue Aug 13 09:58:50 2013
> @@ -7,24 +7,13 @@
> //
> //===-----------------------------------------------------------------
> -----===//
> 
> +#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(
> 
> Modified: 
> clang-tools-extra/trunk/unittests/cpp11-migrate/ReformattingTest.cpp
> URL: 
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/
> cpp11-migrate/ReformattingTest.cpp?rev=188274&r1=188273&r2=188274&view
> =diff 
> ======================================================================
> ========
> --- 
> clang-tools-extra/trunk/unittests/cpp11-migrate/ReformattingTest.cpp 
> (original)
> +++ clang-tools-extra/trunk/unittests/cpp11-migrate/ReformattingTest.c
> +++ pp Tue Aug 13 09:58:50 2013
> @@ -39,7 +39,8 @@ TEST(Reformatter, SingleReformat) {
>       FileName, Changes, VFHelper.getNewSourceManager());
> 
>   SourceOverrides Overrides(FileName, /*TrackChanges=*/false);
> -  Overrides.applyReplacements(Replaces, 
> VFHelper.getNewSourceManager());
> +  Overrides.applyReplacements(Replaces, VFHelper.getNewSourceManager(),
> +                              "reformatter");
> 
>   std::string Expected, Result;
> 
> 
> Added: 
> clang-tools-extra/trunk/unittests/cpp11-migrate/ReplacementsYamlTest.c
> pp
> URL: 
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/
> cpp11-migrate/ReplacementsYamlTest.cpp?rev=188274&view=auto
> ======================================================================
> ========
> --- 
> clang-tools-extra/trunk/unittests/cpp11-migrate/ReplacementsYamlTest.c
> pp (added)
> +++ clang-tools-extra/trunk/unittests/cpp11-migrate/ReplacementsYamlTe
> +++ st.cpp Tue Aug 13 09:58:50 2013
> @@ -0,0 +1,76 @@
> +//===- 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 SourceFileName = "/path/to/source.cpp";  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";
> +
> +  HeaderChangeDocument HCD;
> +  HCD.TransformID = TransformID;
> +  HCD.Replacements.push_back(Replacement(HeaderFileName, ReplacementOffset1,
> +                                        ReplacementLength1, 
> + ReplacementText1));  HCD.Replacements.push_back(Replacement(HeaderFileName, ReplacementOffset2,
> +                                        ReplacementLength2, 
> + ReplacementText2));
> +
> +  HCD.HeaderFileName = HeaderFileName.c_str();  HCD.SourceFileName = 
> + SourceFileName.c_str();
> +
> +  std::string YamlContent;
> +  llvm::raw_string_ostream YamlContentStream(YamlContent);
> +
> +  // Write to the YAML file.
> +  {
> +    yaml::Output YAML(YamlContentStream);
> +    YAML << HCD;
> +    YamlContentStream.str();
> +    ASSERT_NE(YamlContent.length(), 0u);  }
> +
> +  // Read from the YAML file and verify that what was written is 
> + exactly what  // we read back.
> +  {
> +    HeaderChangeDocument HCDActual;
> +    yaml::Input YAML(YamlContent);
> +    YAML >> HCDActual;
> +    ASSERT_NO_ERROR(YAML.error());
> +    EXPECT_EQ(HeaderFileName, HCDActual.HeaderFileName);
> +    EXPECT_EQ(SourceFileName, HCDActual.SourceFileName);
> +    EXPECT_EQ(TransformID, HCDActual.TransformID);
> +    ASSERT_EQ(2u, HCDActual.Replacements.size());
> +
> +    EXPECT_EQ(ReplacementOffset1, HCDActual.Replacements[0].getOffset());
> +    EXPECT_EQ(ReplacementLength1, HCDActual.Replacements[0].getLength());
> +    EXPECT_EQ(ReplacementText1,
> +              HCDActual.Replacements[0].getReplacementText().str());
> +
> +    EXPECT_EQ(ReplacementOffset2, HCDActual.Replacements[1].getOffset());
> +    EXPECT_EQ(ReplacementLength2, HCDActual.Replacements[1].getLength());
> +    EXPECT_EQ(ReplacementText2,
> +              HCDActual.Replacements[1].getReplacementText().str());
> +  }
> +}
> 
> Added: clang-tools-extra/trunk/unittests/cpp11-migrate/Utility.h
> URL: 
> http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/
> cpp11-migrate/Utility.h?rev=188274&view=auto
> ======================================================================
> ========
> --- clang-tools-extra/trunk/unittests/cpp11-migrate/Utility.h (added)
> +++ clang-tools-extra/trunk/unittests/cpp11-migrate/Utility.h Tue Aug 
> +++ 13 09:58:50 2013
> @@ -0,0 +1,25 @@
> +//=-- 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
> 
> 
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits





More information about the cfe-commits mailing list