aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2016-08-31 00:07:38 +0000
committerDavid Malcolm <dmalcolm@gcc.gnu.org>2016-08-31 00:07:38 +0000
commit4ecfc45373b2f6bf13ff978f8b6f9411ad13b764 (patch)
tree1a93335308b8e648e3693e8973cfdf8678cb1bfc /gcc
parent71b308914e31765f2110e6692685e0842c317bed (diff)
downloadgcc-4ecfc45373b2f6bf13ff978f8b6f9411ad13b764.zip
gcc-4ecfc45373b2f6bf13ff978f8b6f9411ad13b764.tar.gz
gcc-4ecfc45373b2f6bf13ff978f8b6f9411ad13b764.tar.bz2
selftest: split out named_temp_file from temp_source_file
Split out a new base class for temp_source_file, named_temp_file, moving the deletion to the base class dtor, so that we can write out temporary files in other ways in selftests. gcc/ChangeLog: * selftest.c (selftest::named_temp_file::named_temp_file): New ctor. (selftest::temp_source_file::~temp_source_file): Move to... (selftest::named_temp_file::~named_temp_file): ...here. (selftest::test_named_temp_file): New function. (selftest::selftest_c_tests): Call test_named_temp_file. * selftest.h (class named_temp_file): New class. (class temp_source_file): Convert to a subclass of named_temp_file. From-SVN: r239875
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog11
-rw-r--r--gcc/selftest.c49
-rw-r--r--gcc/selftest.h24
3 files changed, 63 insertions, 21 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index d43d00e..fa764e8 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,14 @@
+2016-08-30 David Malcolm <dmalcolm@redhat.com>
+
+ * selftest.c (selftest::named_temp_file::named_temp_file): New
+ ctor.
+ (selftest::temp_source_file::~temp_source_file): Move to...
+ (selftest::named_temp_file::~named_temp_file): ...here.
+ (selftest::test_named_temp_file): New function.
+ (selftest::selftest_c_tests): Call test_named_temp_file.
+ * selftest.h (class named_temp_file): New class.
+ (class temp_source_file): Convert to a subclass of named_temp_file.
+
2016-08-30 Segher Boessenkool <segher@kernel.crashing.org>
* config/rs6000/rs6000.c (rs6000_emit_epilogue): Do not emit
diff --git a/gcc/selftest.c b/gcc/selftest.c
index 629db98..e6c9510 100644
--- a/gcc/selftest.c
+++ b/gcc/selftest.c
@@ -120,34 +120,40 @@ selftest::assert_str_contains (const location &loc,
desc_haystack, desc_needle, val_haystack, val_needle);
}
-/* Constructor. Create a tempfile using SUFFIX, and write CONTENT to
- it. Abort if anything goes wrong, using LOC as the effective
- location in the problem report. */
+/* Constructor. Generate a name for the file. */
-selftest::temp_source_file::temp_source_file (const location &loc,
- const char *suffix,
- const char *content)
+selftest::named_temp_file::named_temp_file (const char *suffix)
{
m_filename = make_temp_file (suffix);
ASSERT_NE (m_filename, NULL);
-
- FILE *out = fopen (m_filename, "w");
- if (!out)
- ::selftest::fail_formatted (loc, "unable to open tempfile: %s",
- m_filename);
- fprintf (out, "%s", content);
- fclose (out);
}
/* Destructor. Delete the tempfile. */
-selftest::temp_source_file::~temp_source_file ()
+selftest::named_temp_file::~named_temp_file ()
{
unlink (m_filename);
diagnostics_file_cache_forcibly_evict_file (m_filename);
free (m_filename);
}
+/* Constructor. Create a tempfile using SUFFIX, and write CONTENT to
+ it. Abort if anything goes wrong, using LOC as the effective
+ location in the problem report. */
+
+selftest::temp_source_file::temp_source_file (const location &loc,
+ const char *suffix,
+ const char *content)
+: named_temp_file (suffix)
+{
+ FILE *out = fopen (get_filename (), "w");
+ if (!out)
+ ::selftest::fail_formatted (loc, "unable to open tempfile: %s",
+ get_filename ());
+ fprintf (out, "%s", content);
+ fclose (out);
+}
+
/* Selftests for the selftest system itself. */
namespace selftest {
@@ -167,12 +173,27 @@ test_assertions ()
ASSERT_STR_CONTAINS ("foo bar baz", "bar");
}
+/* Verify named_temp_file. */
+
+static void
+test_named_temp_file ()
+{
+ named_temp_file t (".txt");
+ FILE *f = fopen (t.get_filename (), "w");
+ if (!f)
+ selftest::fail_formatted (SELFTEST_LOCATION,
+ "unable to open %s for writing",
+ t.get_filename ());
+ fclose (f);
+}
+
/* Run all of the selftests within this file. */
void
selftest_c_tests ()
{
test_assertions ();
+ test_named_temp_file ();
}
} // namespace selftest
diff --git a/gcc/selftest.h b/gcc/selftest.h
index e2d7356..47d7350 100644
--- a/gcc/selftest.h
+++ b/gcc/selftest.h
@@ -78,22 +78,32 @@ extern void assert_str_contains (const location &loc,
const char *val_haystack,
const char *val_needle);
-/* A class for writing out a temporary sourcefile for use in selftests
- of input handling. */
+/* A named temporary file for use in selftests.
+ Usable for writing out files, and as the base class for
+ temp_source_file.
+ The file is unlinked in the destructor. */
-class temp_source_file
+class named_temp_file
{
public:
- temp_source_file (const location &loc, const char *suffix,
- const char *content);
- ~temp_source_file ();
-
+ named_temp_file (const char *suffix);
+ ~named_temp_file ();
const char *get_filename () const { return m_filename; }
private:
char *m_filename;
};
+/* A class for writing out a temporary sourcefile for use in selftests
+ of input handling. */
+
+class temp_source_file : public named_temp_file
+{
+ public:
+ temp_source_file (const location &loc, const char *suffix,
+ const char *content);
+};
+
/* Various selftests involving location-handling require constructing a
line table and one or more line maps within it.