From: Edwin Vane Date: Mon, 22 Jul 2013 16:36:58 +0000 (+0000) Subject: cpp11-migrate: Make a VirtualFilesHelper class for the unit tests. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=40be0dc26122ca650f5f888329f9b935899e3b73;p=platform%2Fupstream%2Fllvm.git cpp11-migrate: Make a VirtualFilesHelper class for the unit tests. This change makes it easier for new unit tests to create virtual files. Author: Guillaume Papin llvm-svn: 186842 --- diff --git a/clang-tools-extra/unittests/cpp11-migrate/FileOverridesTest.cpp b/clang-tools-extra/unittests/cpp11-migrate/FileOverridesTest.cpp index 015bb2d..3cbdfd3 100644 --- a/clang-tools-extra/unittests/cpp11-migrate/FileOverridesTest.cpp +++ b/clang-tools-extra/unittests/cpp11-migrate/FileOverridesTest.cpp @@ -9,78 +9,16 @@ #include "Core/FileOverrides.h" #include "gtest/gtest.h" -#include "clang/Basic/Diagnostic.h" -#include "clang/Basic/DiagnosticOptions.h" -#include "clang/Basic/FileManager.h" -#include "clang/Basic/SourceManager.h" +#include "VirtualFileHelper.h" -using namespace llvm; using namespace clang; +using namespace clang::tooling; -// Test fixture object that setup some files once for all test cases and remove -// them when the tests are done. -class SourceOverridesTest : public ::testing::Test { -protected: - static void SetUpTestCase() { - DiagOpts = - new IntrusiveRefCntPtr(new DiagnosticOptions()); - Diagnostics = new DiagnosticsEngine( - IntrusiveRefCntPtr(new DiagnosticIDs()), - DiagOpts->getPtr()); - } - - static void TearDownTestCase() { - delete DiagOpts; - delete Diagnostics; - } - - virtual void SetUp() { - Files = new FileManager(FileSystemOptions()); - Sources = NULL; - FileName = NULL; - Code = NULL; - } - - void setFilename(const char *F) { FileName = F; } - void setCode(const char *C) { Code = C; } - - virtual void TearDown() { - delete Files; - delete Sources; - } - - // Creates a new SourceManager with the virtual file and content - SourceManager &getNewSourceManager() { - assert(FileName && Code && "expected Code and FileName to be set."); - delete Sources; - Sources = new SourceManager(*Diagnostics, *Files); - MemoryBuffer *Buf = MemoryBuffer::getMemBuffer(Code, FileName); - const FileEntry *Entry = Files->getVirtualFile( - FileName, Buf->getBufferSize(), /*ModificationTime=*/0); - Sources->overrideFileContents(Entry, Buf); - return *Sources; - } - - static SourceManager *Sources; - static const char *FileName; - static const char *Code; - -private: - static IntrusiveRefCntPtr *DiagOpts; - static DiagnosticsEngine *Diagnostics; - static FileManager *Files; -}; - -IntrusiveRefCntPtr *SourceOverridesTest::DiagOpts = NULL; -DiagnosticsEngine *SourceOverridesTest::Diagnostics = NULL; -FileManager *SourceOverridesTest::Files = NULL; -SourceManager *SourceOverridesTest::Sources = NULL; -const char *SourceOverridesTest::FileName; -const char *SourceOverridesTest::Code; - -TEST_F(SourceOverridesTest, Interface) { - setFilename(""); - setCode( +TEST(SourceOverridesTest, Interface) { + llvm::StringRef FileName = ""; + VirtualFileHelper VFHelper; + VFHelper.mapFile( + FileName, "std::vector::const_iterator long_type =\n" " vec.begin();\n"); SourceOverrides Overrides(FileName); @@ -88,16 +26,14 @@ TEST_F(SourceOverridesTest, Interface) { EXPECT_EQ(FileName, Overrides.getMainFileName()); EXPECT_FALSE(Overrides.isSourceOverriden()); - tooling::Replacements Replaces; + Replacements Replaces; unsigned ReplacementLength = strlen("std::vector::const_iterator"); - Replaces.insert( - tooling::Replacement(FileName, 0, ReplacementLength, "auto")); - Overrides.applyReplacements(Replaces, getNewSourceManager()); + Replaces.insert(Replacement(FileName, 0, ReplacementLength, "auto")); + Overrides.applyReplacements(Replaces, VFHelper.getNewSourceManager()); EXPECT_TRUE(Overrides.isSourceOverriden()); std::string ExpectedContent = "auto long_type =\n" " vec.begin();\n"; - EXPECT_EQ(ExpectedContent, Overrides.getMainFileContent()); } diff --git a/clang-tools-extra/unittests/cpp11-migrate/VirtualFileHelper.h b/clang-tools-extra/unittests/cpp11-migrate/VirtualFileHelper.h new file mode 100644 index 0000000..4ae29fb --- /dev/null +++ b/clang-tools-extra/unittests/cpp11-migrate/VirtualFileHelper.h @@ -0,0 +1,75 @@ +//===--- VirtualFileHelper.h ------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \brief This file defines an utility class for tests that needs a source +/// manager for a virtual file with customizable content. +/// +//===----------------------------------------------------------------------===// + +#ifndef CPP11_MIGRATE_VIRTUAL_FILE_HELPER_H +#define CPP11_MIGRATE_VIRTUAL_FILE_HELPER_H + +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/DiagnosticOptions.h" +#include "clang/Basic/FileManager.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Frontend/TextDiagnosticPrinter.h" + +namespace clang { + +/// \brief Class that provides easy access to a SourceManager and that allows to +/// map virtual files conveniently. +class VirtualFileHelper { + struct VirtualFile { + llvm::StringRef FileName; + llvm::StringRef Code; + }; + +public: + VirtualFileHelper() + : DiagOpts(new DiagnosticOptions()), + Diagnostics(IntrusiveRefCntPtr(new DiagnosticIDs), + &*DiagOpts), + DiagnosticPrinter(llvm::outs(), &*DiagOpts), + Files((FileSystemOptions())) {} + + /// \brief Create a virtual file \p FileName, with content \p Code. + void mapFile(llvm::StringRef FileName, llvm::StringRef Code) { + VirtualFile VF = { FileName, Code }; + VirtualFiles.push_back(VF); + } + + /// \brief Create a new \c SourceManager with the virtual files and contents + /// mapped to it. + SourceManager &getNewSourceManager() { + Sources.reset(new SourceManager(Diagnostics, Files)); + for (llvm::SmallVectorImpl::iterator I = VirtualFiles.begin(), + E = VirtualFiles.end(); + I != E; ++I) { + llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(I->Code); + const FileEntry *Entry = Files.getVirtualFile( + I->FileName, Buf->getBufferSize(), /*ModificationTime=*/0); + Sources->overrideFileContents(Entry, Buf); + } + return *Sources; + } + +private: + IntrusiveRefCntPtr DiagOpts; + DiagnosticsEngine Diagnostics; + TextDiagnosticPrinter DiagnosticPrinter; + FileManager Files; + // most tests don't need more than one file + llvm::SmallVector VirtualFiles; + llvm::OwningPtr Sources; +}; + +} // end namespace clang + +#endif // CPP11_MIGRATE_VIRTUAL_FILE_HELPER_H