1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:18:11 +00:00

Utilities: Add 'file' and 'definitions' arguments to cpp-preprocessor

The 'file' argument allows specifying a file path.
The 'definitions' flag controls whether preprocessor definitions are
printed.
This commit is contained in:
Itamar 2021-08-08 21:13:02 +03:00 committed by Andreas Kling
parent f6c9071f0d
commit fd864fbb74

View file

@ -4,31 +4,41 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/LexicalPath.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h> #include <LibCore/File.h>
#include <LibCpp/Preprocessor.h> #include <LibCpp/Preprocessor.h>
int main(int, char**) int main(int argc, char** argv)
{ {
auto file = Core::File::construct("/home/anon/Source/little/other.h"); Core::ArgsParser args_parser;
const char* path = nullptr;
bool print_definitions = false;
args_parser.add_positional_argument(path, "File", "file", Core::ArgsParser::Required::Yes);
args_parser.add_option(print_definitions, "Print preprocessor definitions", "definitions", 'D');
args_parser.parse(argc, argv);
auto file = Core::File::construct(path);
if (!file->open(Core::OpenMode::ReadOnly)) { if (!file->open(Core::OpenMode::ReadOnly)) {
perror("open"); perror("open");
exit(1); exit(1);
} }
auto content = file->read_all(); auto content = file->read_all();
Cpp::Preprocessor cpp("other.h", StringView { content }); String name = LexicalPath::basename(path);
Cpp::Preprocessor cpp(name, StringView { content });
auto tokens = cpp.process_and_lex(); auto tokens = cpp.process_and_lex();
outln("Definitions:"); if (print_definitions) {
for (auto& definition : cpp.definitions()) { outln("Definitions:");
if (definition.value.parameters.is_empty()) for (auto& definition : cpp.definitions()) {
outln("{}: {}", definition.key, definition.value.value); if (definition.value.parameters.is_empty())
else outln("{}: {}", definition.key, definition.value.value);
outln("{}({}): {}", definition.key, String::join(",", definition.value.parameters), definition.value.value); else
outln("{}({}): {}", definition.key, String::join(",", definition.value.parameters), definition.value.value);
}
outln("");
} }
outln("");
for (auto& token : tokens) { for (auto& token : tokens) {
dbgln("{}", token.to_string()); outln("{}", token.to_string());
} }
} }