Fix the complexity of errors-backeds run

This commit is contained in:
Sameer Rahmani 2022-01-23 15:08:38 +00:00
parent 1c935928e5
commit 7638c631ed
4 changed files with 77 additions and 141 deletions

View File

@ -1,89 +0,0 @@
# Serene Programming Language
#
# Copyright (c) 2019-2021 Sameer Rahmani <lxsameer@gnu.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
macro(add_ target project)
set(${target}_OLD_LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS})
set(LLVM_LINK_COMPONENTS ${LLVM_LINK_COMPONENTS} TableGen)
# CMake doesn't let compilation units depend on their dependent libraries on some generators.
if(NOT CMAKE_GENERATOR STREQUAL "Ninja" AND NOT XCODE)
# FIXME: It leaks to user, callee of add_tablegen.
set(LLVM_ENABLE_OBJLIB ON)
endif()
set(LLVM_LINK_COMPONENTS ${${target}_OLD_LLVM_LINK_COMPONENTS})
set(${project}_TABLEGEN "${target}" CACHE
STRING "Native TableGen executable. Saves building one when cross-compiling.")
# Effective tblgen executable to be used:
set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN} PARENT_SCOPE)
set(${project}_TABLEGEN_TARGET ${${project}_TABLEGEN} PARENT_SCOPE)
if(LLVM_USE_HOST_TOOLS)
if( ${${project}_TABLEGEN} STREQUAL "${target}" )
# The NATIVE tablegen executable *must* depend on the current target one
# otherwise the native one won't get rebuilt when the tablgen sources
# change, and we end up with incorrect builds.
build_native_tool(${target} ${project}_TABLEGEN_EXE DEPENDS ${target})
set(${project}_TABLEGEN_EXE ${${project}_TABLEGEN_EXE} PARENT_SCOPE)
add_custom_target(${project}-tablegen-host DEPENDS ${${project}_TABLEGEN_EXE})
set(${project}_TABLEGEN_TARGET ${project}-tablegen-host PARENT_SCOPE)
# Create an artificial dependency between tablegen projects, because they
# compile the same dependencies, thus using the same build folders.
# FIXME: A proper fix requires sequentially chaining tablegens.
if (NOT ${project} STREQUAL LLVM AND TARGET ${project}-tablegen-host AND
TARGET LLVM-tablegen-host)
add_dependencies(${project}-tablegen-host LLVM-tablegen-host)
endif()
# If we're using the host tablegen, and utils were not requested, we have no
# need to build this tablegen.
if ( NOT LLVM_BUILD_UTILS )
set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL ON)
endif()
endif()
endif()
if ((${project} STREQUAL LLVM OR ${project} STREQUAL MLIR) AND NOT LLVM_INSTALL_TOOLCHAIN_ONLY AND LLVM_BUILD_UTILS)
set(export_to_llvmexports)
if(${target} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR
NOT LLVM_DISTRIBUTION_COMPONENTS)
set(export_to_llvmexports EXPORT LLVMExports)
endif()
install(TARGETS ${target}
${export_to_llvmexports}
COMPONENT ${target}
RUNTIME DESTINATION ${LLVM_TOOLS_INSTALL_DIR})
if(NOT LLVM_ENABLE_IDE)
add_llvm_install_targets("install-${target}"
DEPENDS ${target}
COMPONENT ${target})
endif()
endif()
set_property(GLOBAL APPEND PROPERTY LLVM_EXPORTS ${target})
endmacro()
function(serene_tablegen ofn)
tablegen(SERENE ${ARGV})
set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn}
PARENT_SCOPE)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
endfunction()

View File

@ -1 +0,0 @@
lxsameer@underworld.2515108:1638986649

View File

