Replace SkSL string macros with inline constexpr char arrays.

C++17 introduced `inline constexpr` as a mechanism for declaring strings
in headers safely without macros. See https://abseil.io/tips/168

Change-Id: I68071fbadebf32753ca4a0586b96e32634083f6a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/309047
Commit-Queue: John Stiles <johnstiles@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
This commit is contained in:
John Stiles 2020-08-10 15:25:24 -04:00 committed by Skia Commit-Bot
parent 5b68ed4c51
commit 02b1128b03
3 changed files with 67 additions and 67 deletions

View File

@ -901,7 +901,7 @@ bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) {
for (const auto u : uniforms) {
this->addUniform(*u);
}
this->writeSection(EMIT_CODE_SECTION);
this->writeSection(kEmitCodeSection);
// Save original buffer as the CPP buffer for flushEmittedCode()
fCPPBuffer = fOut;
@ -921,7 +921,7 @@ bool CPPCodeGenerator::writeEmitCode(std::vector<const Variable*>& uniforms) {
void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
const char* fullName = fFullName.c_str();
const Section* section = fSectionAndParameterHelper.getSection(SET_DATA_SECTION);
const Section* section = fSectionAndParameterHelper.getSection(kSetDataSection);
const char* pdman = section ? section->fArgument.c_str() : "pdman";
this->writef(" void onSetData(const GrGLSLProgramDataManager& %s, "
"const GrFragmentProcessor& _proc) override {\n",
@ -1034,7 +1034,7 @@ void CPPCodeGenerator::writeSetData(std::vector<const Variable*>& uniforms) {
}
}
}
this->writeSection(SET_DATA_SECTION);
this->writeSection(kSetDataSection);
}
this->write(" }\n");
}
@ -1063,8 +1063,8 @@ void CPPCodeGenerator::writeOnTextureSampler() {
}
void CPPCodeGenerator::writeClone() {
if (!this->writeSection(CLONE_SECTION)) {
if (fSectionAndParameterHelper.getSection(FIELDS_SECTION)) {
if (!this->writeSection(kCloneSection)) {
if (fSectionAndParameterHelper.getSection(kFieldsSection)) {
fErrors.error(0, "fragment processors with custom @fields must also have a custom"
"@clone");
}
@ -1103,7 +1103,7 @@ void CPPCodeGenerator::writeClone() {
}
void CPPCodeGenerator::writeTest() {
const Section* test = fSectionAndParameterHelper.getSection(TEST_CODE_SECTION);
const Section* test = fSectionAndParameterHelper.getSection(kTestCodeSection);
if (test) {
this->writef(
"GR_DEFINE_FRAGMENT_PROCESSOR_TEST(%s);\n"
@ -1112,7 +1112,7 @@ void CPPCodeGenerator::writeTest() {
fFullName.c_str(),
fFullName.c_str(),
test->fArgument.c_str());
this->writeSection(TEST_CODE_SECTION);
this->writeSection(kTestCodeSection);
this->write("}\n"
"#endif\n");
}
@ -1243,7 +1243,7 @@ bool CPPCodeGenerator::generateCode() {
this->writef("%s\n", HCodeGenerator::GetHeader(fProgram, fErrors).c_str());
this->writef(kFragmentProcessorHeader, fullName);
this->writef("#include \"%s.h\"\n\n", fullName);
this->writeSection(CPP_SECTION);
this->writeSection(kCppSection);
this->writef("#include \"src/core/SkUtils.h\"\n"
"#include \"src/gpu/GrTexture.h\"\n"
"#include \"src/gpu/glsl/GrGLSLFragmentProcessor.h\"\n"
@ -1296,7 +1296,7 @@ bool CPPCodeGenerator::generateCode() {
this->writeClone();
this->writeOnTextureSampler();
this->writeTest();
this->writeSection(CPP_END_SECTION);
this->writeSection(kCppEndSection);
result &= 0 == fErrors.errorCount();
return result;

View File

@ -137,7 +137,7 @@ void HCodeGenerator::writeExtraConstructorParams(const char* separator) {
// super-simple parse, just assume the last token before a comma is the name of a parameter
// (which is true as long as there are no multi-parameter template types involved). Will replace
// this with something more robust if the need arises.
const Section* section = fSectionAndParameterHelper.getSection(CONSTRUCTOR_PARAMS_SECTION);
const Section* section = fSectionAndParameterHelper.getSection(kConstructorParamsSection);
if (section) {
const char* s = section->fText.c_str();
#define BUFFER_SIZE 64
@ -178,7 +178,7 @@ void HCodeGenerator::writeExtraConstructorParams(const char* separator) {
void HCodeGenerator::writeMake() {
const char* separator;
if (!this->writeSection(MAKE_SECTION)) {
if (!this->writeSection(kMakeSection)) {
this->writef(" static std::unique_ptr<GrFragmentProcessor> Make(");
separator = "";
for (const auto& param : fSectionAndParameterHelper.getParameters()) {
@ -187,7 +187,7 @@ void HCodeGenerator::writeMake() {
String(param->fName).c_str());
separator = ", ";
}
this->writeSection(CONSTRUCTOR_PARAMS_SECTION, separator);
this->writeSection(kConstructorParamsSection, separator);
this->writef(") {\n"
" return std::unique_ptr<GrFragmentProcessor>(new %s(",
fFullName.c_str());
@ -215,12 +215,12 @@ void HCodeGenerator::failOnSection(const char* section, const char* msg) {
}
void HCodeGenerator::writeConstructor() {
if (this->writeSection(CONSTRUCTOR_SECTION)) {
if (this->writeSection(kConstructorSection)) {
const char* msg = "may not be present when constructor is overridden";
this->failOnSection(CONSTRUCTOR_CODE_SECTION, msg);
this->failOnSection(CONSTRUCTOR_PARAMS_SECTION, msg);
this->failOnSection(INITIALIZERS_SECTION, msg);
this->failOnSection(OPTIMIZATION_FLAGS_SECTION, msg);
this->failOnSection(kConstructorCodeSection, msg);
this->failOnSection(kConstructorParamsSection, msg);
this->failOnSection(kInitializersSection, msg);
this->failOnSection(kOptimizationFlagsSection, msg);
return;
}
this->writef(" %s(", fFullName.c_str());
@ -231,14 +231,14 @@ void HCodeGenerator::writeConstructor() {
String(param->fName).c_str());
separator = ", ";
}
this->writeSection(CONSTRUCTOR_PARAMS_SECTION, separator);
this->writeSection(kConstructorParamsSection, separator);
this->writef(")\n"
" : INHERITED(k%s_ClassID", fFullName.c_str());
if (!this->writeSection(OPTIMIZATION_FLAGS_SECTION, ", (OptimizationFlags) ")) {
if (!this->writeSection(kOptimizationFlagsSection, ", (OptimizationFlags) ")) {
this->writef(", kNone_OptimizationFlags");
}
this->writef(")");
this->writeSection(INITIALIZERS_SECTION, "\n , ");
this->writeSection(kInitializersSection, "\n , ");
for (const auto& param : fSectionAndParameterHelper.getParameters()) {
String nameString(param->fName);
const char* name = nameString.c_str();
@ -246,7 +246,7 @@ void HCodeGenerator::writeConstructor() {
if (type.kind() == Type::kSampler_Kind) {
this->writef("\n , %s(std::move(%s)", FieldName(name).c_str(), name);
for (const Section* s : fSectionAndParameterHelper.getSections(
SAMPLER_PARAMS_SECTION)) {
kSamplerParamsSection)) {
if (s->fArgument == name) {
this->writef(", %s", s->fText.c_str());
}
@ -259,7 +259,7 @@ void HCodeGenerator::writeConstructor() {
}
}
this->writef(" {\n");
this->writeSection(CONSTRUCTOR_CODE_SECTION);
this->writeSection(kConstructorCodeSection);
if (Analysis::ReferencesSampleCoords(fProgram)) {
this->writef(" this->setUsesSampleCoordsDirectly();\n");
@ -300,7 +300,7 @@ void HCodeGenerator::writeConstructor() {
}
void HCodeGenerator::writeFields() {
this->writeSection(FIELDS_SECTION);
this->writeSection(kFieldsSection);
for (const auto& param : fSectionAndParameterHelper.getParameters()) {
String name = FieldName(String(param->fName).c_str());
if (param->fType.nonnullable() == *fContext.fFragmentProcessor_Type) {
@ -340,7 +340,7 @@ bool HCodeGenerator::generateCode() {
this->writef("#include \"include/core/SkM44.h\"\n"
"#include \"include/core/SkTypes.h\"\n"
"\n");
this->writeSection(HEADER_SECTION);
this->writeSection(kHeaderSection);
this->writef("\n"
"#include \"src/gpu/GrFragmentProcessor.h\"\n"
"\n");
@ -352,7 +352,7 @@ bool HCodeGenerator::generateCode() {
this->writef("%s\n", ((Enum&) p).code().c_str());
}
}
this->writeSection(CLASS_SECTION);
this->writeSection(kClassSection);
this->writeMake();
this->writef(" %s(const %s& src);\n"
" std::unique_ptr<GrFragmentProcessor> clone() const override;\n"
@ -374,7 +374,7 @@ bool HCodeGenerator::generateCode() {
this->writef(" GR_DECLARE_FRAGMENT_PROCESSOR_TEST\n");
this->writef(" typedef GrFragmentProcessor INHERITED;\n"
"};\n");
this->writeSection(HEADER_END_SECTION);
this->writeSection(kHeaderEndSection);
this->writef("#endif\n");
return 0 == fErrors.errorCount();
}

View File

@ -17,23 +17,23 @@
namespace SkSL {
#define CLASS_SECTION "class"
#define CLONE_SECTION "clone"
#define CONSTRUCTOR_SECTION "constructor"
#define CONSTRUCTOR_CODE_SECTION "constructorCode"
#define CONSTRUCTOR_PARAMS_SECTION "constructorParams"
#define CPP_SECTION "cpp"
#define CPP_END_SECTION "cppEnd"
#define HEADER_SECTION "header"
#define HEADER_END_SECTION "headerEnd"
#define EMIT_CODE_SECTION "emitCode"
#define FIELDS_SECTION "fields"
#define INITIALIZERS_SECTION "initializers"
#define MAKE_SECTION "make"
#define OPTIMIZATION_FLAGS_SECTION "optimizationFlags"
#define SAMPLER_PARAMS_SECTION "samplerParams"
#define SET_DATA_SECTION "setData"
#define TEST_CODE_SECTION "test"
inline constexpr char kClassSection[] = "class";
inline constexpr char kCloneSection[] = "clone";
inline constexpr char kConstructorSection[] = "constructor";
inline constexpr char kConstructorCodeSection[] = "constructorCode";
inline constexpr char kConstructorParamsSection[] = "constructorParams";
inline constexpr char kCppSection[] = "cpp";
inline constexpr char kCppEndSection[] = "cppEnd";
inline constexpr char kEmitCodeSection[] = "emitCode";
inline constexpr char kFieldsSection[] = "fields";
inline constexpr char kHeaderSection[] = "header";
inline constexpr char kHeaderEndSection[] = "headerEnd";
inline constexpr char kInitializersSection[] = "initializers";
inline constexpr char kMakeSection[] = "make";
inline constexpr char kOptimizationFlagsSection[] = "optimizationFlags";
inline constexpr char kSamplerParamsSection[] = "samplerParams";
inline constexpr char kSetDataSection[] = "setData";
inline constexpr char kTestCodeSection[] = "test";
class SectionAndParameterHelper {
public:
@ -67,39 +67,39 @@ public:
}
static bool IsSupportedSection(const char* name) {
return !strcmp(name, CLASS_SECTION) ||
!strcmp(name, CLONE_SECTION) ||
!strcmp(name, CONSTRUCTOR_SECTION) ||
!strcmp(name, CONSTRUCTOR_CODE_SECTION) ||
!strcmp(name, CONSTRUCTOR_PARAMS_SECTION) ||
!strcmp(name, CPP_SECTION) ||
!strcmp(name, CPP_END_SECTION) ||
!strcmp(name, EMIT_CODE_SECTION) ||
!strcmp(name, FIELDS_SECTION) ||
!strcmp(name, HEADER_SECTION) ||
!strcmp(name, HEADER_END_SECTION) ||
!strcmp(name, INITIALIZERS_SECTION) ||
!strcmp(name, MAKE_SECTION) ||
!strcmp(name, OPTIMIZATION_FLAGS_SECTION) ||
!strcmp(name, SAMPLER_PARAMS_SECTION) ||
!strcmp(name, SET_DATA_SECTION) ||
!strcmp(name, TEST_CODE_SECTION);
return !strcmp(name, kClassSection) ||
!strcmp(name, kCloneSection) ||
!strcmp(name, kConstructorSection) ||
!strcmp(name, kConstructorCodeSection) ||
!strcmp(name, kConstructorParamsSection) ||
!strcmp(name, kCppSection) ||
!strcmp(name, kCppEndSection) ||
!strcmp(name, kEmitCodeSection) ||
!strcmp(name, kFieldsSection) ||
!strcmp(name, kHeaderSection) ||
!strcmp(name, kHeaderEndSection) ||
!strcmp(name, kInitializersSection) ||
!strcmp(name, kMakeSection) ||
!strcmp(name, kOptimizationFlagsSection) ||
!strcmp(name, kSamplerParamsSection) ||
!strcmp(name, kSetDataSection) ||
!strcmp(name, kTestCodeSection);
}
static bool SectionAcceptsArgument(const char* name) {
return !strcmp(name, SAMPLER_PARAMS_SECTION) ||
!strcmp(name, SET_DATA_SECTION) ||
!strcmp(name, TEST_CODE_SECTION);
return !strcmp(name, kSamplerParamsSection) ||
!strcmp(name, kSetDataSection) ||
!strcmp(name, kTestCodeSection);
}
static bool SectionRequiresArgument(const char* name) {
return !strcmp(name, SAMPLER_PARAMS_SECTION) ||
!strcmp(name, SET_DATA_SECTION) ||
!strcmp(name, TEST_CODE_SECTION);
return !strcmp(name, kSamplerParamsSection) ||
!strcmp(name, kSetDataSection) ||
!strcmp(name, kTestCodeSection);
}
static bool SectionPermitsDuplicates(const char* name) {
return !strcmp(name, SAMPLER_PARAMS_SECTION);
return !strcmp(name, kSamplerParamsSection);
}
private: