mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 21:37:58 +03: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:
parent
f6c9071f0d
commit
fd864fbb74
Notes:
sideshowbarker
2024-07-18 06:59:42 +09:00
Author: https://github.com/itamar8910 Commit: https://github.com/SerenityOS/serenity/commit/fd864fbb74a Pull-request: https://github.com/SerenityOS/serenity/pull/9402
@ -4,31 +4,41 @@
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/File.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)) {
|
||||
perror("open");
|
||||
exit(1);
|
||||
}
|
||||
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();
|
||||
|
||||
outln("Definitions:");
|
||||
for (auto& definition : cpp.definitions()) {
|
||||
if (definition.value.parameters.is_empty())
|
||||
outln("{}: {}", definition.key, definition.value.value);
|
||||
else
|
||||
outln("{}({}): {}", definition.key, String::join(",", definition.value.parameters), definition.value.value);
|
||||
if (print_definitions) {
|
||||
outln("Definitions:");
|
||||
for (auto& definition : cpp.definitions()) {
|
||||
if (definition.value.parameters.is_empty())
|
||||
outln("{}: {}", definition.key, definition.value.value);
|
||||
else
|
||||
outln("{}({}): {}", definition.key, String::join(",", definition.value.parameters), definition.value.value);
|
||||
}
|
||||
outln("");
|
||||
}
|
||||
|
||||
outln("");
|
||||
|
||||
for (auto& token : tokens) {
|
||||
dbgln("{}", token.to_string());
|
||||
outln("{}", token.to_string());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user