@ -14,6 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
function(serene_tablegen ofn)
tablegen(SERENE ${ARGV})
set(TABLEGEN_OUTPUT ${TABLEGEN_OUTPUT} ${CMAKE_CURRENT_BINARY_DIR}/${ofn}

View File

@ -40,14 +40,76 @@ private:
public:
ErrorsBackend(llvm::RecordKeeper &rk) : records(rk) {}
void createNSBody(llvm::raw_ostream &os);
void createErrorClass(const int id, llvm::Record &defRec,
llvm::raw_ostream &os);
void run(llvm::raw_ostream &os);
}; // emitter class
void ErrorsBackend::run(llvm::raw_ostream &os) {
static void inNamespace(llvm::StringRef name, llvm::raw_ostream &os,
std::function<void(llvm::raw_ostream &)> f) {
os << "namespace " << name << " {\n\n";
f(os);
os << "} // namespace " << name << "\n";
};
void ErrorsBackend::createErrorClass(const int id, llvm::Record &defRec,
llvm::raw_ostream &os) {
(void)records;
const auto recName = defRec.getName();
os << "class " << recName << " : public llvm::ErrorInfo<" << recName
<< "> {\n";
os << " static int ID = " << id << ";\n";
for (const auto &val : defRec.getValues()) {
auto valName = val.getName();
if (!(valName == "title" || valName == "description")) {
llvm::PrintWarning("Only 'title' and 'description' are allowed.");
llvm::PrintWarning("Record: " + recName);
continue;
}
auto *stringVal = llvm::dyn_cast<llvm::StringInit>(val.getValue());
if (stringVal == nullptr) {
llvm::PrintError("The value of " + valName + " is not string.");
llvm::PrintError("Record: " + recName);
continue;
}
if (stringVal->getValue().empty()) {
llvm::PrintError("The value of " + valName + " is an empty string.");
llvm::PrintError("Record: " + recName);
continue;
}
os << " static std::string " << valName << " = ";
const llvm::MemoryBufferRef value(stringVal->getValue(), valName);
llvm::line_iterator lines(value, false);
while (!lines.is_at_end()) {
if (lines.line_number() != 1) {
os << '\t';
}
auto prevLine = *lines;
lines++;
os << '"' << prevLine << '"';
if (lines.is_at_end()) {
os << ";\n";
} else {
os << '\n';
}
}
}
os << "};\n\n";
};
void ErrorsBackend::createNSBody(llvm::raw_ostream &os) {
int counter = 1;
llvm::emitSourceFileHeader("Serene's Errors collection", os);
for (const auto &defPair : records.getDefs()) {
llvm::Record &defRec = *defPair.second;
@ -55,60 +117,23 @@ void ErrorsBackend::run(llvm::raw_ostream &os) {
continue;
}
const auto recName = defRec.getName();
os << "class " << recName << " : public llvm::ErrorInfo<" << recName
<< "> {\n";
os << " static int ID = " << counter << ";\n";
for (const auto &val : defRec.getValues()) {
auto valName = val.getName();
if (!(valName == "title" || valName == "description")) {
llvm::PrintWarning("Only 'title' and 'description' are allowed.");
llvm::PrintWarning("Record: " + recName);
continue;
}
auto *stringVal = llvm::dyn_cast<llvm::StringInit>(val.getValue());
if (stringVal == nullptr) {
llvm::PrintError("The value of " + valName + " is not string.");
llvm::PrintError("Record: " + recName);
continue;
}
if (stringVal->getValue().empty()) {
llvm::PrintError("The value of " + valName + " is an empty string.");
llvm::PrintError("Record: " + recName);
continue;
}
os << " static std::string " << valName << " = ";
const llvm::MemoryBufferRef value(stringVal->getValue(), valName);
llvm::line_iterator lines(value, false);
while (!lines.is_at_end()) {
if (lines.line_number() != 1) {
os << '\t';
}
auto prevLine = *lines;
lines++;
os << '"' << prevLine << '"';
if (lines.is_at_end()) {
os << ";\n";
} else {
os << '\n';
}
}
}
createErrorClass(counter, defRec, os);
counter++;
os << "};\n\n";
}
(void)records;
}
void ErrorsBackend::run(llvm::raw_ostream &os) {
(void)records;
llvm::emitSourceFileHeader("Serene's Errors collection", os);
os << "#include <llvm/Support/Error.h>\n\n";
inNamespace("serene::errors", os,
[&](llvm::raw_ostream &os) { createNSBody(os); });
}
void emitErrors(llvm::RecordKeeper &rk, llvm::raw_ostream &os) {
ErrorsBackend(rk).run(os);
}