Add tint::TmpFile

TmpFile constructs a temporary file that can be written to, and is automatically deleted on destruction.

Will be used to create a temporary source file for verifying generated HLSL and MSL against their shader compilers.

Change-Id: Ieaa6f257b93f4f2193dafe6297603816f6964928
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/41941
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2021-02-18 15:39:00 +00:00 committed by Commit Bot service account
parent 7206e25798
commit a87fda9225
7 changed files with 304 additions and 0 deletions

View File

@ -872,6 +872,8 @@ source_set("tint_unittests_core_src") {
"src/type/u32_type_test.cc",
"src/type/vector_type_test.cc",
"src/type_determiner_test.cc",
"src/utils/tmpfile_test.cc",
"src/utils/tmpfile.h",
"src/utils/unique_vector_test.cc",
"src/validator/validator_builtins_test.cc",
"src/validator/validator_control_block_test.cc",
@ -882,6 +884,14 @@ source_set("tint_unittests_core_src") {
"src/writer/float_to_string_test.cc",
]
if (is_linux || is_mac) {
sources += [ "src/utils/tmpfile_posix.cc" ]
} else if (is_win) {
sources += [ "src/utils/tmpfile_windows.cc" ]
} else {
sources += [ "src/utils/tmpfile_other.cc" ]
}
configs += [
":tint_common_config",
":tint_unittests_config",

View File

@ -497,6 +497,8 @@ if(${TINT_BUILD_TESTS})
type/type_manager_test.cc
type/u32_type_test.cc
type/vector_type_test.cc
utils/tmpfile_test.cc
utils/tmpfile.h
utils/unique_vector_test.cc
validator/validator_builtins_test.cc
validator/validator_control_block_test.cc
@ -508,6 +510,14 @@ if(${TINT_BUILD_TESTS})
writer/float_to_string_test.cc
)
if(UNIX OR APPLE)
list(APPEND TINT_TEST_SRCS utils/tmpfile_posix.cc)
elseif(WIN32)
list(APPEND TINT_TEST_SRCS utils/tmpfile_windows.cc)
else()
list(APPEND TINT_TEST_SRCS utils/tmpfile_other.cc)
endif()
if(${TINT_BUILD_SPV_READER})
list(APPEND TINT_TEST_SRCS
reader/spirv/enum_converter_test.cc

74
src/utils/tmpfile.h Normal file
View File

@ -0,0 +1,74 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_UTILS_TMPFILE_H_
#define SRC_UTILS_TMPFILE_H_
#include <sstream>
#include <string>
namespace tint {
namespace utils {
/// TmpFile constructs a temporary file that can be written to, and is
/// automatically deleted on destruction.
class TmpFile {
public:
/// Constructor.
/// Creates a new temporary file which can be written to.
/// The temporary file will be automatically deleted on destruction.
TmpFile();
/// Destructor.
/// Deletes the temporary file.
~TmpFile();
/// @return true if the temporary file was successfully created.
operator bool() { return !path_.empty(); }
/// @return the path to the temporary file
std::string Path() const { return path_; }
/// Opens the temporary file and appends |size| bytes from |data| to the end
/// of the temporary file. The temporary file is closed again before
/// returning, allowing other processes to open the file on operating systems
/// that require exclusive ownership of opened files.
/// @param data the data to write to the end of the file
/// @param size the number of bytes to write from data
/// @returns true on success, otherwise false
bool Append(const void* data, size_t size) const;
/// Appends the argument to the end of the file.
/// @param data the data to write to the end of the file
/// @return a reference to this TmpFile
template <typename T>
inline TmpFile& operator<<(T&& data) {
std::stringstream ss;
ss << data;
std::string str = ss.str();
Append(str.data(), str.size());
return *this;
}
private:
TmpFile(const TmpFile&) = delete;
TmpFile& operator=(const TmpFile&) = delete;
std::string path_;
};
} // namespace utils
} // namespace tint
#endif // SRC_UTILS_TMPFILE_H_

View File

@ -0,0 +1,29 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/utils/tmpfile.h"
namespace tint {
namespace utils {
TmpFile::TmpFile() = default;
TmpFile::~TmpFile() = default;
bool TmpFile::Append(const void* data, size_t size) const {
return false;
}
} // namespace utils
} // namespace tint

View File

@ -0,0 +1,56 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/utils/tmpfile.h"
#include <stdio.h>
#include <unistd.h>
#include <cstdio>
namespace tint {
namespace utils {
namespace {
std::string TmpFilePath() {
char name[] = "tint_XXXXXX";
int file = mkstemp(name);
if (file != -1) {
close(file);
return name;
}
return "";
}
} // namespace
TmpFile::TmpFile() : path_(TmpFilePath()) {}
TmpFile::~TmpFile() {
if (!path_.empty()) {
remove(path_.c_str());
}
}
bool TmpFile::Append(const void* data, size_t size) const {
if (auto* file = fopen(path_.c_str(), "ab")) {
fwrite(data, size, 1, file);
fclose(file);
return true;
}
return false;
}
} // namespace utils
} // namespace tint

71
src/utils/tmpfile_test.cc Normal file
View File

@ -0,0 +1,71 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/utils/tmpfile.h"
#include <fstream>
#include "gtest/gtest.h"
namespace tint {
namespace utils {
namespace {
TEST(TmpFileTest, WriteReadAppendDelete) {
std::string path;
{
TmpFile tmp;
if (!tmp) {
GTEST_SKIP() << "Unable to create a temporary file";
}
path = tmp.Path();
// Write a string to the temporary file
tmp << "hello world\n";
// Check the content of the file
{
std::ifstream file(path);
ASSERT_TRUE(file);
std::string line;
EXPECT_TRUE(std::getline(file, line));
EXPECT_EQ(line, "hello world");
EXPECT_FALSE(std::getline(file, line));
}
// Write some more content to the file
tmp << 42;
// Check the content of the file again
{
std::ifstream file(path);
ASSERT_TRUE(file);
std::string line;
EXPECT_TRUE(std::getline(file, line));
EXPECT_EQ(line, "hello world");
EXPECT_TRUE(std::getline(file, line));
EXPECT_EQ(line, "42");
EXPECT_FALSE(std::getline(file, line));
}
}
// Check the file has been deleted when it fell out of scope
std::ifstream file(path);
ASSERT_FALSE(file);
}
} // namespace
} // namespace utils
} // namespace tint

View File

@ -0,0 +1,54 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/utils/tmpfile.h"
#include <stdio.h>
#include <cstdio>
namespace tint {
namespace utils {
namespace {
std::string TmpFilePath() {
char name[L_tmpnam];
if (tmpnam_s(name, L_tmpnam - 1) == 0) {
return name;
}
return "";
}
} // namespace
TmpFile::TmpFile() : path_(TmpFilePath()) {}
TmpFile::~TmpFile() {
if (!path_.empty()) {
remove(path_.c_str());
}
}
bool TmpFile::Append(const void* data, size_t size) const {
FILE* file = nullptr;
if (fopen_s(&file, path_.c_str(), "ab") != 0) {
return false;
}
fwrite(data, size, 1, file);
fclose(file);
return true;
}
} // namespace utils
} // namespace tint