2020-06-09 18:24:30 +08:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2020-06-09 18:24:30 +08:00
|
|
|
//
|
|
|
|
// 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 "sandboxed_api/tools/clang_generator/emitter.h"
|
|
|
|
|
2020-09-25 16:13:50 +08:00
|
|
|
#include <initializer_list>
|
2022-10-12 20:22:51 +08:00
|
|
|
#include <memory>
|
2020-09-25 16:13:50 +08:00
|
|
|
|
2020-06-09 18:24:30 +08:00
|
|
|
#include "gmock/gmock.h"
|
2020-06-10 20:34:35 +08:00
|
|
|
#include "gtest/gtest.h"
|
2020-10-27 00:08:06 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2020-06-10 20:34:35 +08:00
|
|
|
#include "absl/strings/string_view.h"
|
2020-09-25 16:13:50 +08:00
|
|
|
#include "sandboxed_api/tools/clang_generator/frontend_action_test_util.h"
|
|
|
|
#include "sandboxed_api/tools/clang_generator/generator.h"
|
2020-06-09 18:24:30 +08:00
|
|
|
#include "sandboxed_api/util/status_matchers.h"
|
|
|
|
|
|
|
|
namespace sapi {
|
|
|
|
namespace {
|
|
|
|
|
2022-03-08 23:16:17 +08:00
|
|
|
using ::testing::ElementsAre;
|
|
|
|
using ::testing::IsEmpty;
|
2020-06-09 18:24:30 +08:00
|
|
|
using ::testing::MatchesRegex;
|
2020-09-25 16:13:50 +08:00
|
|
|
using ::testing::SizeIs;
|
2020-06-09 18:24:30 +08:00
|
|
|
using ::testing::StrEq;
|
2020-06-10 20:34:35 +08:00
|
|
|
using ::testing::StrNe;
|
2020-06-09 18:24:30 +08:00
|
|
|
|
2020-09-25 16:13:50 +08:00
|
|
|
class EmitterForTesting : public Emitter {
|
|
|
|
public:
|
2022-11-07 16:37:13 +08:00
|
|
|
std::vector<std::string> SpellingsForNS(const std::string& ns_name) {
|
|
|
|
std::vector<std::string> result;
|
|
|
|
for (const RenderedType* rt : rendered_types_ordered_) {
|
|
|
|
if (rt->ns_name == ns_name) {
|
|
|
|
result.push_back(rt->spelling);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<std::string>& GetRenderedFunctions() {
|
|
|
|
return rendered_functions_ordered_;
|
|
|
|
}
|
2020-09-25 16:13:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class EmitterTest : public FrontendActionTest {};
|
|
|
|
|
|
|
|
TEST_F(EmitterTest, BasicFunctionality) {
|
|
|
|
GeneratorOptions options;
|
|
|
|
options.set_function_names<std::initializer_list<std::string>>(
|
|
|
|
{"ExposedFunction"});
|
|
|
|
|
|
|
|
EmitterForTesting emitter;
|
2022-11-01 23:59:48 +08:00
|
|
|
ASSERT_THAT(
|
|
|
|
RunFrontendAction(R"(extern "C" void ExposedFunction() {})",
|
|
|
|
std::make_unique<GeneratorAction>(emitter, options)),
|
|
|
|
IsOk());
|
2022-11-07 16:37:13 +08:00
|
|
|
EXPECT_THAT(emitter.GetRenderedFunctions(), SizeIs(1));
|
2020-09-25 16:13:50 +08:00
|
|
|
|
|
|
|
absl::StatusOr<std::string> header = emitter.EmitHeader(options);
|
|
|
|
EXPECT_THAT(header, IsOk());
|
|
|
|
}
|
|
|
|
|
2022-03-08 23:16:17 +08:00
|
|
|
TEST_F(EmitterTest, RelatedTypes) {
|
|
|
|
EmitterForTesting emitter;
|
2022-11-01 23:59:48 +08:00
|
|
|
ASSERT_THAT(
|
|
|
|
RunFrontendAction(
|
|
|
|
R"(namespace std {
|
|
|
|
using size_t = unsigned long;
|
|
|
|
} // namespace std
|
|
|
|
using std::size_t;
|
|
|
|
typedef enum { kRed, kGreen, kBlue } Color;
|
|
|
|
struct Channel {
|
|
|
|
Color color;
|
|
|
|
size_t width;
|
|
|
|
size_t height;
|
|
|
|
};
|
2022-11-02 15:42:51 +08:00
|
|
|
extern "C" void Colorize(Channel* chan);
|
2022-11-01 23:59:48 +08:00
|
|
|
|
|
|
|
typedef struct { int member; } MyStruct;
|
|
|
|
extern "C" void Structize(MyStruct* s);)",
|
|
|
|
std::make_unique<GeneratorAction>(emitter, GeneratorOptions())),
|
|
|
|
IsOk());
|
2022-11-15 00:18:42 +08:00
|
|
|
EXPECT_THAT(emitter.GetRenderedFunctions(), SizeIs(2));
|
2022-03-08 23:16:17 +08:00
|
|
|
|
|
|
|
// Types from "std" should be skipped
|
2022-11-07 16:37:13 +08:00
|
|
|
EXPECT_THAT(emitter.SpellingsForNS("std"), IsEmpty());
|
2022-03-08 23:16:17 +08:00
|
|
|
|
2022-11-07 16:37:13 +08:00
|
|
|
EXPECT_THAT(UglifyAll(emitter.SpellingsForNS("")),
|
2022-03-08 23:16:17 +08:00
|
|
|
ElementsAre("typedef enum { kRed, kGreen, kBlue } Color",
|
2022-03-17 18:12:46 +08:00
|
|
|
"struct Channel {"
|
|
|
|
" Color color;"
|
2022-08-03 19:55:15 +08:00
|
|
|
" size_t width;"
|
|
|
|
" size_t height; }",
|
2022-03-08 23:16:17 +08:00
|
|
|
"typedef struct { int member; } MyStruct"));
|
|
|
|
}
|
|
|
|
|
2022-11-23 21:18:30 +08:00
|
|
|
TEST_F(EmitterTest, CollectFunctionPointer) {
|
|
|
|
EmitterForTesting emitter;
|
|
|
|
EXPECT_THAT(
|
|
|
|
RunFrontendAction(
|
|
|
|
R"(typedef void (callback_t)(void*);
|
|
|
|
struct HandlerData {
|
|
|
|
int member;
|
|
|
|
callback_t* cb;
|
|
|
|
};
|
|
|
|
extern "C" int Structize(HandlerData*);)",
|
|
|
|
std::make_unique<GeneratorAction>(emitter, GeneratorOptions())),
|
|
|
|
IsOk());
|
|
|
|
EXPECT_THAT(emitter.GetRenderedFunctions(), SizeIs(1));
|
|
|
|
|
|
|
|
EXPECT_THAT(
|
|
|
|
UglifyAll(emitter.SpellingsForNS("")),
|
2022-11-23 23:54:13 +08:00
|
|
|
ElementsAre("typedef void (callback_t)(void *)",
|
|
|
|
"struct HandlerData { int member; callback_t *cb; }"));
|
2022-11-23 21:18:30 +08:00
|
|
|
}
|
|
|
|
|
2022-11-15 00:18:42 +08:00
|
|
|
TEST_F(EmitterTest, TypedefNames) {
|
|
|
|
EmitterForTesting emitter;
|
|
|
|
ASSERT_THAT(
|
|
|
|
RunFrontendAction(
|
|
|
|
R"(typedef enum { kNone, kSome } E;
|
|
|
|
struct A { E member; };
|
|
|
|
typedef struct { int member; } B;
|
|
|
|
typedef struct tagC { int member; } C;
|
|
|
|
extern "C" void Colorize(A*, B*, C*);)",
|
|
|
|
std::make_unique<GeneratorAction>(emitter, GeneratorOptions())),
|
|
|
|
IsOk());
|
|
|
|
|
|
|
|
EXPECT_THAT(
|
|
|
|
UglifyAll(emitter.SpellingsForNS("")),
|
|
|
|
ElementsAre("typedef enum { kNone, kSome } E", "struct A { E member; }",
|
|
|
|
"typedef struct { int member; } B",
|
|
|
|
"struct tagC { int member; }", "typedef struct tagC C"));
|
|
|
|
}
|
|
|
|
|
2022-03-17 18:12:46 +08:00
|
|
|
TEST_F(EmitterTest, NestedStruct) {
|
|
|
|
EmitterForTesting emitter;
|
2022-11-01 23:59:48 +08:00
|
|
|
ASSERT_THAT(
|
|
|
|
RunFrontendAction(
|
|
|
|
R"(struct A {
|
|
|
|
struct B { int number; };
|
|
|
|
B b;
|
|
|
|
int data;
|
|
|
|
};
|
|
|
|
extern "C" void Structize(A* s);)",
|
|
|
|
std::make_unique<GeneratorAction>(emitter, GeneratorOptions())),
|
|
|
|
IsOk());
|
2022-03-17 18:12:46 +08:00
|
|
|
|
2022-11-07 16:37:13 +08:00
|
|
|
EXPECT_THAT(UglifyAll(emitter.SpellingsForNS("")),
|
2022-03-17 18:29:36 +08:00
|
|
|
ElementsAre("struct A {"
|
|
|
|
" struct B { int number; };"
|
2022-08-03 19:55:15 +08:00
|
|
|
" B b;"
|
2022-03-17 18:29:36 +08:00
|
|
|
" int data; }"));
|
2022-03-17 18:12:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EmitterTest, NestedAnonymousStruct) {
|
|
|
|
EmitterForTesting emitter;
|
2022-11-01 23:59:48 +08:00
|
|
|
ASSERT_THAT(
|
|
|
|
RunFrontendAction(
|
|
|
|
R"(struct A {
|
|
|
|
struct { int number; } b;
|
|
|
|
int data;
|
|
|
|
};
|
|
|
|
extern "C" void Structize(A* s);)",
|
|
|
|
std::make_unique<GeneratorAction>(emitter, GeneratorOptions())),
|
|
|
|
IsOk());
|
2022-03-17 18:29:36 +08:00
|
|
|
|
2022-11-07 16:37:13 +08:00
|
|
|
EXPECT_THAT(UglifyAll(emitter.SpellingsForNS("")),
|
2022-03-17 18:29:36 +08:00
|
|
|
ElementsAre("struct A {"
|
|
|
|
" struct { int number; } b;"
|
|
|
|
" int data; }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EmitterTest, ParentNotCollected) {
|
|
|
|
EmitterForTesting emitter;
|
2022-11-01 23:59:48 +08:00
|
|
|
ASSERT_THAT(
|
|
|
|
RunFrontendAction(
|
|
|
|
R"(struct A {
|
|
|
|
struct B { int number; };
|
|
|
|
B b;
|
|
|
|
int data;
|
|
|
|
};
|
|
|
|
extern "C" void Structize(A::B* s);)",
|
|
|
|
std::make_unique<GeneratorAction>(emitter, GeneratorOptions())),
|
|
|
|
IsOk());
|
2022-03-17 18:29:36 +08:00
|
|
|
|
2022-11-07 16:37:13 +08:00
|
|
|
EXPECT_THAT(UglifyAll(emitter.SpellingsForNS("")),
|
2022-03-17 18:29:36 +08:00
|
|
|
ElementsAre("struct A {"
|
|
|
|
" struct B { int number; };"
|
2022-08-03 19:55:15 +08:00
|
|
|
" B b;"
|
2022-03-17 18:29:36 +08:00
|
|
|
" int data; }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EmitterTest, RemoveQualifiers) {
|
|
|
|
EmitterForTesting emitter;
|
2022-11-01 23:59:48 +08:00
|
|
|
ASSERT_THAT(
|
|
|
|
RunFrontendAction(
|
|
|
|
R"(struct A { int data; };
|
|
|
|
extern "C" void Structize(const A* in, A* out);)",
|
|
|
|
std::make_unique<GeneratorAction>(emitter, GeneratorOptions())),
|
|
|
|
IsOk());
|
2022-03-17 18:12:46 +08:00
|
|
|
|
2022-11-07 16:37:13 +08:00
|
|
|
EXPECT_THAT(UglifyAll(emitter.SpellingsForNS("")),
|
2022-03-17 18:29:36 +08:00
|
|
|
ElementsAre("struct A { int data; }"));
|
2022-03-17 18:12:46 +08:00
|
|
|
}
|
|
|
|
|
2022-11-03 21:01:50 +08:00
|
|
|
TEST_F(EmitterTest, StructByValueSkipsFunction) {
|
2022-11-02 15:42:51 +08:00
|
|
|
EmitterForTesting emitter;
|
|
|
|
EXPECT_THAT(
|
|
|
|
RunFrontendAction(
|
|
|
|
R"(struct A { int data; };
|
|
|
|
extern "C" int Structize(A a);)",
|
|
|
|
std::make_unique<GeneratorAction>(emitter, GeneratorOptions())),
|
2022-11-03 21:01:50 +08:00
|
|
|
IsOk());
|
2022-11-07 16:37:13 +08:00
|
|
|
EXPECT_THAT(emitter.GetRenderedFunctions(), IsEmpty());
|
2022-11-02 15:42:51 +08:00
|
|
|
}
|
|
|
|
|
2022-11-03 21:01:50 +08:00
|
|
|
TEST_F(EmitterTest, ReturnStructByValueSkipsFunction) {
|
2022-11-02 15:42:51 +08:00
|
|
|
EmitterForTesting emitter;
|
|
|
|
EXPECT_THAT(
|
|
|
|
RunFrontendAction(
|
|
|
|
R"(struct A { int data; };
|
|
|
|
extern "C" A Structize();)",
|
|
|
|
std::make_unique<GeneratorAction>(emitter, GeneratorOptions())),
|
2022-11-03 21:01:50 +08:00
|
|
|
IsOk());
|
2022-11-07 16:37:13 +08:00
|
|
|
EXPECT_THAT(emitter.GetRenderedFunctions(), IsEmpty());
|
2022-11-02 15:42:51 +08:00
|
|
|
}
|
|
|
|
|
2022-11-03 21:01:50 +08:00
|
|
|
TEST_F(EmitterTest, TypedefStructByValueSkipsFunction) {
|
2022-11-02 15:42:51 +08:00
|
|
|
EmitterForTesting emitter;
|
|
|
|
EXPECT_THAT(
|
|
|
|
RunFrontendAction(
|
|
|
|
R"(typedef struct { int data; } A;
|
|
|
|
extern "C" int Structize(A a);)",
|
|
|
|
std::make_unique<GeneratorAction>(emitter, GeneratorOptions())),
|
2022-11-03 21:01:50 +08:00
|
|
|
IsOk());
|
2022-11-07 16:37:13 +08:00
|
|
|
EXPECT_THAT(emitter.GetRenderedFunctions(), IsEmpty());
|
2022-11-02 15:42:51 +08:00
|
|
|
}
|
|
|
|
|
2022-11-23 23:54:13 +08:00
|
|
|
TEST_F(EmitterTest, CollectTypedefPointerType) {
|
|
|
|
EmitterForTesting emitter;
|
|
|
|
EXPECT_THAT(
|
|
|
|
RunFrontendAction(
|
|
|
|
R"(typedef struct _KernelProfileRecord {
|
|
|
|
int member;
|
|
|
|
}* KernelProfileRecord;
|
|
|
|
extern "C" const KernelProfileRecord*
|
|
|
|
GetOpenCLKernelProfileRecords(const int, long long int*);)",
|
|
|
|
std::make_unique<GeneratorAction>(emitter, GeneratorOptions())),
|
|
|
|
IsOk());
|
|
|
|
EXPECT_THAT(emitter.GetRenderedFunctions(), SizeIs(1));
|
|
|
|
|
|
|
|
EXPECT_THAT(
|
|
|
|
UglifyAll(emitter.SpellingsForNS("")),
|
|
|
|
ElementsAre("struct _KernelProfileRecord { int member; }",
|
|
|
|
"typedef struct _KernelProfileRecord *KernelProfileRecord"));
|
|
|
|
}
|
|
|
|
|
2022-11-15 00:18:42 +08:00
|
|
|
TEST_F(EmitterTest, TypedefTypeDependencies) {
|
|
|
|
EmitterForTesting emitter;
|
|
|
|
EXPECT_THAT(
|
|
|
|
RunFrontendAction(
|
|
|
|
R"(typedef bool some_other_unused;
|
|
|
|
using size_t = long long int;
|
|
|
|
typedef struct _Image Image;
|
|
|
|
typedef size_t (*StreamHandler)(const Image*, const void*,
|
|
|
|
const size_t);
|
|
|
|
enum unrelated_unused { NONE, SOME };
|
|
|
|
struct _Image {
|
|
|
|
StreamHandler stream;
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
extern "C" void Process(StreamHandler handler);)",
|
|
|
|
std::make_unique<GeneratorAction>(emitter, GeneratorOptions())),
|
|
|
|
IsOk());
|
|
|
|
EXPECT_THAT(emitter.GetRenderedFunctions(), SizeIs(1));
|
|
|
|
|
|
|
|
EXPECT_THAT(UglifyAll(emitter.SpellingsForNS("")),
|
|
|
|
ElementsAre("using size_t = long long", "struct _Image",
|
2022-11-23 23:54:13 +08:00
|
|
|
"typedef struct _Image Image",
|
2022-11-15 00:18:42 +08:00
|
|
|
"typedef size_t (*StreamHandler)(const Image *, "
|
|
|
|
"const void *, const size_t)",
|
|
|
|
"struct _Image {"
|
|
|
|
" StreamHandler stream;"
|
|
|
|
" int size; }"));
|
|
|
|
}
|
|
|
|
|
2022-11-24 00:31:27 +08:00
|
|
|
TEST_F(EmitterTest, OmitDependentTypes) {
|
|
|
|
EmitterForTesting emitter;
|
|
|
|
EXPECT_THAT(
|
|
|
|
RunFrontendAction(
|
|
|
|
R"(template <typename T>
|
|
|
|
struct Callback {
|
|
|
|
typedef void (T::*MemberSignature)();
|
|
|
|
MemberSignature pointer;
|
|
|
|
};
|
|
|
|
struct S : public Callback<S> {
|
|
|
|
void Callable() {}
|
|
|
|
};
|
|
|
|
extern "C" void Invoke(S::MemberSignature* cb);)",
|
|
|
|
std::make_unique<GeneratorAction>(emitter, GeneratorOptions())),
|
|
|
|
IsOk());
|
|
|
|
EXPECT_THAT(emitter.GetRenderedFunctions(), SizeIs(1));
|
|
|
|
|
|
|
|
EXPECT_THAT(UglifyAll(emitter.SpellingsForNS("")), IsEmpty());
|
|
|
|
}
|
|
|
|
|
2022-11-24 00:46:59 +08:00
|
|
|
TEST_F(EmitterTest, SkipAbseilInternals) {
|
|
|
|
EmitterForTesting emitter;
|
|
|
|
EXPECT_THAT(
|
|
|
|
RunFrontendAction(
|
|
|
|
R"(namespace absl::internal {
|
|
|
|
typedef int Int;
|
|
|
|
}
|
|
|
|
extern "C" void TakesAnInternalInt(absl::internal::Int);
|
|
|
|
extern "C" void AbslInternalTakingAnInt(int);)",
|
|
|
|
std::make_unique<GeneratorAction>(emitter, GeneratorOptions())),
|
|
|
|
IsOk());
|
|
|
|
EXPECT_THAT(emitter.GetRenderedFunctions(), SizeIs(1));
|
|
|
|
|
|
|
|
EXPECT_THAT(UglifyAll(emitter.SpellingsForNS("")), IsEmpty());
|
|
|
|
}
|
|
|
|
|
2020-06-09 18:24:30 +08:00
|
|
|
TEST(IncludeGuard, CreatesRandomizedGuardForEmptyFilename) {
|
|
|
|
// Copybara will transform the string. This is intentional.
|
|
|
|
constexpr absl::string_view kGeneratedHeaderPrefix =
|
|
|
|
"SANDBOXED_API_GENERATED_HEADER_";
|
|
|
|
|
|
|
|
const std::string include_guard = GetIncludeGuard("");
|
|
|
|
EXPECT_THAT(include_guard, MatchesRegex(absl::StrCat(kGeneratedHeaderPrefix,
|
|
|
|
R"([0-9A-F]+_)")));
|
2020-06-10 20:34:35 +08:00
|
|
|
|
|
|
|
EXPECT_THAT(GetIncludeGuard(""), StrNe(include_guard));
|
2020-06-09 18:24:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IncludeGuard, BasicFunctionality) {
|
|
|
|
EXPECT_THAT(GetIncludeGuard("boost/graph/compressed_sparse_row_graph.hpp"),
|
|
|
|
StrEq("BOOST_GRAPH_COMPRESSED_SPARSE_ROW_GRAPH_HPP_"));
|
|
|
|
|
|
|
|
// "SAPI_" prefix is there to avoid generating guards starting with "_"
|
|
|
|
EXPECT_THAT(GetIncludeGuard("/usr/include/unistd.h"),
|
|
|
|
StrEq("SAPI_USR_INCLUDE_UNISTD_H_"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(IncludeGuard, AvoidReservedIdentifiers) {
|
|
|
|
EXPECT_THAT(GetIncludeGuard("9p.h"), StrEq("SAPI_9P_H_"));
|
|
|
|
EXPECT_THAT(GetIncludeGuard("double__under.h"), StrEq("DOUBLE_UNDER_H_"));
|
|
|
|
EXPECT_THAT(GetIncludeGuard("_single.h"), StrEq("SAPI_SINGLE_H_"));
|
|
|
|
EXPECT_THAT(GetIncludeGuard("__double.h"), StrEq("SAPI_DOUBLE_H_"));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace sapi
|