[PATCH] cpp11-migrate: Make a VirtualFilesHelper class for the unit tests.
Guillaume Papin
guillaume.papin at epitech.eu
Sat Jul 20 11:04:25 PDT 2013
Hi revane, arielbernal, tareqsiraj,
This change makes it easier for new unit tests to create virtual files.
It is similar to http://llvm-reviews.chandlerc.com/D1155 except now it's simpler
and can be re-used by some other tests (my next patch for LibFormat integrate
make use of it).
http://llvm-reviews.chandlerc.com/D1190
Files:
unittests/cpp11-migrate/FileOverridesTest.cpp
unittests/cpp11-migrate/VirtualFileHelper.h
Index: unittests/cpp11-migrate/FileOverridesTest.cpp
===================================================================
--- unittests/cpp11-migrate/FileOverridesTest.cpp
+++ unittests/cpp11-migrate/FileOverridesTest.cpp
@@ -9,95 +9,31 @@
#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<DiagnosticOptions>(new DiagnosticOptions());
- Diagnostics = new DiagnosticsEngine(
- IntrusiveRefCntPtr<DiagnosticIDs>(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<DiagnosticOptions> *DiagOpts;
- static DiagnosticsEngine *Diagnostics;
- static FileManager *Files;
-};
-
-IntrusiveRefCntPtr<DiagnosticOptions> *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("<test-file>");
- setCode(
+TEST(SourceOverridesTest, Interface) {
+ llvm::StringRef FileName = "<test-file>";
+ VirtualFileHelper VFHelper;
+ VFHelper.mapFile(
+ FileName,
"std::vector<such_a_long_name_for_a_type>::const_iterator long_type =\n"
" vec.begin();\n");
SourceOverrides Overrides(FileName);
EXPECT_EQ(FileName, Overrides.getMainFileName());
EXPECT_FALSE(Overrides.isSourceOverriden());
- tooling::Replacements Replaces;
+ Replacements Replaces;
unsigned ReplacementLength =
strlen("std::vector<such_a_long_name_for_a_type>::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());
}
Index: unittests/cpp11-migrate/VirtualFileHelper.h
===================================================================
--- /dev/null
+++ 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<DiagnosticIDs>(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<VirtualFile>::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<DiagnosticOptions> DiagOpts;
+ DiagnosticsEngine Diagnostics;
+ TextDiagnosticPrinter DiagnosticPrinter;
+ FileManager Files;
+ // most tests don't need more than one file
+ llvm::SmallVector<VirtualFile, 1> VirtualFiles;
+ llvm::OwningPtr<SourceManager> Sources;
+};
+
+} // end namespace clang
+
+#endif // CPP11_MIGRATE_VIRTUAL_FILE_HELPER_H
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1190.1.patch
Type: text/x-patch
Size: 6619 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130720/e691a6cd/attachment.bin>
More information about the cfe-commits
mailing list