2021-02-06 10:51:04 -03:00
|
|
|
/*
|
2022-09-14 07:24:13 -03:00
|
|
|
* Copyright (c) 2021-2022, the SerenityOS developers.
|
2021-02-06 10:51:04 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-02-06 10:51:04 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-08-08 15:13:02 -03:00
|
|
|
#include <AK/LexicalPath.h>
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-09-14 07:24:13 -03:00
|
|
|
#include <LibCore/Stream.h>
|
2021-02-06 10:51:04 -03:00
|
|
|
#include <LibCpp/Preprocessor.h>
|
2021-11-27 17:18:01 -03:00
|
|
|
#include <LibMain/Main.h>
|
2021-02-06 10:51:04 -03:00
|
|
|
|
2021-11-27 17:18:01 -03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-02-06 10:51:04 -03:00
|
|
|
{
|
2021-08-08 15:13:02 -03:00
|
|
|
Core::ArgsParser args_parser;
|
2022-09-14 07:24:13 -03:00
|
|
|
StringView path;
|
2021-08-08 15:13:02 -03:00
|
|
|
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');
|
2021-11-27 17:18:01 -03:00
|
|
|
args_parser.parse(arguments);
|
2022-09-14 07:24:13 -03:00
|
|
|
|
|
|
|
|
auto file = TRY(Core::Stream::File::open(path, Core::Stream::OpenMode::Read));
|
2022-12-11 13:49:00 -03:00
|
|
|
auto content = TRY(file->read_until_eof());
|
2022-12-04 15:02:33 -03:00
|
|
|
DeprecatedString name = LexicalPath::basename(path);
|
2021-08-08 15:13:02 -03:00
|
|
|
Cpp::Preprocessor cpp(name, StringView { content });
|
2021-08-06 04:29:57 -03:00
|
|
|
auto tokens = cpp.process_and_lex();
|
2021-08-11 16:31:43 -03:00
|
|
|
|
2021-08-08 15:13:02 -03:00
|
|
|
if (print_definitions) {
|
|
|
|
|
outln("Definitions:");
|
2022-09-14 07:24:13 -03:00
|
|
|
for (auto const& definition : cpp.definitions()) {
|
2021-08-08 15:13:02 -03:00
|
|
|
if (definition.value.parameters.is_empty())
|
|
|
|
|
outln("{}: {}", definition.key, definition.value.value);
|
|
|
|
|
else
|
2022-12-04 15:02:33 -03:00
|
|
|
outln("{}({}): {}", definition.key, DeprecatedString::join(',', definition.value.parameters), definition.value.value);
|
2021-08-08 15:13:02 -03:00
|
|
|
}
|
|
|
|
|
outln("");
|
2021-08-11 16:31:43 -03:00
|
|
|
}
|
|
|
|
|
|
2021-08-06 04:29:57 -03:00
|
|
|
for (auto& token : tokens) {
|
2022-12-05 22:12:49 -03:00
|
|
|
outln("{}", token.to_deprecated_string());
|
2021-08-06 04:29:57 -03:00
|
|
|
}
|
2021-11-27 17:18:01 -03:00
|
|
|
|
|
|
|
|
return 0;
|
2021-02-06 10:51:04 -03:00
|
|
|
}
|