Skip to content

[lldb] Use Python script to generate SBLanguages.h #90753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions lldb/scripts/generate-sbapi-dwarf-enum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3

import argparse
import re

HEADER = """\
//===-- SBLanguages.h -----------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_API_SBLANGUAGE_H
#define LLDB_API_SBLANGUAGE_H
/// Used by \\ref SBExpressionOptions.
/// These enumerations use the same language enumerations as the DWARF
/// specification for ease of use and consistency.
enum SBSourceLanguageName : uint16_t {
"""

FOOTER = """\
};

#endif
"""

REGEX = re.compile(
r'^ *HANDLE_DW_LNAME *\( *(?P<value>[^,]+), (?P<comment>[^"]+), "(?P<name>.*)",.*\)'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to combine my feedback with Adrian's:

r'^ *HANDLE_DW_LNAME *\( *(?P<value>[^,]+), (?P<name>[^,]+), "(?P<comment>[^"]*)",.*\)'

)


def emit_enum(input, output):
# Read the input and break it up by lines.
lines = []
with open(input, "r") as f:
lines = f.readlines()

# Write the output.
with open(output, "w") as f:
# Emit the header.
f.write(HEADER)

# Emit the enum values.
for line in lines:
match = REGEX.match(line)
if not match:
continue
f.write(f" /// {match.group('comment')}.\n")
f.write(f" eLanguageName{match.group('name')} = {match.group('value')},\n")

# Emit the footer
f.write(FOOTER)


def main():
parser = argparse.ArgumentParser()
parser.add_argument("--output", "-o")
parser.add_argument("input")
args = parser.parse_args()

emit_enum(args.input, args.output)


if __name__ == "__main__":
main()
18 changes: 14 additions & 4 deletions lldb/source/API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@ if(LLDB_ENABLE_LUA)
set(lldb_lua_wrapper ${lua_bindings_dir}/LLDBWrapLua.cpp)
endif()

lldb_tablegen(../../include/lldb/API/SBLanguages.h -gen-lldb-sbapi-dwarf-enum
SOURCE ${LLVM_MAIN_INCLUDE_DIR}/llvm/BinaryFormat/Dwarf.def
TARGET lldb-sbapi-dwarf-enums)
# Target to generate SBLanguages.h from Dwarf.def.
set(sb_languages_file
${CMAKE_CURRENT_BINARY_DIR}/../../include/lldb/API/SBLanguages.h)
add_custom_target(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should be using add_custom_command. Per the cmake docs, add_custom_target "... has no output file and is always considered out of date even if the commands try to create a file with the name of the target". This causes an incremental clean build to always run this command and also rebuild anything that depends on it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lldb-sbapi-dwarf-enums
"${Python3_EXECUTABLE}"
${LLDB_SOURCE_DIR}/scripts/generate-sbapi-dwarf-enum.py
${LLVM_MAIN_INCLUDE_DIR}/llvm/BinaryFormat/Dwarf.def
-o ${sb_languages_file}
BYPRODUCTS ${sb_languages_file}
DEPENDS ${LLVM_MAIN_INCLUDE_DIR}/llvm/BinaryFormat/Dwarf.def
WORKING_DIRECTORY ${LLVM_LIBRARY_OUTPUT_INTDIR}
)

add_lldb_library(liblldb SHARED ${option_framework}
SBAddress.cpp
Expand Down Expand Up @@ -106,7 +116,7 @@ add_lldb_library(liblldb SHARED ${option_framework}

DEPENDS
lldb-sbapi-dwarf-enums

LINK_LIBS
lldbBreakpoint
lldbCore
Expand Down
1 change: 0 additions & 1 deletion lldb/utils/TableGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ if (NOT DEFINED LLDB_TABLEGEN_EXE)
add_tablegen(lldb-tblgen LLDB
LLDBOptionDefEmitter.cpp
LLDBPropertyDefEmitter.cpp
LLDBSBAPIDWARFEnum.cpp
LLDBTableGen.cpp
LLDBTableGenUtils.cpp
)
Expand Down
67 changes: 0 additions & 67 deletions lldb/utils/TableGen/LLDBSBAPIDWARFEnum.cpp

This file was deleted.

8 changes: 0 additions & 8 deletions lldb/utils/TableGen/LLDBTableGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ enum ActionType {
GenOptionDefs,
GenPropertyDefs,
GenPropertyEnumDefs,
GenSBAPIDWARFEnum
};

static cl::opt<ActionType> Action(
Expand All @@ -41,8 +40,6 @@ static cl::opt<ActionType> Action(
clEnumValN(GenPropertyDefs, "gen-lldb-property-defs",
"Generate lldb property definitions"),
clEnumValN(GenPropertyEnumDefs, "gen-lldb-property-enum-defs",
"Generate lldb property enum definitions"),
clEnumValN(GenSBAPIDWARFEnum, "gen-lldb-sbapi-dwarf-enum",
"Generate lldb property enum definitions")));

static bool LLDBTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
Expand All @@ -62,8 +59,6 @@ static bool LLDBTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
case GenPropertyEnumDefs:
EmitPropertyEnumDefs(Records, OS);
break;
case GenSBAPIDWARFEnum:
llvm_unreachable("already handled");
}
return false;
}
Expand All @@ -74,9 +69,6 @@ int main(int argc, char **argv) {
cl::ParseCommandLineOptions(argc, argv);
llvm_shutdown_obj Y;

if (Action == GenSBAPIDWARFEnum)
return EmitSBAPIDWARFEnum(argc, argv);

return TableGenMain(argv[0], &LLDBTableGenMain);
}

Expand Down