diff options
| author | Ellie Hermaszewska <ellieh@nvidia.com> | 2024-10-29 14:49:26 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-29 14:49:26 +0800 |
| commit | f65d756bff8d4c5cbc15bd0322a2ae8e6b896a21 (patch) | |
| tree | ea1d61342cd29368e19135000ec2948813096205 /tools/slang-cpp-extractor | |
| parent | a729c15e9dce9f5116a38afc66329ab2ca4cea54 (diff) | |
format
* format
* Minor test fixes
* enable checking cpp format in ci
Diffstat (limited to 'tools/slang-cpp-extractor')
20 files changed, 872 insertions, 637 deletions
diff --git a/tools/slang-cpp-extractor/cpp-extractor-main.cpp b/tools/slang-cpp-extractor/cpp-extractor-main.cpp index e3bb041be..78ae336fa 100644 --- a/tools/slang-cpp-extractor/cpp-extractor-main.cpp +++ b/tools/slang-cpp-extractor/cpp-extractor-main.cpp @@ -1,39 +1,38 @@ // main.cpp -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include "../../source/core/slang-secure-crt.h" - -#include "slang-com-helper.h" - -#include "../../source/core/slang-list.h" -#include "../../source/core/slang-string.h" -#include "../../source/core/slang-string-util.h" +#include "../../source/compiler-core/slang-diagnostic-sink.h" +#include "../../source/compiler-core/slang-doc-extractor.h" +#include "../../source/compiler-core/slang-lexer.h" +#include "../../source/compiler-core/slang-name-convention-util.h" +#include "../../source/compiler-core/slang-name.h" +#include "../../source/compiler-core/slang-source-loc.h" +#include "../../source/core/slang-file-system.h" #include "../../source/core/slang-io.h" +#include "../../source/core/slang-list.h" +#include "../../source/core/slang-secure-crt.h" #include "../../source/core/slang-string-slice-pool.h" +#include "../../source/core/slang-string-util.h" +#include "../../source/core/slang-string.h" #include "../../source/core/slang-writer.h" -#include "../../source/core/slang-file-system.h" - -#include "../../source/compiler-core/slang-source-loc.h" -#include "../../source/compiler-core/slang-lexer.h" -#include "../../source/compiler-core/slang-diagnostic-sink.h" -#include "../../source/compiler-core/slang-name.h" -#include "../../source/compiler-core/slang-name-convention-util.h" -#include "../../source/compiler-core/slang-doc-extractor.h" - -#include "node.h" #include "diagnostics.h" +#include "file-util.h" +#include "macro-writer.h" +#include "node.h" #include "options.h" #include "parser.h" -#include "macro-writer.h" -#include "file-util.h" +#include "slang-com-helper.h" #include "unit-test.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + /* Some command lines: --d source/slang slang-ast-support-types.h slang-ast-base.h slang-ast-decl.h slang-ast-expr.h slang-ast-modifier.h slang-ast-stmt.h slang-ast-type.h slang-ast-val.h -strip-prefix slang- -o slang-generated -output-fields -mark-suffix _CLASS +-d source/slang slang-ast-support-types.h slang-ast-base.h slang-ast-decl.h slang-ast-expr.h +slang-ast-modifier.h slang-ast-stmt.h slang-ast-type.h slang-ast-val.h -strip-prefix slang- -o +slang-generated -output-fields -mark-suffix _CLASS */ namespace CppExtract @@ -46,24 +45,20 @@ using namespace Slang; class App { public: - SlangResult execute(const Options& options); - /// Execute - SlangResult executeWithArgs(int argc, const char*const* argv); + /// Execute + SlangResult executeWithArgs(int argc, const char* const* argv); const Options& getOptions() const { return m_options; } - App(DiagnosticSink* sink, SourceManager* sourceManager, RootNamePool* rootNamePool): - m_sink(sink), - m_sourceManager(sourceManager), - m_slicePool(StringSlicePool::Style::Default) + App(DiagnosticSink* sink, SourceManager* sourceManager, RootNamePool* rootNamePool) + : m_sink(sink), m_sourceManager(sourceManager), m_slicePool(StringSlicePool::Style::Default) { m_namePool.setRootNamePool(rootNamePool); } protected: - SlangResult _extractDoc(NodeTree* nodeTree); NamePool m_namePool; @@ -71,7 +66,7 @@ protected: Options m_options; DiagnosticSink* m_sink; SourceManager* m_sourceManager; - + StringSlicePool m_slicePool; }; @@ -79,7 +74,8 @@ protected: // Work out an appropriate search type for a node type. // // TODO(JS): -// NOTE! Currently extractor doesn't extract callable types and so doesn't extract callable types parameters +// NOTE! Currently extractor doesn't extract callable types and so doesn't extract callable types +// parameters static DocMarkupExtractor::SearchStyle _getSearchStyle(Node* node) { typedef DocMarkupExtractor::SearchStyle SearchStyle; @@ -91,27 +87,27 @@ static DocMarkupExtractor::SearchStyle _getSearchStyle(Node* node) switch (node->m_kind) { - case Node::Kind::Invalid: + case Node::Kind::Invalid: { return SearchStyle::None; } - case Node::Kind::Field: + case Node::Kind::Field: { return SearchStyle::Variable; } - case Node::Kind::EnumCase: + case Node::Kind::EnumCase: { return SearchStyle::EnumCase; } - case Node::Kind::TypeDef: + case Node::Kind::TypeDef: { return SearchStyle::Variable; } - case Node::Kind::Callable: + case Node::Kind::Callable: { return SearchStyle::Before; } - default: break; + default: break; } // Default is to only allow before. @@ -162,7 +158,13 @@ SlangResult App::_extractDoc(NodeTree* nodeTree) DocMarkupExtractor extractor; - SLANG_RETURN_ON_FAIL(extractor.extract(inputItems.getBuffer(), inputItems.getCount(), m_sourceManager, m_sink, views, outputItems)); + SLANG_RETURN_ON_FAIL(extractor.extract( + inputItems.getBuffer(), + inputItems.getCount(), + m_sourceManager, + m_sink, + views, + outputItems)); // Put what was extracted into the nodes { @@ -177,7 +179,8 @@ SlangResult App::_extractDoc(NodeTree* nodeTree) const auto inputIndex = outputItem.inputIndex; const auto& inputItem = inputItems[inputIndex]; - if (inputItem.searchStyle != DocMarkupExtractor::SearchStyle::None && outputItem.text.getLength()) + if (inputItem.searchStyle != DocMarkupExtractor::SearchStyle::None && + outputItem.text.getLength()) { Node* node = nodes[inputIndex]; @@ -238,15 +241,20 @@ SlangResult App::execute(const Options& options) { for (TypeSet* typeSet : tree.getTypeSets()) { - // The macro name is in upper snake, so split it + // The macro name is in upper snake, so split it List<UnownedStringSlice> slices; NameConventionUtil::split(typeSet->m_macroName, slices); if (typeSet->m_fileMark.getLength() == 0) { StringBuilder buf; - // Let's guess a 'fileMark' (it becomes part of the filename) based on the macro name. Use lower kabab. - NameConventionUtil::join(slices.getBuffer(), slices.getCount(), NameConvention::LowerKabab, buf); + // Let's guess a 'fileMark' (it becomes part of the filename) based on the macro + // name. Use lower kabab. + NameConventionUtil::join( + slices.getBuffer(), + slices.getCount(), + NameConvention::LowerKabab, + buf); typeSet->m_fileMark = buf.produceString(); } @@ -254,7 +262,11 @@ SlangResult App::execute(const Options& options) { // Let's guess a typename if not set -> go with upper camel StringBuilder buf; - NameConventionUtil::join(slices.getBuffer(), slices.getCount(), NameConvention::UpperCamel, buf); + NameConventionUtil::join( + slices.getBuffer(), + slices.getCount(), + NameConvention::UpperCamel, + buf); typeSet->m_typeName = buf.produceString(); } } @@ -303,7 +315,7 @@ SlangResult App::execute(const Options& options) } /// Execute -SlangResult App::executeWithArgs(int argc, const char*const* argv) +SlangResult App::executeWithArgs(int argc, const char* const* argv) { Options options; OptionsParser optionsParser; @@ -316,10 +328,13 @@ SlangResult App::executeWithArgs(int argc, const char*const* argv) /* -The typical command line for producing generated slang files. Can be determined by setting `dumpCommandLine` belong and compiling. +The typical command line for producing generated slang files. Can be determined by setting +`dumpCommandLine` belong and compiling. ``` --d E:\git\slang-jsmall-nvidia\source\slang\ slang-ast-support-types.h slang-ast-base.h slang-ast-decl.h slang-ast-expr.h slang-ast-modifier.h slang-ast-stmt.h slang-ast-type.h slang-ast-val.h -strip-prefix slang- -o slang-generated -output-fields -mark-suffix _CLASS +-d E:\git\slang-jsmall-nvidia\source\slang\ slang-ast-support-types.h slang-ast-base.h +slang-ast-decl.h slang-ast-expr.h slang-ast-modifier.h slang-ast-stmt.h slang-ast-type.h +slang-ast-val.h -strip-prefix slang- -o slang-generated -output-fields -mark-suffix _CLASS ``` A command line to try and parse the slang.h @@ -329,7 +344,7 @@ A command line to try and parse the slang.h ``` */ -int main(int argc, const char*const* argv) +int main(int argc, const char* const* argv) { using namespace CppExtract; using namespace Slang; @@ -345,8 +360,8 @@ int main(int argc, const char*const* argv) DiagnosticSink sink(&sourceManager, Lexer::sourceLocationLexer); sink.writer = writer; - // Set to true to see command line that initiated C++ extractor. Helpful when finding issues from solution building failing, and then so - // being able to repeat the issue + // Set to true to see command line that initiated C++ extractor. Helpful when finding issues + // from solution building failing, and then so being able to repeat the issue bool dumpCommandLine = false; if (dumpCommandLine) @@ -384,4 +399,3 @@ int main(int argc, const char*const* argv) } return 0; } - diff --git a/tools/slang-cpp-extractor/diagnostic-defs.h b/tools/slang-cpp-extractor/diagnostic-defs.h index 85e88d64d..290036a23 100644 --- a/tools/slang-cpp-extractor/diagnostic-defs.h +++ b/tools/slang-cpp-extractor/diagnostic-defs.h @@ -41,19 +41,39 @@ DIAGNOSTIC(100007, Error, superTypeNotFound, "Super type not found for $0") DIAGNOSTIC(100008, Error, superTypeNotAType, "Named super type is not a type $0") DIAGNOSTIC(100009, Error, unexpectedUnbalancedToken, "Unexpected unbalanced token") DIAGNOSTIC(100010, Error, unexpectedEndOfFile, "Unexpected end of file") -DIAGNOSTIC(100011, Error, expectingTypeKeyword, "Expecting type keyword - struct or class, found $0") +DIAGNOSTIC( + 100011, + Error, + expectingTypeKeyword, + "Expecting type keyword - struct or class, found $0") -DIAGNOSTIC(100012, Error, typeInDifferentTypeSet, "Type $0 in different type set $1 from super class $2") +DIAGNOSTIC( + 100012, + Error, + typeInDifferentTypeSet, + "Type $0 in different type set $1 from super class $2") DIAGNOSTIC(100013, Error, expectingIdentifier, "Expecting an identifier, found $0") DIAGNOSTIC(100014, Error, cannotDeclareTypeInScope, "Cannot declare types in this scope") DIAGNOSTIC(100015, Error, identifierAlreadyDefined, "Identifier already defined '$0'") DIAGNOSTIC(100016, Error, expectingType, "Expecting a type") DIAGNOSTIC(100017, Error, cannotParseExpression, "Cannot parse expression") DIAGNOSTIC(100018, Error, cannotOverload, "Cannot overload this declaration"); -DIAGNOSTIC(100019, Error, classMarkerOutsideOfClass, "A class/struct marker is found outside of class or struct"); -DIAGNOSTIC(100020, Error, classMarkerAlreadyFound, "A class/struct marker already found in type '$0'"); +DIAGNOSTIC( + 100019, + Error, + classMarkerOutsideOfClass, + "A class/struct marker is found outside of class or struct"); +DIAGNOSTIC( + 100020, + Error, + classMarkerAlreadyFound, + "A class/struct marker already found in type '$0'"); DIAGNOSTIC(100021, Error, cannotParseType, "Cannot parse type"); -DIAGNOSTIC(100022, Error, destructorNameDoesntMatch, "Destructor name doesn't match class name '$0'"); +DIAGNOSTIC( + 100022, + Error, + destructorNameDoesntMatch, + "Destructor name doesn't match class name '$0'"); DIAGNOSTIC(100023, Error, cannotParseCallable, "Cannot parse callable"); // Command line errors 100100 diff --git a/tools/slang-cpp-extractor/diagnostics.cpp b/tools/slang-cpp-extractor/diagnostics.cpp index 3abbe1b38..782e5e9da 100644 --- a/tools/slang-cpp-extractor/diagnostics.cpp +++ b/tools/slang-cpp-extractor/diagnostics.cpp @@ -1,13 +1,15 @@ #include "diagnostics.h" -namespace CppExtract { +namespace CppExtract +{ namespace CPPDiagnostics { using namespace Slang; -#define DIAGNOSTIC(id, severity, name, messageFormat) const DiagnosticInfo name = { id, Severity::severity, #name, messageFormat }; +#define DIAGNOSTIC(id, severity, name, messageFormat) \ + const DiagnosticInfo name = {id, Severity::severity, #name, messageFormat}; #include "diagnostic-defs.h" -} +} // namespace CPPDiagnostics } // namespace CppExtract diff --git a/tools/slang-cpp-extractor/diagnostics.h b/tools/slang-cpp-extractor/diagnostics.h index 3a98beee3..cb3bd342b 100644 --- a/tools/slang-cpp-extractor/diagnostics.h +++ b/tools/slang-cpp-extractor/diagnostics.h @@ -3,15 +3,17 @@ #include "../../source/slang/slang-diagnostics.h" -namespace CppExtract { +namespace CppExtract +{ using namespace Slang; -namespace CPPDiagnostics { +namespace CPPDiagnostics +{ #define DIAGNOSTIC(id, severity, name, messageFormat) extern const DiagnosticInfo name; #include "diagnostic-defs.h" -} // CPPDiagnostics -} // CppExtract +} // namespace CPPDiagnostics +} // namespace CppExtract #endif diff --git a/tools/slang-cpp-extractor/file-util.cpp b/tools/slang-cpp-extractor/file-util.cpp index e73ba7f55..2980a22ce 100644 --- a/tools/slang-cpp-extractor/file-util.cpp +++ b/tools/slang-cpp-extractor/file-util.cpp @@ -2,10 +2,12 @@ #include "../../source/core/slang-io.h" -namespace CppExtract { +namespace CppExtract +{ using namespace Slang; -namespace { // anonymous +namespace +{ // anonymous struct DiagnosticReporter { SlangResult report(SlangResult res) @@ -27,9 +29,8 @@ struct DiagnosticReporter return res; } - DiagnosticReporter(const String& filename, DiagnosticSink* sink) : - m_filename(filename), - m_sink(sink) + DiagnosticReporter(const String& filename, DiagnosticSink* sink) + : m_filename(filename), m_sink(sink) { } @@ -37,14 +38,18 @@ struct DiagnosticReporter String m_filename; }; -} // anonymous +} // namespace -/* static */SlangResult FileUtil::readAllText(const Slang::String& fileName, DiagnosticSink* sink, String& outRead) +/* static */ SlangResult FileUtil::readAllText( + const Slang::String& fileName, + DiagnosticSink* sink, + String& outRead) { DiagnosticReporter reporter(fileName, sink); - + RefPtr<FileStream> stream = new FileStream; - SLANG_RETURN_ON_FAIL(reporter.report(stream->init(fileName, FileMode::Open, FileAccess::Read, FileShare::ReadWrite))); + SLANG_RETURN_ON_FAIL(reporter.report( + stream->init(fileName, FileMode::Open, FileAccess::Read, FileShare::ReadWrite))); StreamReader reader; SLANG_RETURN_ON_FAIL(reporter.report(reader.init(stream))); @@ -53,12 +58,16 @@ struct DiagnosticReporter return SLANG_OK; } -/* static */SlangResult FileUtil::writeAllText(const Slang::String& fileName, DiagnosticSink* sink, const UnownedStringSlice& text) +/* static */ SlangResult FileUtil::writeAllText( + const Slang::String& fileName, + DiagnosticSink* sink, + const UnownedStringSlice& text) { // TODO(JS): - // There is an optimization/behavior here,that checks if the contents has changed. It only writes if it hasn't - // That might not be what you want (both because of extra work of read, the file modified stamp or other reasons, file is write only etc) - // NOTE! That this also does the work of the comparison after it is decoded, but the *bytes* might actually be different. + // There is an optimization/behavior here,that checks if the contents has changed. It only + // writes if it hasn't That might not be what you want (both because of extra work of read, the + // file modified stamp or other reasons, file is write only etc) NOTE! That this also does the + // work of the comparison after it is decoded, but the *bytes* might actually be different. if (File::exists(fileName)) { @@ -78,7 +87,7 @@ struct DiagnosticReporter StreamWriter writer; SLANG_RETURN_ON_FAIL(reporter.report(writer.init(stream))); SLANG_RETURN_ON_FAIL(reporter.report(writer.write(text))) - return SLANG_OK; + return SLANG_OK; } /* static */ void FileUtil::indent(Index indentCount, StringBuilder& out) diff --git a/tools/slang-cpp-extractor/file-util.h b/tools/slang-cpp-extractor/file-util.h index 01aafeedf..201825973 100644 --- a/tools/slang-cpp-extractor/file-util.h +++ b/tools/slang-cpp-extractor/file-util.h @@ -3,7 +3,8 @@ #include "diagnostics.h" -namespace CppExtract { +namespace CppExtract +{ using namespace Slang; // A macro to define a single indent as a string @@ -11,15 +12,23 @@ using namespace Slang; struct FileUtil { - /// Read text into outRead. Any failures written to sink (can be passed as nullptr, for no output) - static SlangResult readAllText(const Slang::String& fileName, DiagnosticSink* sink, String& outRead); - /// Write text to filename. Any failures written to sink. (can be passed as nullptr, for no output) - static SlangResult writeAllText(const Slang::String& fileName, DiagnosticSink* sink, const UnownedStringSlice& text); + /// Read text into outRead. Any failures written to sink (can be passed as nullptr, for no + /// output) + static SlangResult readAllText( + const Slang::String& fileName, + DiagnosticSink* sink, + String& outRead); + /// Write text to filename. Any failures written to sink. (can be passed as nullptr, for no + /// output) + static SlangResult writeAllText( + const Slang::String& fileName, + DiagnosticSink* sink, + const UnownedStringSlice& text); - /// Appends CPP_EXTRACT_INDENT_STRING indentCount number of times to out + /// Appends CPP_EXTRACT_INDENT_STRING indentCount number of times to out static void indent(Index indentCount, StringBuilder& out); }; -} // CppExtract +} // namespace CppExtract #endif diff --git a/tools/slang-cpp-extractor/identifier-lookup.cpp b/tools/slang-cpp-extractor/identifier-lookup.cpp index 6b60f573c..c7255aa1f 100644 --- a/tools/slang-cpp-extractor/identifier-lookup.cpp +++ b/tools/slang-cpp-extractor/identifier-lookup.cpp @@ -1,41 +1,42 @@ #include "identifier-lookup.h" -namespace CppExtract { +namespace CppExtract +{ using namespace Slang; -/* static */const IdentifierFlags IdentifierLookup::kIdentifierFlags[Index(IdentifierStyle::CountOf)] = -{ - 0, /// None - 0, /// Identifier - 0, /// Declare type - 0, /// Type set - IdentifierFlag::Keyword, /// TypeModifier - IdentifierFlag::Keyword, /// Keyword +/* static */ const IdentifierFlags + IdentifierLookup::kIdentifierFlags[Index(IdentifierStyle::CountOf)] = { + 0, /// None + 0, /// Identifier + 0, /// Declare type + 0, /// Type set + IdentifierFlag::Keyword, /// TypeModifier + IdentifierFlag::Keyword, /// Keyword - IdentifierFlag::Keyword | IdentifierFlag::StartScope | IdentifierFlag::ClassLike, /// Class - IdentifierFlag::Keyword | IdentifierFlag::StartScope | IdentifierFlag::ClassLike, /// Struct - IdentifierFlag::Keyword | IdentifierFlag::StartScope, /// Namespace - IdentifierFlag::Keyword | IdentifierFlag::StartScope, /// Enum + IdentifierFlag::Keyword | IdentifierFlag::StartScope | IdentifierFlag::ClassLike, /// Class + IdentifierFlag::Keyword | IdentifierFlag::StartScope | IdentifierFlag::ClassLike, /// Struct + IdentifierFlag::Keyword | IdentifierFlag::StartScope, /// Namespace + IdentifierFlag::Keyword | IdentifierFlag::StartScope, /// Enum - IdentifierFlag::Keyword, /// Typedef + IdentifierFlag::Keyword, /// Typedef - IdentifierFlag::Keyword, /// Access - IdentifierFlag::Reflection, /// Reflected - IdentifierFlag::Reflection, /// Unreflected + IdentifierFlag::Keyword, /// Access + IdentifierFlag::Reflection, /// Reflected + IdentifierFlag::Reflection, /// Unreflected - IdentifierFlag::Keyword, /// virtual - 0, /// Calling convention - IdentifierFlag::Keyword, /// template - IdentifierFlag::Keyword, /// static + IdentifierFlag::Keyword, /// virtual + 0, /// Calling convention + IdentifierFlag::Keyword, /// template + IdentifierFlag::Keyword, /// static - IdentifierFlag::Keyword, /// unsigned/signed + IdentifierFlag::Keyword, /// unsigned/signed - IdentifierFlag::Keyword, /// extern + IdentifierFlag::Keyword, /// extern - 0, /// Callable misc - 0, /// IntegerType int, short, char, long + 0, /// Callable misc + 0, /// IntegerType int, short, char, long - IdentifierFlag::Keyword, /// default + IdentifierFlag::Keyword, /// default }; void IdentifierLookup::set(const UnownedStringSlice& name, IdentifierStyle style) @@ -54,7 +55,7 @@ void IdentifierLookup::set(const UnownedStringSlice& name, IdentifierStyle style } } -void IdentifierLookup::set(const char*const* names, size_t namesCount, IdentifierStyle style) +void IdentifierLookup::set(const char* const* names, size_t namesCount, IdentifierStyle style) { for (size_t i = 0; i < namesCount; ++i) { @@ -77,20 +78,42 @@ void IdentifierLookup::initDefault(const UnownedStringSlice& markPrefix) // Some keywords { - const char* names[] = { "continue", "if", "case", "break", "catch", "delete", "do", "else", "for", "new", "goto", "return", "switch", "throw", "using", "while", "operator", "explicit"}; + const char* names[] = { + "continue", + "if", + "case", + "break", + "catch", + "delete", + "do", + "else", + "for", + "new", + "goto", + "return", + "switch", + "throw", + "using", + "while", + "operator", + "explicit"}; set(names, SLANG_COUNT_OF(names), IdentifierStyle::Keyword); } // Type modifier keywords { - const char* names[] = { "const", "volatile" }; + const char* names[] = {"const", "volatile"}; set(names, SLANG_COUNT_OF(names), IdentifierStyle::TypeModifier); } // Special markers { - const char* names[] = { "PRE_DECLARE", "TYPE_SET", "REFLECTED", "UNREFLECTED" }; - const IdentifierStyle styles[] = { IdentifierStyle::PreDeclare, IdentifierStyle::TypeSet, IdentifierStyle::Reflected, IdentifierStyle::Unreflected }; + const char* names[] = {"PRE_DECLARE", "TYPE_SET", "REFLECTED", "UNREFLECTED"}; + const IdentifierStyle styles[] = { + IdentifierStyle::PreDeclare, + IdentifierStyle::TypeSet, + IdentifierStyle::Reflected, + IdentifierStyle::Unreflected}; SLANG_COMPILE_TIME_ASSERT(SLANG_COUNT_OF(names) == SLANG_COUNT_OF(styles)); StringBuilder buf; @@ -104,7 +127,7 @@ void IdentifierLookup::initDefault(const UnownedStringSlice& markPrefix) { set("virtual", IdentifierStyle::Virtual); - + set("template", IdentifierStyle::Template); set("static", IdentifierStyle::Static); set("extern", IdentifierStyle::Extern); @@ -112,29 +135,28 @@ void IdentifierLookup::initDefault(const UnownedStringSlice& markPrefix) } { - const char* names[] = { "char", "short", "int", "long"}; + const char* names[] = {"char", "short", "int", "long"}; set(names, SLANG_COUNT_OF(names), IdentifierStyle::IntegerType); } { - const char* names[] = { "SLANG_MCALL" }; + const char* names[] = {"SLANG_MCALL"}; set(names, SLANG_COUNT_OF(names), IdentifierStyle::CallingConvention); } { - const char* names[] = { "SLANG_NO_THROW", "inline"}; + const char* names[] = {"SLANG_NO_THROW", "inline"}; set(names, SLANG_COUNT_OF(names), IdentifierStyle::CallableMisc); } // Keywords which introduce types/scopes { - const Pair pairs[] = - { - { "struct", IdentifierStyle::Struct }, - { "class", IdentifierStyle::Class }, - { "namespace", IdentifierStyle::Namespace }, - { "enum", IdentifierStyle::Enum }, - { "typedef", IdentifierStyle::TypeDef }, + const Pair pairs[] = { + {"struct", IdentifierStyle::Struct}, + {"class", IdentifierStyle::Class}, + {"namespace", IdentifierStyle::Namespace}, + {"enum", IdentifierStyle::Enum}, + {"typedef", IdentifierStyle::TypeDef}, }; set(pairs, SLANG_COUNT_OF(pairs)); @@ -142,11 +164,11 @@ void IdentifierLookup::initDefault(const UnownedStringSlice& markPrefix) // Keywords that control access { - const char* names[] = { "private", "protected", "public" }; + const char* names[] = {"private", "protected", "public"}; set(names, SLANG_COUNT_OF(names), IdentifierStyle::Access); } { - const char* names[] = { "signed", "unsigned"}; + const char* names[] = {"signed", "unsigned"}; set(names, SLANG_COUNT_OF(names), IdentifierStyle::IntegerModifier); } diff --git a/tools/slang-cpp-extractor/identifier-lookup.h b/tools/slang-cpp-extractor/identifier-lookup.h index 0d55ba65c..f26220f1f 100644 --- a/tools/slang-cpp-extractor/identifier-lookup.h +++ b/tools/slang-cpp-extractor/identifier-lookup.h @@ -3,49 +3,50 @@ #include "diagnostics.h" -namespace CppExtract { +namespace CppExtract +{ using namespace Slang; enum class IdentifierStyle { - None, ///< It's not an identifier + None, ///< It's not an identifier - Identifier, ///< Just an identifier + Identifier, ///< Just an identifier - PreDeclare, ///< Declare a type (not visible in C++ code) - TypeSet, ///< TypeSet + PreDeclare, ///< Declare a type (not visible in C++ code) + TypeSet, ///< TypeSet - TypeModifier, ///< const, volatile etc - Keyword, ///< A keyword C/C++ keyword that is not another type + TypeModifier, ///< const, volatile etc + Keyword, ///< A keyword C/C++ keyword that is not another type - Class, ///< class - Struct, ///< struct - Namespace, ///< namespace - Enum, ///< enum + Class, ///< class + Struct, ///< struct + Namespace, ///< namespace + Enum, ///< enum - TypeDef, ///< typedef + TypeDef, ///< typedef - Access, ///< public, protected, private + Access, ///< public, protected, private Reflected, Unreflected, - CallingConvention, ///< Used on a method - Virtual, ///< + CallingConvention, ///< Used on a method + Virtual, ///< Template, - Static, + Static, IntegerModifier, Extern, - CallableMisc, ///< For SLANG_NO_THROW etc + CallableMisc, ///< For SLANG_NO_THROW etc - IntegerType, ///< Built in integer type + IntegerType, ///< Built in integer type - Default, /// default + Default, /// default CountOf, }; @@ -55,8 +56,8 @@ struct IdentifierFlag { enum Enum : IdentifierFlags { - StartScope = 0x1, ///< namespace, struct or class - ClassLike = 0x2, ///< Struct or class + StartScope = 0x1, ///< namespace, struct or class + ClassLike = 0x2, ///< Struct or class Keyword = 0x4, Reflection = 0x8, }; @@ -66,7 +67,6 @@ struct IdentifierFlag class IdentifierLookup { public: - struct Pair { const char* name; @@ -79,14 +79,11 @@ public: return (index >= 0) ? m_styles[index] : IdentifierStyle::None; } - void set(const char* name, IdentifierStyle style) - { - set(UnownedStringSlice(name), style); - } + void set(const char* name, IdentifierStyle style) { set(UnownedStringSlice(name), style); } void set(const UnownedStringSlice& name, IdentifierStyle style); - void set(const char*const* names, size_t namesCount, IdentifierStyle style); + void set(const char* const* names, size_t namesCount, IdentifierStyle style); void set(const Pair* pairs, Index pairsCount); @@ -98,8 +95,8 @@ public: void initDefault(const UnownedStringSlice& markPrefix); - IdentifierLookup() : - m_pool(StringSlicePool::Style::Empty) + IdentifierLookup() + : m_pool(StringSlicePool::Style::Empty) { SLANG_ASSERT(m_pool.getSlicesCount() == 0); } @@ -122,6 +119,6 @@ SLANG_FORCE_INLINE bool hasFlag(IdentifierStyle style, IdentifierFlag::Enum flag return (getFlags(style) & flag) != 0; } -} // CppExtract +} // namespace CppExtract #endif diff --git a/tools/slang-cpp-extractor/macro-writer.cpp b/tools/slang-cpp-extractor/macro-writer.cpp index 285a91851..3f0e0a5fa 100644 --- a/tools/slang-cpp-extractor/macro-writer.cpp +++ b/tools/slang-cpp-extractor/macro-writer.cpp @@ -1,27 +1,27 @@ #include "macro-writer.h" -#include "slang-com-helper.h" - #include "../../source/core/slang-list.h" #include "../../source/core/slang-string.h" -//#include "../../source/core/slang-string-util.h" +#include "slang-com-helper.h" +// #include "../../source/core/slang-string-util.h" +#include "../../source/compiler-core/slang-diagnostic-sink.h" #include "../../source/core/slang-io.h" - #include "../../source/core/slang-writer.h" - -#include "../../source/compiler-core/slang-diagnostic-sink.h" -//#include "../../source/compiler-core/slang-name.h" +// #include "../../source/compiler-core/slang-name.h" #include "diagnostics.h" -#include "options.h" -#include "node-tree.h" #include "file-util.h" +#include "node-tree.h" +#include "options.h" namespace CppExtract { using namespace Slang; -SLANG_FORCE_INLINE static void _indent(Index indentCount, StringBuilder& out) { return FileUtil::indent(indentCount, out); } +SLANG_FORCE_INLINE static void _indent(Index indentCount, StringBuilder& out) +{ + return FileUtil::indent(indentCount, out); +} SlangResult MacroWriter::calcDef(NodeTree* tree, SourceOrigin* origin, StringBuilder& out) { @@ -31,12 +31,14 @@ SlangResult MacroWriter::calcDef(NodeTree* tree, SourceOrigin* origin, StringBui { if (auto classLikeNode = as<ClassLikeNode>(node)) { - if (classLikeNode->m_marker.getContent().indexOf(UnownedStringSlice::fromLiteral("ABSTRACT")) >= 0) + if (classLikeNode->m_marker.getContent().indexOf( + UnownedStringSlice::fromLiteral("ABSTRACT")) >= 0) { out << "ABSTRACT_"; } - out << "SYNTAX_CLASS(" << node->m_name.getContent() << ", " << classLikeNode->m_super.getContent() << ")\n"; + out << "SYNTAX_CLASS(" << node->m_name.getContent() << ", " + << classLikeNode->m_super.getContent() << ")\n"; out << "END_SYNTAX_CLASS()\n\n"; } } @@ -55,23 +57,24 @@ SlangResult MacroWriter::calcChildrenHeader(NodeTree* tree, TypeSet* typeSet, St List<ClassLikeNode*> classNodes; for (Index i = 0; i < baseTypes.getCount(); ++i) { - ClassLikeNode* baseType = baseTypes[i]; + ClassLikeNode* baseType = baseTypes[i]; baseType->calcDerivedDepthFirst(classNodes); } - //Node::filter(Node::isClassLike, nodes); + // Node::filter(Node::isClassLike, nodes); List<ClassLikeNode*> derivedTypes; out << "\n\n /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!! CHILDREN !!!!!!!!!!!!!!!!!!!!!!!!!!!! */ \n\n"; - // Now the children + // Now the children for (ClassLikeNode* classNode : classNodes) { classNode->getReflectedDerivedTypes(derivedTypes); // Define the derived types - out << "#define " << m_options->m_markPrefix << "CHILDREN_" << reflectTypeName << "_" << classNode->m_name.getContent() << "(x, param)"; + out << "#define " << m_options->m_markPrefix << "CHILDREN_" << reflectTypeName << "_" + << classNode->m_name.getContent() << "(x, param)"; if (derivedTypes.getCount()) { @@ -80,12 +83,13 @@ SlangResult MacroWriter::calcChildrenHeader(NodeTree* tree, TypeSet* typeSet, St { Node* derivedType = derivedTypes[j]; _indent(1, out); - out << m_options->m_markPrefix << "ALL_" << reflectTypeName << "_" << derivedType->m_name.getContent() << "(x, param)"; + out << m_options->m_markPrefix << "ALL_" << reflectTypeName << "_" + << derivedType->m_name.getContent() << "(x, param)"; if (j < derivedTypes.getCount() - 1) { out << "\\\n"; } - } + } } out << "\n\n"; } @@ -95,16 +99,19 @@ SlangResult MacroWriter::calcChildrenHeader(NodeTree* tree, TypeSet* typeSet, St for (ClassLikeNode* classNode : classNodes) { // Define the derived types - out << "#define " << m_options->m_markPrefix << "ALL_" << reflectTypeName << "_" << classNode->m_name.getContent() << "(x, param) \\\n"; + out << "#define " << m_options->m_markPrefix << "ALL_" << reflectTypeName << "_" + << classNode->m_name.getContent() << "(x, param) \\\n"; _indent(1, out); - out << m_options->m_markPrefix << reflectTypeName << "_" << classNode->m_name.getContent() << "(x, param)"; + out << m_options->m_markPrefix << reflectTypeName << "_" << classNode->m_name.getContent() + << "(x, param)"; // If has derived types output them if (classNode->hasReflectedDerivedType()) { out << " \\\n"; _indent(1, out); - out << m_options->m_markPrefix << "CHILDREN_" << reflectTypeName << "_" << classNode->m_name.getContent() << "(x, param)"; + out << m_options->m_markPrefix << "CHILDREN_" << reflectTypeName << "_" + << classNode->m_name.getContent() << "(x, param)"; } out << "\n\n"; } @@ -116,7 +123,8 @@ SlangResult MacroWriter::calcChildrenHeader(NodeTree* tree, TypeSet* typeSet, St for (ClassLikeNode* classNode : classNodes) { // Define the derived types - out << "#define " << m_options->m_markPrefix << "FIELDS_" << reflectTypeName << "_" << classNode->m_name.getContent() << "(_x_, _param_)"; + out << "#define " << m_options->m_markPrefix << "FIELDS_" << reflectTypeName << "_" + << classNode->m_name.getContent() << "(_x_, _param_)"; // Find all of the instance fields fields List<FieldNode*> fields; @@ -137,10 +145,10 @@ SlangResult MacroWriter::calcChildrenHeader(NodeTree* tree, TypeSet* typeSet, St const Index fieldsCount = fields.getCount(); bool previousField = false; - for (Index j = 0; j < fieldsCount; ++j) + for (Index j = 0; j < fieldsCount; ++j) { const FieldNode* field = fields[j]; - + if (field->isReflected()) { if (previousField) @@ -150,14 +158,15 @@ SlangResult MacroWriter::calcChildrenHeader(NodeTree* tree, TypeSet* typeSet, St _indent(1, out); - // NOTE! We put the type field in brackets, such that there is no issue with templates containing a comma. - // If stringified - out << "_x_(" << field->m_name.getContent() << ", (" << field->m_fieldType << "), _param_)"; + // NOTE! We put the type field in brackets, such that there is no issue with + // templates containing a comma. If stringified + out << "_x_(" << field->m_name.getContent() << ", (" << field->m_fieldType + << "), _param_)"; previousField = true; } } } - + out << "\n\n"; } } @@ -172,8 +181,9 @@ SlangResult MacroWriter::calcOriginHeader(NodeTree* tree, StringBuilder& out) out << "// Origin macros\n\n"; for (SourceOrigin* origin : tree->getSourceOrigins()) - { - out << "#define " << m_options->m_markPrefix << "ORIGIN_" << origin->m_macroOrigin << "(x, param) \\\n"; + { + out << "#define " << m_options->m_markPrefix << "ORIGIN_" << origin->m_macroOrigin + << "(x, param) \\\n"; for (Node* node : origin->m_nodes) { @@ -279,14 +289,17 @@ SlangResult MacroWriter::calcTypeHeader(NodeTree* tree, TypeSet* typeSet, String out << "// SUPER - is the super class name (or NO_SUPER)\n"; out << "// ORIGIN - where the definition was found\n"; out << "// LAST - is the class name for the last in the range (or NO_LAST)\n"; - out << "// MARKER - is the text inbetween in the prefix/postix (like ABSTRACT). If no inbetween text is is 'NONE'\n"; - out << "// TYPE - Can be BASE, INNER or LEAF for the overall base class, an INNER class, or a LEAF class\n"; + out << "// MARKER - is the text inbetween in the prefix/postix (like ABSTRACT). If no " + "inbetween text is is 'NONE'\n"; + out << "// TYPE - Can be BASE, INNER or LEAF for the overall base class, an INNER class, " + "or a LEAF class\n"; out << "// param is a user defined parameter that can be parsed to the invoked x macro\n\n"; // Output all of the definitions for each type for (ClassLikeNode* node : nodes) { - out << "#define " << m_options->m_markPrefix << reflectTypeName << "_" << node->m_name.getContent() << "(x, param) "; + out << "#define " << m_options->m_markPrefix << reflectTypeName << "_" + << node->m_name.getContent() << "(x, param) "; // Output the X macro part _indent(1, out); @@ -319,9 +332,12 @@ SlangResult MacroWriter::calcTypeHeader(NodeTree* tree, TypeSet* typeSet, String // Output any specifics of the markup UnownedStringSlice marker = node->m_marker.getContent(); // Need to extract the name - if (marker.getLength() > m_options->m_markPrefix.getLength() + m_options->m_markSuffix.getLength()) + if (marker.getLength() > + m_options->m_markPrefix.getLength() + m_options->m_markSuffix.getLength()) { - marker = UnownedStringSlice(marker.begin() + m_options->m_markPrefix.getLength(), marker.end() - m_options->m_markSuffix.getLength()); + marker = UnownedStringSlice( + marker.begin() + m_options->m_markPrefix.getLength(), + marker.end() - m_options->m_markSuffix.getLength()); } else { @@ -417,7 +433,8 @@ SlangResult MacroWriter::writeOutput(NodeTree* tree) StringBuilder headerPath; headerPath << path << "-" << typeSet->m_fileMark << "." << ext; - SLANG_RETURN_ON_FAIL(FileUtil::writeAllText(headerPath, m_sink, header.getUnownedSlice())); + SLANG_RETURN_ON_FAIL( + FileUtil::writeAllText(headerPath, m_sink, header.getUnownedSlice())); } { @@ -426,7 +443,8 @@ SlangResult MacroWriter::writeOutput(NodeTree* tree) StringBuilder headerPath; headerPath << path << "-" << typeSet->m_fileMark << "-macro." + ext; - SLANG_RETURN_ON_FAIL(FileUtil::writeAllText(headerPath, m_sink, childrenHeader.getUnownedSlice())); + SLANG_RETURN_ON_FAIL( + FileUtil::writeAllText(headerPath, m_sink, childrenHeader.getUnownedSlice())); } } @@ -434,4 +452,3 @@ SlangResult MacroWriter::writeOutput(NodeTree* tree) } } // namespace CppExtract - diff --git a/tools/slang-cpp-extractor/macro-writer.h b/tools/slang-cpp-extractor/macro-writer.h index b1754741d..0cbf85fb3 100644 --- a/tools/slang-cpp-extractor/macro-writer.h +++ b/tools/slang-cpp-extractor/macro-writer.h @@ -1,47 +1,43 @@ #ifndef CPP_EXTRACT_MACRO_WRITER_H #define CPP_EXTRACT_MACRO_WRITER_H +#include "../../source/compiler-core/slang-diagnostic-sink.h" #include "diagnostics.h" - -#include "options.h" #include "node-tree.h" +#include "options.h" -#include "../../source/compiler-core/slang-diagnostic-sink.h" - -namespace CppExtract { +namespace CppExtract +{ using namespace Slang; /* A class that writes out macros that define type hierarchies, as well as fields of types */ class MacroWriter { public: - - /// Write output + /// Write output SlangResult writeOutput(NodeTree* tree); - /// Write def files + /// Write def files SlangResult writeDefs(NodeTree* tree); - /// Calculate the header + /// Calculate the header SlangResult calcTypeHeader(NodeTree* tree, TypeSet* typeSet, StringBuilder& out); SlangResult calcChildrenHeader(NodeTree* tree, TypeSet* typeSet, StringBuilder& out); SlangResult calcOriginHeader(NodeTree* tree, StringBuilder& out); SlangResult calcDef(NodeTree* tree, SourceOrigin* origin, StringBuilder& out); - /// Ctor. - MacroWriter(DiagnosticSink* sink, const Options* options): - m_sink(sink), - m_options(options) + /// Ctor. + MacroWriter(DiagnosticSink* sink, const Options* options) + : m_sink(sink), m_options(options) { } protected: - const Options* m_options = nullptr; DiagnosticSink* m_sink; }; -} // CppExtract +} // namespace CppExtract #endif diff --git a/tools/slang-cpp-extractor/node-tree.cpp b/tools/slang-cpp-extractor/node-tree.cpp index 546a54c27..6b543ad31 100644 --- a/tools/slang-cpp-extractor/node-tree.cpp +++ b/tools/slang-cpp-extractor/node-tree.cpp @@ -1,22 +1,24 @@ #include "node-tree.h" -#include "options.h" -#include "identifier-lookup.h" - #include "../../source/compiler-core/slang-name-convention-util.h" - #include "../../source/core/slang-io.h" +#include "identifier-lookup.h" +#include "options.h" -namespace CppExtract { +namespace CppExtract +{ using namespace Slang; /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!! NodeTree !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ -NodeTree::NodeTree(StringSlicePool* typePool, NamePool* namePool, IdentifierLookup* identifierLookup): - m_typePool(typePool), - m_namePool(namePool), - m_identifierLookup(identifierLookup), - m_typeSetPool(StringSlicePool::Style::Empty) +NodeTree::NodeTree( + StringSlicePool* typePool, + NamePool* namePool, + IdentifierLookup* identifierLookup) + : m_typePool(typePool) + , m_namePool(namePool) + , m_identifierLookup(identifierLookup) + , m_typeSetPool(StringSlicePool::Style::Empty) { m_rootNode = new ScopeNode(Node::Kind::Namespace); m_rootNode->m_reflectionType = ReflectionType::Reflected; @@ -52,7 +54,7 @@ TypeSet* NodeTree::getOrAddTypeSet(const UnownedStringSlice& slice) SourceOrigin* NodeTree::addSourceOrigin(SourceFile* sourceFile, const Options& options) { - // Calculate from the path, a 'macro origin' name. + // Calculate from the path, a 'macro origin' name. const String macroOrigin = calcMacroOrigin(sourceFile->getPathInfo().foundPath, options); SourceOrigin* origin = new SourceOrigin(sourceFile, macroOrigin); @@ -60,7 +62,7 @@ SourceOrigin* NodeTree::addSourceOrigin(SourceFile* sourceFile, const Options& o return origin; } -/* static */String NodeTree::calcMacroOrigin(const String& filePath, const Options& options) +/* static */ String NodeTree::calcMacroOrigin(const String& filePath, const Options& options) { // Get the filename without extension String fileName = Path::getFileNameWithoutExt(filePath); @@ -69,7 +71,8 @@ SourceOrigin* NodeTree::addSourceOrigin(SourceFile* sourceFile, const Options& o UnownedStringSlice slice = fileName.getUnownedSlice(); // Filename prefix - if (options.m_stripFilePrefix.getLength() && slice.startsWith(options.m_stripFilePrefix.getUnownedSlice())) + if (options.m_stripFilePrefix.getLength() && + slice.startsWith(options.m_stripFilePrefix.getUnownedSlice())) { const Index len = options.m_stripFilePrefix.getLength(); slice = UnownedStringSlice(slice.begin() + len, slice.end()); @@ -94,7 +97,9 @@ SlangResult NodeTree::_calcDerivedTypesRec(ScopeNode* inScopeNode, DiagnosticSin ScopeNode* parentScope = classLikeNode->m_parentScope; if (parentScope == nullptr) { - sink->diagnoseRaw(Severity::Error, UnownedStringSlice::fromLiteral("Can't lookup in scope if there is none!")); + sink->diagnoseRaw( + Severity::Error, + UnownedStringSlice::fromLiteral("Can't lookup in scope if there is none!")); return SLANG_FAIL; } @@ -104,7 +109,10 @@ SlangResult NodeTree::_calcDerivedTypesRec(ScopeNode* inScopeNode, DiagnosticSin { if (classLikeNode->isReflected()) { - sink->diagnose(classLikeNode->m_name, CPPDiagnostics::superTypeNotFound, classLikeNode->getAbsoluteName()); + sink->diagnose( + classLikeNode->m_name, + CPPDiagnostics::superTypeNotFound, + classLikeNode->getAbsoluteName()); return SLANG_FAIL; } } @@ -114,17 +122,26 @@ SlangResult NodeTree::_calcDerivedTypesRec(ScopeNode* inScopeNode, DiagnosticSin if (!superType) { - sink->diagnose(classLikeNode->m_name, CPPDiagnostics::superTypeNotAType, classLikeNode->getAbsoluteName()); + sink->diagnose( + classLikeNode->m_name, + CPPDiagnostics::superTypeNotAType, + classLikeNode->getAbsoluteName()); return SLANG_FAIL; } if (superType->m_typeSet != classLikeNode->m_typeSet) { - sink->diagnose(classLikeNode->m_name, CPPDiagnostics::typeInDifferentTypeSet, classLikeNode->m_name.getContent(), classLikeNode->m_typeSet->m_macroName, superType->m_typeSet->m_macroName); + sink->diagnose( + classLikeNode->m_name, + CPPDiagnostics::typeInDifferentTypeSet, + classLikeNode->m_name.getContent(), + classLikeNode->m_typeSet->m_macroName, + superType->m_typeSet->m_macroName); return SLANG_FAIL; } - // The base class must be defined in same scope (as we didn't allow different scopes for base classes) + // The base class must be defined in same scope (as we didn't allow different scopes + // for base classes) superType->addDerived(classLikeNode); } } diff --git a/tools/slang-cpp-extractor/node-tree.h b/tools/slang-cpp-extractor/node-tree.h index b54321b09..f1547dbc1 100644 --- a/tools/slang-cpp-extractor/node-tree.h +++ b/tools/slang-cpp-extractor/node-tree.h @@ -1,31 +1,31 @@ #ifndef CPP_EXTRACT_NODE_TREE_H #define CPP_EXTRACT_NODE_TREE_H +#include "../../source/compiler-core/slang-lexer.h" #include "diagnostics.h" -#include "node.h" #include "identifier-lookup.h" +#include "node.h" -#include "../../source/compiler-core/slang-lexer.h" - -namespace CppExtract { +namespace CppExtract +{ using namespace Slang; class TypeSet : public RefObject { public: /// This is the looked up name. - UnownedStringSlice m_macroName; ///< The name extracted from the macro SLANG_ABSTRACT_AST_CLASS -> AST + UnownedStringSlice + m_macroName; ///< The name extracted from the macro SLANG_ABSTRACT_AST_CLASS -> AST - String m_typeName; ///< The enum type name associated with this type for AST it is ASTNode - String m_fileMark; ///< This 'mark' becomes of the output filename + String m_typeName; ///< The enum type name associated with this type for AST it is ASTNode + String m_fileMark; ///< This 'mark' becomes of the output filename - List<ClassLikeNode*> m_baseTypes; ///< The base types for this type set + List<ClassLikeNode*> m_baseTypes; ///< The base types for this type set }; class SourceOrigin : public RefObject { public: - void addNode(Node* node) { if (auto classLike = as<ClassLikeNode>(node)) @@ -37,17 +37,18 @@ public: m_nodes.add(node); } - SourceOrigin(SourceFile* sourceFile, const String& macroOrigin) : - m_sourceFile(sourceFile), - m_macroOrigin(macroOrigin) - {} + SourceOrigin(SourceFile* sourceFile, const String& macroOrigin) + : m_sourceFile(sourceFile), m_macroOrigin(macroOrigin) + { + } - String m_macroOrigin; ///< The macro text is inserted into the macro to identify the origin. It is based on the filename - SourceFile* m_sourceFile; ///< The source file - also holds the path information + String m_macroOrigin; ///< The macro text is inserted into the macro to identify the origin. It + ///< is based on the filename + SourceFile* m_sourceFile; ///< The source file - also holds the path information /// All of the nodes defined in this file in the order they were defined /// Note that the same namespace may be listed multiple times. - List<RefPtr<Node> > m_nodes; + List<RefPtr<Node>> m_nodes; }; struct Options; @@ -59,22 +60,23 @@ class NodeTree { public: friend class Parser; - /// Get all of the parsed source origins - const List<RefPtr<SourceOrigin> >& getSourceOrigins() const { return m_sourceOrigins; } + /// Get all of the parsed source origins + const List<RefPtr<SourceOrigin>>& getSourceOrigins() const { return m_sourceOrigins; } TypeSet* getTypeSet(const UnownedStringSlice& slice); TypeSet* getOrAddTypeSet(const UnownedStringSlice& slice); SourceOrigin* addSourceOrigin(SourceFile* sourceFile, const Options& options); - /// Get all of the type sets + /// Get all of the type sets const List<RefPtr<TypeSet>>& getTypeSets() const { return m_typeSets; } - /// Get the root node + /// Get the root node Node* getRootNode() const { return m_rootNode; } - /// When parsing we don't lookup all up super types/add derived types. This is because - /// we allow files to be processed in any order, so we have to do the type lookup as a separate operation + /// When parsing we don't lookup all up super types/add derived types. This is because + /// we allow files to be processed in any order, so we have to do the type lookup as a separate + /// operation SlangResult calcDerivedTypes(DiagnosticSink* sink); NodeTree(StringSlicePool* typePool, NamePool* namePool, IdentifierLookup* identifierLookup); @@ -84,19 +86,19 @@ public: protected: SlangResult _calcDerivedTypesRec(ScopeNode* node, DiagnosticSink* sink); - StringSlicePool m_typeSetPool; ///< Pool for type set names - List<RefPtr<TypeSet> > m_typeSets; ///< The type sets + StringSlicePool m_typeSetPool; ///< Pool for type set names + List<RefPtr<TypeSet>> m_typeSets; ///< The type sets IdentifierLookup* m_identifierLookup; - StringSlicePool* m_typePool; ///< Pool for just types + StringSlicePool* m_typePool; ///< Pool for just types NamePool* m_namePool; - RefPtr<ScopeNode> m_rootNode; ///< The root scope + RefPtr<ScopeNode> m_rootNode; ///< The root scope List<RefPtr<SourceOrigin>> m_sourceOrigins; }; -} // CppExtract +} // namespace CppExtract #endif diff --git a/tools/slang-cpp-extractor/node.cpp b/tools/slang-cpp-extractor/node.cpp index 1189e3815..4598e34c9 100644 --- a/tools/slang-cpp-extractor/node.cpp +++ b/tools/slang-cpp-extractor/node.cpp @@ -1,15 +1,19 @@ #include "node.h" -#include "file-util.h" - -#include "../../source/core/slang-string-util.h" #include "../../source/core/slang-string-escape-util.h" +#include "../../source/core/slang-string-util.h" +#include "file-util.h" -namespace CppExtract { +namespace CppExtract +{ -// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Node Impl !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Node Impl +// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -SLANG_FORCE_INLINE static void _indent(Index indentCount, StringBuilder& out) { FileUtil::indent(indentCount, out); } +SLANG_FORCE_INLINE static void _indent(Index indentCount, StringBuilder& out) +{ + FileUtil::indent(indentCount, out); +} void Node::dumpMarkup(int indentCount, StringBuilder& out) { @@ -93,7 +97,7 @@ void Node::calcAbsoluteName(StringBuilder& outName) const } } -/* static */void Node::calcScopePath(Node* node, List<Node*>& outPath) +/* static */ void Node::calcScopePath(Node* node, List<Node*>& outPath) { outPath.clear(); @@ -107,11 +111,11 @@ void Node::calcAbsoluteName(StringBuilder& outName) const outPath.reverse(); } -/* static */void Node::filterImpl(Filter inFilter, List<Node*>& ioNodes) +/* static */ void Node::filterImpl(Filter inFilter, List<Node*>& ioNodes) { // Filter out all the unreflected nodes Index count = ioNodes.getCount(); - for (Index j = 0; j < count; ) + for (Index j = 0; j < count;) { Node* node = ioNodes[j]; @@ -127,7 +131,7 @@ void Node::calcAbsoluteName(StringBuilder& outName) const } } -/* static */Node* Node::lookupNameInScope(ScopeNode* scope, const UnownedStringSlice& name) +/* static */ Node* Node::lookupNameInScope(ScopeNode* scope, const UnownedStringSlice& name) { // TODO(JS): Doesn't handle 'using namespace'. @@ -167,7 +171,10 @@ void Node::calcAbsoluteName(StringBuilder& outName) const return nullptr; } -/* static */Node* Node::lookupFromScope(ScopeNode* scope, const UnownedStringSlice* parts, Index partsCount) +/* static */ Node* Node::lookupFromScope( + ScopeNode* scope, + const UnownedStringSlice* parts, + Index partsCount) { SLANG_ASSERT(partsCount > 0); if (partsCount == 1) @@ -202,7 +209,9 @@ void Node::calcAbsoluteName(StringBuilder& outName) const return nullptr; } -/* static */void Node::splitPath(const UnownedStringSlice& inPath, List<UnownedStringSlice>& outParts) +/* static */ void Node::splitPath( + const UnownedStringSlice& inPath, + List<UnownedStringSlice>& outParts) { if (inPath.indexOf(UnownedStringSlice::fromLiteral("::")) >= 0) { @@ -220,7 +229,7 @@ void Node::calcAbsoluteName(StringBuilder& outName) const } } -/* static */Node* Node::lookupFromScope(ScopeNode* scope, const UnownedStringSlice& inPath) +/* static */ Node* Node::lookupFromScope(ScopeNode* scope, const UnownedStringSlice& inPath) { if (inPath.indexOf(UnownedStringSlice::fromLiteral("::")) >= 0) { @@ -235,7 +244,7 @@ void Node::calcAbsoluteName(StringBuilder& outName) const } } -/* static */Node* Node::lookup(ScopeNode* scope, const UnownedStringSlice& inPath) +/* static */ Node* Node::lookup(ScopeNode* scope, const UnownedStringSlice& inPath) { if (inPath.indexOf(UnownedStringSlice::fromLiteral("::")) >= 0) { @@ -341,11 +350,11 @@ void ScopeNode::dump(int indentCount, StringBuilder& out) switch (m_kind) { - case Kind::AnonymousNamespace: + case Kind::AnonymousNamespace: { out << "namespace {\n"; } - case Kind::Namespace: + case Kind::Namespace: { if (m_name.hasContent()) { @@ -379,7 +388,7 @@ static bool _needsSpace(const Token& prevTok, const Token& tok) auto loc = tok.getLoc(); auto prevContent = prevTok.getContent(); - + if (prevLoc + prevContent.getLength() == loc) { return false; @@ -477,7 +486,7 @@ void EnumNode::dump(int indent, StringBuilder& out) } if (m_backingTokens.getCount() > 0) - { + { out << " : "; _dumpTokens(m_backingTokens, out); } diff --git a/tools/slang-cpp-extractor/node.h b/tools/slang-cpp-extractor/node.h index 8455588ad..3590aa5ce 100644 --- a/tools/slang-cpp-extractor/node.h +++ b/tools/slang-cpp-extractor/node.h @@ -1,11 +1,11 @@ #ifndef CPP_EXTRACT_NODE_H #define CPP_EXTRACT_NODE_H -#include "diagnostics.h" - #include "../../source/compiler-core/slang-doc-extractor.h" +#include "diagnostics.h" -namespace CppExtract { +namespace CppExtract +{ using namespace Slang; enum class ReflectionType : uint8_t @@ -30,7 +30,7 @@ public: StructType, ClassType, - Enum, + Enum, EnumClass, Namespace, @@ -41,10 +41,10 @@ public: TypeDef, - Callable, ///< Functions/methods + Callable, ///< Functions/methods - Other, ///< Used 'other' parsing like for TYPE - Unknown, ///< Used for marking tokens consumed but usage is not known + Other, ///< Used 'other' parsing like for TYPE + Unknown, ///< Used for marking tokens consumed but usage is not known CountOf, }; @@ -67,35 +67,57 @@ public: EnumEnd = int(Kind::EnumClass), }; - /// Returns true if kind can cast to this type - /// Used for implementing as<T> casting - static bool isOfKind(Kind kind) { SLANG_UNUSED(kind); return true; } + /// Returns true if kind can cast to this type + /// Used for implementing as<T> casting + static bool isOfKind(Kind kind) + { + SLANG_UNUSED(kind); + return true; + } - static bool isKindScope(Kind kind) { return int(kind) >= int(KindRange::ScopeStart) && int(kind) <= int(KindRange::ScopeEnd); } - static bool isKindClassLike(Kind kind) { return int(kind) >= int(KindRange::ClassLikeStart) && int(kind) <= int(KindRange::ClassLikeEnd); } - static bool isKindEnumLike(Kind kind) { return int(kind) >= int(KindRange::EnumStart) && int(kind) <= int(KindRange::EnumEnd); } + static bool isKindScope(Kind kind) + { + return int(kind) >= int(KindRange::ScopeStart) && int(kind) <= int(KindRange::ScopeEnd); + } + static bool isKindClassLike(Kind kind) + { + return int(kind) >= int(KindRange::ClassLikeStart) && + int(kind) <= int(KindRange::ClassLikeEnd); + } + static bool isKindEnumLike(Kind kind) + { + return int(kind) >= int(KindRange::EnumStart) && int(kind) <= int(KindRange::EnumEnd); + } - /// It a type, but doesn't have a scope - static bool isKindOtherType(Kind kind) { return int(kind) >= int(KindRange::OtherTypeStart) && int(kind) <= int(KindRange::OtherTypeEnd); } - /// Is a type and has a scope - static bool isKindScopeType(Kind kind) { return int(kind) >= int(KindRange::ScopeTypeStart) && int(kind) <= int(KindRange::ScopeTypeEnd); } + /// It a type, but doesn't have a scope + static bool isKindOtherType(Kind kind) + { + return int(kind) >= int(KindRange::OtherTypeStart) && + int(kind) <= int(KindRange::OtherTypeEnd); + } + /// Is a type and has a scope + static bool isKindScopeType(Kind kind) + { + return int(kind) >= int(KindRange::ScopeTypeStart) && + int(kind) <= int(KindRange::ScopeTypeEnd); + } - /// True if the kind is any type + /// True if the kind is any type static bool isKindType(Kind kind) { return isKindOtherType(kind) || isKindScopeType(kind); } - /// True if the kind can accept contained types + /// True if the kind can accept contained types static bool canKindContainTypes(Kind type) { switch (type) { - case Kind::StructType: - case Kind::ClassType: - case Kind::Namespace: - case Kind::AnonymousNamespace: + case Kind::StructType: + case Kind::ClassType: + case Kind::Namespace: + case Kind::AnonymousNamespace: { return true; } - default: break; + default: break; } return false; } @@ -105,73 +127,86 @@ public: bool isScope() const { return isKindScope(m_kind); } bool isEnumLike() const { return isKindEnumLike(m_kind); } - /// These are useful for the filter - static bool isClassLikeAndReflected(Node* node) { return node->isClassLike() && node->isReflected(); } + /// These are useful for the filter + static bool isClassLikeAndReflected(Node* node) + { + return node->isClassLike() && node->isReflected(); + } static bool isClassLike(Node* node) { return isKindClassLike(node->m_kind); } virtual void dump(int indent, StringBuilder& out) = 0; - /// Do depth first traversal of nodes in scopes + /// Do depth first traversal of nodes in scopes virtual void calcScopeDepthFirst(List<Node*>& outNodes); - /// Calculate the absolute name for this namespace/type + /// Calculate the absolute name for this namespace/type void calcAbsoluteName(StringBuilder& outName) const; - /// Get the absolute name - String getAbsoluteName() const { StringBuilder buf; calcAbsoluteName(buf); return buf.produceString(); } + /// Get the absolute name + String getAbsoluteName() const + { + StringBuilder buf; + calcAbsoluteName(buf); + return buf.produceString(); + } - /// Calculate the scope path to this node, from the root + /// Calculate the scope path to this node, from the root void calcScopePath(List<Node*>& outPath) { calcScopePath(this, outPath); } - /// True if reflected + /// True if reflected bool isReflected() const { return m_reflectionType == ReflectionType::Reflected; } SourceLoc getSourceLoc() const { return m_name.getLoc(); } ScopeNode* getRootScope(); - typedef bool(*Filter)(Node* node); - - template <typename T> - static void filter(Filter filter, List<T*>& io) { const Node* _isNodeDerived = (T*)nullptr; SLANG_UNUSED(_isNodeDerived); filterImpl(filter, reinterpret_cast<List<Node*>&>(io)); } + typedef bool (*Filter)(Node* node); + + template<typename T> + static void filter(Filter filter, List<T*>& io) + { + const Node* _isNodeDerived = (T*)nullptr; + SLANG_UNUSED(_isNodeDerived); + filterImpl(filter, reinterpret_cast<List<Node*>&>(io)); + } static void filterImpl(Filter filter, List<Node*>& io); static void calcScopePath(Node* node, List<Node*>& outPath); - /// Lookup a name in just the specified scope - /// Handles anonymous namespaces, or name lookups that are in the parents space + /// Lookup a name in just the specified scope + /// Handles anonymous namespaces, or name lookups that are in the parents space static Node* lookupNameInScope(ScopeNode* scope, const UnownedStringSlice& name); - /// Lookup from a path + /// Lookup from a path static Node* lookupFromScope(ScopeNode* scope, const UnownedStringSlice* path, Index pathCount); - /// Looks up *just* from the specified scope. + /// Looks up *just* from the specified scope. static Node* lookupFromScope(ScopeNode* scope, const UnownedStringSlice& slice); - /// Look up name (which can contain ::) + /// Look up name (which can contain ::) static Node* lookup(ScopeNode* scope, const UnownedStringSlice& name); static void splitPath(const UnownedStringSlice& slice, List<UnownedStringSlice>& outSplitPath); - /// If markup is specified dump it + /// If markup is specified dump it void dumpMarkup(int indent, StringBuilder& out); - Node(Kind type) : - m_kind(type), - m_parentScope(nullptr), - m_reflectionType(ReflectionType::NotReflected) + Node(Kind type) + : m_kind(type), m_parentScope(nullptr), m_reflectionType(ReflectionType::NotReflected) { } - Kind m_kind; ///< The kind of node this is - ReflectionType m_reflectionType; ///< Classes can be traversed, but not reflected. To be reflected they have to contain the marker - - MarkupVisibility m_markupVisibility = MarkupVisibility::Public; ///< The visibility of the markup - String m_markup; ///< Documentation associated with this node + Kind m_kind; ///< The kind of node this is + ReflectionType m_reflectionType; ///< Classes can be traversed, but not reflected. To be + ///< reflected they have to contain the marker + + MarkupVisibility m_markupVisibility = + MarkupVisibility::Public; ///< The visibility of the markup + String m_markup; ///< Documentation associated with this node - Token m_name; ///< The name of this scope/type + Token m_name; ///< The name of this scope/type - ScopeNode* m_parentScope; ///< The scope this type/scope is defined in + ScopeNode* m_parentScope; ///< The scope this type/scope is defined in }; struct ScopeNode : public Node @@ -183,33 +218,37 @@ struct ScopeNode : public Node virtual void dump(int indent, StringBuilder& out) SLANG_OVERRIDE; virtual void calcScopeDepthFirst(List<Node*>& outNodes) SLANG_OVERRIDE; - /// True if can contain callable entries + /// True if can contain callable entries bool canContainCallable() const { return isClassLike() || isNamespace(); } - /// True if can accept fields (class like types can) + /// True if can accept fields (class like types can) bool canContainFields() const { return isClassLike(); } - /// True if the scope can accept types + /// True if the scope can accept types bool canContainTypes() const { return canKindContainTypes(m_kind); } - /// Gets the reflection for any contained types - ReflectionType getContainedReflectionType() const { return m_reflectionType == ReflectionType::NotReflected ? ReflectionType::NotReflected : m_reflectionOverride; } + /// Gets the reflection for any contained types + ReflectionType getContainedReflectionType() const + { + return m_reflectionType == ReflectionType::NotReflected ? ReflectionType::NotReflected + : m_reflectionOverride; + } - /// Add a child node to this nodes scope + /// Add a child node to this nodes scope void addChild(Node* child); - /// Adds the child but does not add the name to the map + /// Adds the child but does not add the name to the map void addChildIgnoringName(Node* child); - /// Find a child node in this scope with the specified name. Return nullptr if not found + /// Find a child node in this scope with the specified name. Return nullptr if not found Node* findChild(const UnownedStringSlice& name) const; - /// Gets the anonymous namespace associated with this scope + /// Gets the anonymous namespace associated with this scope ScopeNode* getAnonymousNamespace(); - ScopeNode(Kind kind) : - Super(kind), - m_reflectionOverride(ReflectionType::Reflected), - m_anonymousNamespace(nullptr) + ScopeNode(Kind kind) + : Super(kind) + , m_reflectionOverride(ReflectionType::Reflected) + , m_anonymousNamespace(nullptr) { } @@ -234,8 +273,8 @@ struct FieldNode : public Node virtual void dump(int indent, StringBuilder& out) SLANG_OVERRIDE; - FieldNode() : - Super(Kind::Field) + FieldNode() + : Super(Kind::Field) { } @@ -252,49 +291,48 @@ struct ClassLikeNode : public ScopeNode static bool isOfKind(Kind kind) { return isKindClassLike(kind); } - /// Add a node that is derived from this + /// Add a node that is derived from this void addDerived(ClassLikeNode* derived); - /// Dump all of the derived types + /// Dump all of the derived types void dumpDerived(int indentCount, StringBuilder& out); - /// Calculates the derived depth + /// Calculates the derived depth Index calcDerivedDepth() const; - /// Find the last (reflected) derived type + /// Find the last (reflected) derived type ClassLikeNode* findLastDerived(); - /// Traverse the hierarchy of derived nodes, in depth first order + /// Traverse the hierarchy of derived nodes, in depth first order void calcDerivedDepthFirst(List<ClassLikeNode*>& outNodes); - /// True if has a derived type that is reflected + /// True if has a derived type that is reflected bool hasReflectedDerivedType() const; - /// Stores in out any reflected derived types + /// Stores in out any reflected derived types void getReflectedDerivedTypes(List<ClassLikeNode*>& out) const; // Node Impl virtual void dump(int indent, StringBuilder& out) SLANG_OVERRIDE; - ClassLikeNode(Kind kind) : - Super(kind), - m_origin(nullptr), - m_typeSet(nullptr), - m_superNode(nullptr) + ClassLikeNode(Kind kind) + : Super(kind), m_origin(nullptr), m_typeSet(nullptr), m_superNode(nullptr) { SLANG_ASSERT(kind == Kind::ClassType || kind == Kind::StructType); } - SourceOrigin* m_origin; ///< Defines where this was uniquely defined. + SourceOrigin* m_origin; ///< Defines where this was uniquely defined. - Token m_marker; ///< The marker associated with this scope (typically the marker is SLANG_CLASS etc, that is used to identify reflectedType) + Token m_marker; ///< The marker associated with this scope (typically the marker is SLANG_CLASS + ///< etc, that is used to identify reflectedType) - List<RefPtr<ClassLikeNode>> m_derivedTypes; ///< All of the types derived from this type + List<RefPtr<ClassLikeNode>> m_derivedTypes; ///< All of the types derived from this type - TypeSet* m_typeSet; ///< The typeset this type belongs to. + TypeSet* m_typeSet; ///< The typeset this type belongs to. - Token m_super; ///< Super class name - ClassLikeNode* m_superNode; ///< If this is a class/struct, the type it is derived from (or nullptr if base) + Token m_super; ///< Super class name + ClassLikeNode* m_superNode; ///< If this is a class/struct, the type it is derived from (or + ///< nullptr if base) }; struct CallableNode : public Node @@ -305,7 +343,10 @@ struct CallableNode : public Node virtual void dump(int indent, StringBuilder& out) SLANG_OVERRIDE; - CallableNode() :Super(Kind::Callable) {} + CallableNode() + : Super(Kind::Callable) + { + } struct Param { @@ -332,8 +373,8 @@ struct EnumCaseNode : public Node virtual void dump(int indent, StringBuilder& out) SLANG_OVERRIDE; - EnumCaseNode(): - Super(Kind::EnumCase) + EnumCaseNode() + : Super(Kind::EnumCase) { } @@ -348,8 +389,8 @@ struct EnumNode : public ScopeNode virtual void dump(int indent, StringBuilder& out) SLANG_OVERRIDE; - EnumNode(Kind kind): - Super(kind) + EnumNode(Kind kind) + : Super(kind) { SLANG_ASSERT(isKindEnumLike(kind)); } @@ -364,17 +405,20 @@ struct TypeDefNode : public Node virtual void dump(int indent, StringBuilder& out) SLANG_OVERRIDE; - TypeDefNode(): - Super(Kind::TypeDef) + TypeDefNode() + : Super(Kind::TypeDef) { } List<Token> m_targetTypeTokens; }; -template <typename T> -T* as(Node* node) { return (node && T::isOfKind(node->m_kind)) ? static_cast<T*>(node) : nullptr; } +template<typename T> +T* as(Node* node) +{ + return (node && T::isOfKind(node->m_kind)) ? static_cast<T*>(node) : nullptr; +} -} // CppExtract +} // namespace CppExtract #endif diff --git a/tools/slang-cpp-extractor/options.cpp b/tools/slang-cpp-extractor/options.cpp index 90639da38..17ff5eebc 100644 --- a/tools/slang-cpp-extractor/options.cpp +++ b/tools/slang-cpp-extractor/options.cpp @@ -2,7 +2,8 @@ #include "diagnostics.h" -namespace CppExtract { +namespace CppExtract +{ SlangResult OptionsParser::_parseArgFlag(const char* option, bool& outFlag) { @@ -52,7 +53,11 @@ SlangResult OptionsParser::_parseArgReplaceValue(const char* option, String& ioV return SLANG_OK; } -SlangResult OptionsParser::parse(int argc, const char*const* argv, DiagnosticSink* sink, Options& outOptions) +SlangResult OptionsParser::parse( + int argc, + const char* const* argv, + DiagnosticSink* sink, + Options& outOptions) { outOptions.reset(); @@ -86,12 +91,14 @@ SlangResult OptionsParser::parse(int argc, const char*const* argv, DiagnosticSin } else if (arg == "-mark-prefix") { - SLANG_RETURN_ON_FAIL(_parseArgReplaceValue("-mark-prefix", outOptions.m_markPrefix)); + SLANG_RETURN_ON_FAIL( + _parseArgReplaceValue("-mark-prefix", outOptions.m_markPrefix)); continue; } else if (arg == "-mark-suffix") { - SLANG_RETURN_ON_FAIL(_parseArgReplaceValue("-mark-suffix", outOptions.m_markSuffix)); + SLANG_RETURN_ON_FAIL( + _parseArgReplaceValue("-mark-suffix", outOptions.m_markSuffix)); continue; } else if (arg == "-defs") @@ -106,7 +113,8 @@ SlangResult OptionsParser::parse(int argc, const char*const* argv, DiagnosticSin } else if (arg == "-strip-prefix") { - SLANG_RETURN_ON_FAIL(_parseArgWithValue("-strip-prefix", outOptions.m_stripFilePrefix)); + SLANG_RETURN_ON_FAIL( + _parseArgWithValue("-strip-prefix", outOptions.m_stripFilePrefix)); continue; } else if (arg == "-unit-test") diff --git a/tools/slang-cpp-extractor/options.h b/tools/slang-cpp-extractor/options.h index e660bc376..8231d5e3b 100644 --- a/tools/slang-cpp-extractor/options.h +++ b/tools/slang-cpp-extractor/options.h @@ -3,7 +3,8 @@ #include "../../source/slang/slang-diagnostics.h" -namespace CppExtract { +namespace CppExtract +{ using namespace Slang; @@ -11,10 +12,7 @@ using namespace Slang; struct Options { - void reset() - { - *this = Options(); - } + void reset() { *this = Options(); } Options() { @@ -22,28 +20,34 @@ struct Options m_markSuffix = "_CLASS"; } - bool m_defs = false; ///< If set will output a '-defs.h' file for each of the input files, that corresponds to previous defs files (although doesn't have fields/RAW) - bool m_dump = false; ///< If true will dump to stderr the types/fields and hierarchy it extracted - bool m_runUnitTests = false; ///< If true will run internal unit tests - bool m_extractDoc = true; ///< If set will try to extract documentation associated with nodes + bool m_defs = false; ///< If set will output a '-defs.h' file for each of the input files, that + ///< corresponds to previous defs files (although doesn't have fields/RAW) + bool m_dump = + false; ///< If true will dump to stderr the types/fields and hierarchy it extracted + bool m_runUnitTests = false; ///< If true will run internal unit tests + bool m_extractDoc = true; ///< If set will try to extract documentation associated with nodes - bool m_outputFields = false; ///< When dumping macros also dump field definitions + bool m_outputFields = false; ///< When dumping macros also dump field definitions bool m_requireMark = true; - List<String> m_inputPaths; ///< The input paths to the files to be processed + List<String> m_inputPaths; ///< The input paths to the files to be processed - String m_outputPath; ///< The output path. Note that the extractor can generate multiple output files, and this will actually be the 'stem' of several files + String m_outputPath; ///< The output path. Note that the extractor can generate multiple output + ///< files, and this will actually be the 'stem' of several files - String m_inputDirectory; ///< The input directory that is by default used for reading m_inputPaths from. - String m_markPrefix; ///< The prefix of the 'marker' used to identify a reflected type - String m_markSuffix; ///< The postfix of the 'marker' used to identify a reflected type - String m_stripFilePrefix; ///< Used for the 'origin' information, this is stripped from the source filename, and the remainder of the filename (without extension) is 'macroized' + String m_inputDirectory; ///< The input directory that is by default used for reading + ///< m_inputPaths from. + String m_markPrefix; ///< The prefix of the 'marker' used to identify a reflected type + String m_markSuffix; ///< The postfix of the 'marker' used to identify a reflected type + String m_stripFilePrefix; ///< Used for the 'origin' information, this is stripped from the + ///< source filename, and the remainder of the filename (without + ///< extension) is 'macroized' }; struct OptionsParser { /// Parse the parameters. NOTE! Must have the program path removed - SlangResult parse(int argc, const char*const* argv, DiagnosticSink* sink, Options& outOptions); + SlangResult parse(int argc, const char* const* argv, DiagnosticSink* sink, Options& outOptions); SlangResult _parseArgWithValue(const char* option, String& outValue); SlangResult _parseArgReplaceValue(const char* option, String& outValue); @@ -53,11 +57,11 @@ struct OptionsParser Index m_index; Int m_argCount; - const char*const* m_args; + const char* const* m_args; DiagnosticSink* m_sink; }; -} // CppExtract +} // namespace CppExtract #endif diff --git a/tools/slang-cpp-extractor/parser.cpp b/tools/slang-cpp-extractor/parser.cpp index 7ceb91824..a76965508 100644 --- a/tools/slang-cpp-extractor/parser.cpp +++ b/tools/slang-cpp-extractor/parser.cpp @@ -1,14 +1,13 @@ #include "parser.h" -#include "options.h" -#include "identifier-lookup.h" - #include "../../source/compiler-core/slang-name-convention-util.h" - -#include "../../source/core/slang-string-util.h" #include "../../source/core/slang-io.h" +#include "../../source/core/slang-string-util.h" +#include "identifier-lookup.h" +#include "options.h" -namespace CppExtract { +namespace CppExtract +{ using namespace Slang; // If fails then we need more bits to identify types @@ -16,14 +15,11 @@ SLANG_COMPILE_TIME_ASSERT(int(Node::Kind::CountOf) <= 8 * sizeof(uint32_t)); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Parser !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -Parser::Parser(NodeTree* nodeTree, DiagnosticSink* sink) : - m_sink(sink), - m_nodeTree(nodeTree), - m_nodeTypeEnabled(0) +Parser::Parser(NodeTree* nodeTree, DiagnosticSink* sink) + : m_sink(sink), m_nodeTree(nodeTree), m_nodeTypeEnabled(0) { // Enable types by default - const Node::Kind defaultEnabled[] = - { + const Node::Kind defaultEnabled[] = { Node::Kind::ClassType, Node::Kind::StructType, Node::Kind::Namespace, @@ -62,7 +58,8 @@ void Parser::setKindsEnabled(const Node::Kind* kinds, Index kindsCount, bool isE bool Parser::_isMarker(const UnownedStringSlice& name) { - return name.startsWith(m_options->m_markPrefix.getUnownedSlice()) && name.endsWith(m_options->m_markSuffix.getUnownedSlice()); + return name.startsWith(m_options->m_markPrefix.getUnownedSlice()) && + name.endsWith(m_options->m_markSuffix.getUnownedSlice()); } SlangResult Parser::expect(TokenType type, Token* outToken) @@ -117,7 +114,8 @@ bool Parser::advanceIfStyle(IdentifierStyle style, Token* outToken) { if (m_reader.peekTokenType() == TokenType::Identifier) { - IdentifierStyle readStyle = m_nodeTree->m_identifierLookup->get(m_reader.peekToken().getContent()); + IdentifierStyle readStyle = + m_nodeTree->m_identifierLookup->get(m_reader.peekToken().getContent()); if (readStyle == style) { Token token = m_reader.advanceToken(); @@ -152,7 +150,7 @@ SlangResult Parser::pushScope(ScopeNode* scopeNode) // We can only have one 'special' scope. SLANG_ASSERT(scopeNode || m_scopeStack.getLast()); - // We keep to track. + // We keep to track. m_scopeStack.add(scopeNode); // If we pass nullptr, we don't update the current scope. @@ -168,15 +166,21 @@ SlangResult Parser::pushScope(ScopeNode* scopeNode) if (scopeNode->m_name.hasContent()) { - // For anonymous namespace, we should look if we already have one and just reopen that. Doing so will mean will - // find anonymous namespace clashes + // For anonymous namespace, we should look if we already have one and just reopen that. + // Doing so will mean will find anonymous namespace clashes if (Node* foundNode = m_currentScope->findChild(scopeNode->m_name.getContent())) { if (scopeNode->isClassLike()) { - m_sink->diagnose(m_reader.peekToken(), CPPDiagnostics::typeAlreadyDeclared, scopeNode->m_name.getContent()); - m_sink->diagnose(foundNode->m_name, CPPDiagnostics::seeDeclarationOf, scopeNode->m_name.getContent()); + m_sink->diagnose( + m_reader.peekToken(), + CPPDiagnostics::typeAlreadyDeclared, + scopeNode->m_name.getContent()); + m_sink->diagnose( + foundNode->m_name, + CPPDiagnostics::seeDeclarationOf, + scopeNode->m_name.getContent()); return SLANG_FAIL; } @@ -185,15 +189,18 @@ SlangResult Parser::pushScope(ScopeNode* scopeNode) if (foundNode->m_kind != scopeNode->m_kind) { // Different types can't work - m_sink->diagnose(m_reader.peekToken(), CPPDiagnostics::typeAlreadyDeclared, scopeNode->m_name.getContent()); + m_sink->diagnose( + m_reader.peekToken(), + CPPDiagnostics::typeAlreadyDeclared, + scopeNode->m_name.getContent()); return SLANG_FAIL; } ScopeNode* foundScopeNode = as<ScopeNode>(foundNode); SLANG_ASSERT(foundScopeNode); - // Make sure the node is empty, as we are *not* going to add it, we are just going to use - // the pre-existing namespace + // Make sure the node is empty, as we are *not* going to add it, we are just going + // to use the pre-existing namespace SLANG_ASSERT(scopeNode->m_children.getCount() == 0); // We can just use the pre-existing namespace @@ -224,7 +231,7 @@ SlangResult Parser::popScope() { return SLANG_OK; } - + m_currentScope = m_currentScope->m_parentScope; return SLANG_OK; } @@ -242,7 +249,7 @@ SlangResult Parser::_maybeConsumeScope() } else if (type == TokenType::LBrace) { - //m_reader.advanceToken(); + // m_reader.advanceToken(); return consumeToClosingBrace(); } else if (type == TokenType::EndOfFile) @@ -271,23 +278,23 @@ SlangResult Parser::consumeToClosingBrace(const Token* inOpenBraceToken) { switch (m_reader.peekTokenType()) { - case TokenType::EndOfFile: + case TokenType::EndOfFile: { m_sink->diagnose(m_reader.peekLoc(), CPPDiagnostics::didntFindMatchingBrace); m_sink->diagnose(openToken, CPPDiagnostics::seeOpen); return SLANG_FAIL; } - case TokenType::LBrace: + case TokenType::LBrace: { SLANG_RETURN_ON_FAIL(consumeToClosingBrace()); break; } - case TokenType::RBrace: + case TokenType::RBrace: { m_reader.advanceToken(); return SLANG_OK; } - default: + default: { m_reader.advanceToken(); break; @@ -331,7 +338,10 @@ SlangResult Parser::_parseEnum() } else { - m_sink->diagnose(nameToken.loc, CPPDiagnostics::expectingIdentifier, nameToken.getContent()); + m_sink->diagnose( + nameToken.loc, + CPPDiagnostics::expectingIdentifier, + nameToken.getContent()); return SLANG_FAIL; } } @@ -348,8 +358,7 @@ SlangResult Parser::_parseEnum() while (true) { TokenType tokenType = m_reader.peekTokenType(); - if (tokenType == TokenType::Semicolon || - tokenType == TokenType::LBrace || + if (tokenType == TokenType::Semicolon || tokenType == TokenType::LBrace || tokenType == TokenType::EndOfFile) { break; @@ -358,9 +367,9 @@ SlangResult Parser::_parseEnum() backingTokens.add(m_reader.advanceToken()); } - // TODO - Look up the backing type. It can only be an integral. We can assume it must be defined before lookup - // for our uses here. - // If we can't find the type, we could assume it's size is undefined + // TODO - Look up the backing type. It can only be an integral. We can assume it must be + // defined before lookup for our uses here. If we can't find the type, we could assume it's + // size is undefined if (backingTokens.getCount() > 0) { @@ -377,13 +386,17 @@ SlangResult Parser::_parseEnum() Node* node = m_currentScope->findChild(nameToken.getContent()); if (node) { - // Strictly speaking we should check the backing type etc, match, but for now ignore and assume it's ok + // Strictly speaking we should check the backing type etc, match, but for now ignore + // and assume it's ok if (node->m_kind == kind) { return SLANG_OK; } - m_sink->diagnose(nameToken.loc, CPPDiagnostics::typeAlreadyDeclared, nameToken.getContent()); + m_sink->diagnose( + nameToken.loc, + CPPDiagnostics::typeAlreadyDeclared, + nameToken.getContent()); return SLANG_FAIL; } return popScope(); @@ -407,7 +420,10 @@ SlangResult Parser::_parseEnum() if (node->findChild(caseNode->m_name.getContent())) { - m_sink->diagnose(caseNode->m_name.loc, CPPDiagnostics::identifierAlreadyDefined, caseNode->m_name.getContent()); + m_sink->diagnose( + caseNode->m_name.loc, + CPPDiagnostics::identifierAlreadyDefined, + caseNode->m_name.getContent()); return SLANG_FAIL; } @@ -433,7 +449,7 @@ SlangResult Parser::_parseEnum() m_reader.advanceToken(); continue; } - + break; } @@ -497,34 +513,34 @@ SlangResult Parser::_consumeTemplate() } } - // Search for { or ; to consume remaining + // Search for { or ; to consume remaining while (true) { auto tokenType = m_reader.peekTokenType(); switch (tokenType) { - case TokenType::EndOfFile: + case TokenType::EndOfFile: { m_sink->diagnose(m_reader.peekLoc(), CPPDiagnostics::unexpectedEndOfFile); return SLANG_FAIL; } - case TokenType::Semicolon: + case TokenType::Semicolon: { // Ends with semicolon if it's a template pre-declaration m_reader.advanceToken(); return SLANG_OK; } - case TokenType::LBrace: + case TokenType::LBrace: { - // If ends with {, means could be body of a struct/class or a body of a function/method. - // Consume it + // If ends with {, means could be body of a struct/class or a body of a + // function/method. Consume it SLANG_RETURN_ON_FAIL(consumeToClosingBrace()); // If we hit a ; just consume and ignore advanceIfToken(TokenType::Semicolon); return SLANG_OK; } - default: + default: { // Consume m_reader.advanceToken(); @@ -537,7 +553,8 @@ SlangResult Parser::_consumeTemplate() SlangResult Parser::_maybeParseNode(Node::Kind kind) { // We are looking for - // struct/class identifier [: [public|private|protected] Identifier ] { [public|private|proctected:]* marker ( identifier ); + // struct/class identifier [: [public|private|protected] Identifier ] { + // [public|private|proctected:]* marker ( identifier ); if (kind == Node::Kind::Namespace) { @@ -625,7 +642,10 @@ SlangResult Parser::_maybeParseNode(Node::Kind kind) if (peekTokenType == TokenType::EndOfFile) { // Expecting brace - m_sink->diagnose(m_reader.peekToken(), CPPDiagnostics::expectingToken, TokenType::LBrace); + m_sink->diagnose( + m_reader.peekToken(), + CPPDiagnostics::expectingToken, + TokenType::LBrace); return SLANG_FAIL; } else if (peekTokenType == TokenType::LBrace) @@ -643,7 +663,7 @@ SlangResult Parser::_maybeParseNode(Node::Kind kind) // Push the class scope return pushScope(node); } - + SlangResult Parser::_consumeToSync() { while (true) @@ -652,15 +672,15 @@ SlangResult Parser::_consumeToSync() switch (type) { - case TokenType::Semicolon: + case TokenType::Semicolon: { m_reader.advanceToken(); return SLANG_OK; } - case TokenType::Pound: - case TokenType::EndOfFile: - case TokenType::LBrace: - case TokenType::RBrace: + case TokenType::Pound: + case TokenType::EndOfFile: + case TokenType::LBrace: + case TokenType::RBrace: { return SLANG_OK; } @@ -674,18 +694,18 @@ SlangResult Parser::_maybeParseTemplateArg(Index& ioTemplateDepth) { switch (m_reader.peekTokenType()) { - case TokenType::Identifier: + case TokenType::Identifier: { TokenReader::ParsingCursor nameCursor; SLANG_RETURN_ON_FAIL(_maybeParseType(ioTemplateDepth, nameCursor)); return SLANG_OK; } - case TokenType::IntegerLiteral: + case TokenType::IntegerLiteral: { m_reader.advanceToken(); return SLANG_OK; } - default: break; + default: break; } return SLANG_FAIL; } @@ -708,7 +728,7 @@ SlangResult Parser::_maybeParseTemplateArgs(Index& ioTemplateDepth) switch (m_reader.peekTokenType()) { - case TokenType::OpGreater: + case TokenType::OpGreater: { if (ioTemplateDepth <= 0) { @@ -719,7 +739,7 @@ SlangResult Parser::_maybeParseTemplateArgs(Index& ioTemplateDepth) m_reader.advanceToken(); return SLANG_OK; } - case TokenType::OpRsh: + case TokenType::OpRsh: { if (ioTemplateDepth <= 1) { @@ -730,7 +750,7 @@ SlangResult Parser::_maybeParseTemplateArgs(Index& ioTemplateDepth) m_reader.advanceToken(); return SLANG_OK; } - default: + default: { while (true) { @@ -752,48 +772,52 @@ SlangResult Parser::_maybeParseTemplateArgs(Index& ioTemplateDepth) SlangResult Parser::_maybeConsume(IdentifierStyle style) { - while (advanceIfStyle(style)); + while (advanceIfStyle(style)) + ; return SLANG_OK; } -// True if two of these token types of the same type placed immediately after one another +// True if two of these token types of the same type placed immediately after one another // produce a different token. Can be conservative, as if not strictly required // it will just mean more spacing in the output static bool _canRepeatTokenType(TokenType type) { switch (type) { - case TokenType::OpAdd: - case TokenType::OpSub: - case TokenType::OpAnd: - case TokenType::OpOr: - case TokenType::OpGreater: - case TokenType::OpLess: - case TokenType::Identifier: - case TokenType::OpAssign: - case TokenType::Colon: + case TokenType::OpAdd: + case TokenType::OpSub: + case TokenType::OpAnd: + case TokenType::OpOr: + case TokenType::OpGreater: + case TokenType::OpLess: + case TokenType::Identifier: + case TokenType::OpAssign: + case TokenType::Colon: { return false; } - default: break; + default: break; } return true; } // Returns true if there needs to be a space between the previous token type, and the current token // type for correct output. It is assumed that the token stream is appropriate. -// The implementation might need more sophistication, but this at least avoids Blah const * -> Blahconst* +// The implementation might need more sophistication, but this at least avoids Blah const * -> +// Blahconst* static bool _tokenConcatNeedsSpace(TokenType prev, TokenType cur) { - if ((cur == TokenType::OpAssign) || - (prev == cur && !_canRepeatTokenType(cur))) + if ((cur == TokenType::OpAssign) || (prev == cur && !_canRepeatTokenType(cur))) { return true; } return false; } -void Parser::_getTypeTokens(TokenReader::ParsingCursor start, TokenReader::ParsingCursor nameCursor, List<Token>& outToks) +void Parser::_getTypeTokens( + TokenReader::ParsingCursor start, + TokenReader::ParsingCursor nameCursor, + List<Token>& outToks) { auto endCursor = m_reader.getCursor(); m_reader.setCursor(start); @@ -811,7 +835,9 @@ void Parser::_getTypeTokens(TokenReader::ParsingCursor start, TokenReader::Parsi } } -UnownedStringSlice Parser::_concatType(TokenReader::ParsingCursor start, TokenReader::ParsingCursor nameCursor) +UnownedStringSlice Parser::_concatType( + TokenReader::ParsingCursor start, + TokenReader::ParsingCursor nameCursor) { List<Token> toks; _getTypeTokens(start, nameCursor, toks); @@ -868,7 +894,9 @@ UnownedStringSlice Parser::_concatTokens(TokenReader::ParsingCursor start) return typePool->getSlice(typePool->add(buf)); } -SlangResult Parser::_maybeParseType(Index& ioTemplateDepth, TokenReader::ParsingCursor& outNameCursor) +SlangResult Parser::_maybeParseType( + Index& ioTemplateDepth, + TokenReader::ParsingCursor& outNameCursor) { outNameCursor = TokenReader::ParsingCursor(); @@ -876,11 +904,11 @@ SlangResult Parser::_maybeParseType(Index& ioTemplateDepth, TokenReader::Parsing { if (m_reader.peekTokenType() == TokenType::Identifier) { - const IdentifierStyle style = m_nodeTree->m_identifierLookup->get(m_reader.peekToken().getContent()); + const IdentifierStyle style = + m_nodeTree->m_identifierLookup->get(m_reader.peekToken().getContent()); if (style == IdentifierStyle::TypeModifier || - style == IdentifierStyle::IntegerModifier || - style == IdentifierStyle::Class || + style == IdentifierStyle::IntegerModifier || style == IdentifierStyle::Class || style == IdentifierStyle::Struct) { // These are ok keywords in this context @@ -899,7 +927,8 @@ SlangResult Parser::_maybeParseType(Index& ioTemplateDepth, TokenReader::Parsing const Token peekToken = m_reader.peekToken(); if (peekToken.type == TokenType::Identifier) { - const IdentifierStyle style = m_nodeTree->m_identifierLookup->get(peekToken.getContent()); + const IdentifierStyle style = + m_nodeTree->m_identifierLookup->get(peekToken.getContent()); if (style == IdentifierStyle::IntegerType) { m_reader.advanceToken(); @@ -907,16 +936,16 @@ SlangResult Parser::_maybeParseType(Index& ioTemplateDepth, TokenReader::Parsing } break; } - + advanceIfToken(TokenType::Scope); while (true) { // if we have a struct/class prefix in front of a name just consume it. if (m_reader.peekTokenType() == TokenType::Identifier) { - const IdentifierStyle style = m_nodeTree->m_identifierLookup->get(m_reader.peekToken().getContent()); - if (style == IdentifierStyle::Class || - style == IdentifierStyle::Struct) + const IdentifierStyle style = + m_nodeTree->m_identifierLookup->get(m_reader.peekToken().getContent()); + if (style == IdentifierStyle::Class || style == IdentifierStyle::Struct) { m_reader.advanceToken(); } @@ -928,7 +957,8 @@ SlangResult Parser::_maybeParseType(Index& ioTemplateDepth, TokenReader::Parsing return SLANG_FAIL; } - const IdentifierStyle style = m_nodeTree->m_identifierLookup->get(identifierToken.getContent()); + const IdentifierStyle style = + m_nodeTree->m_identifierLookup->get(identifierToken.getContent()); if (hasFlag(style, IdentifierFlag::Keyword)) { return SLANG_FAIL; @@ -986,11 +1016,14 @@ SlangResult Parser::_maybeParseType(Index& ioTemplateDepth, TokenReader::Parsing SLANG_RETURN_ON_FAIL(expect(TokenType::Identifier)); SLANG_RETURN_ON_FAIL(expect(TokenType::RParent)); - + // We need to parse and add the params if (m_reader.peekTokenType() != TokenType::LParent) { - m_sink->diagnose(m_reader.peekToken(), CPPDiagnostics::expectingToken, TokenType::LParent); + m_sink->diagnose( + m_reader.peekToken(), + CPPDiagnostics::expectingToken, + TokenType::LParent); return SLANG_FAIL; } @@ -1108,15 +1141,18 @@ SlangResult Parser::_parseSpecialMacro() SlangResult Parser::_parseMarker() { - SLANG_ASSERT(m_reader.peekTokenType() == TokenType::Identifier && - _isMarker(m_reader.peekToken().getContent()) && - m_currentScope->isClassLike()); + SLANG_ASSERT( + m_reader.peekTokenType() == TokenType::Identifier && + _isMarker(m_reader.peekToken().getContent()) && m_currentScope->isClassLike()); ClassLikeNode* node = as<ClassLikeNode>(m_currentScope); if (node->m_marker.type != TokenType::Unknown) { - m_sink->diagnose(m_reader.peekToken(), CPPDiagnostics::classMarkerAlreadyFound, node->m_name.getContent()); + m_sink->diagnose( + m_reader.peekToken(), + CPPDiagnostics::classMarkerAlreadyFound, + node->m_name.getContent()); m_sink->diagnose(node->m_marker, CPPDiagnostics::previousLocation); return SLANG_FAIL; } @@ -1128,7 +1164,9 @@ SlangResult Parser::_parseMarker() UnownedStringSlice slice(node->m_marker.getContent()); // Strip the prefix and suffix - slice = UnownedStringSlice(slice.begin() + m_options->m_markPrefix.getLength(), slice.end() - m_options->m_markSuffix.getLength()); + slice = UnownedStringSlice( + slice.begin() + m_options->m_markPrefix.getLength(), + slice.end() - m_options->m_markSuffix.getLength()); // Strip ABSTRACT_ if it's there UnownedStringSlice abstractSlice("ABSTRACT_"); @@ -1140,7 +1178,7 @@ SlangResult Parser::_parseMarker() // TODO: We could strip other stuff or have other heuristics there, but this is // probably okay for now - // Set the typeSet + // Set the typeSet node->m_typeSet = m_nodeTree->getOrAddTypeSet(slice); // Okay now looking for ( identifier) @@ -1152,7 +1190,10 @@ SlangResult Parser::_parseMarker() if (typeNameToken.getContent() != node->m_name.getContent()) { - m_sink->diagnose(typeNameToken, CPPDiagnostics::typeNameDoesntMatch, node->m_name.getContent()); + m_sink->diagnose( + typeNameToken, + CPPDiagnostics::typeNameDoesntMatch, + node->m_name.getContent()); return SLANG_FAIL; } @@ -1203,16 +1244,14 @@ SlangResult Parser::_maybeParseType(UnownedStringSlice& outType, Token& outName) static bool _isBalancedOpen(TokenType tokenType) { - return tokenType == TokenType::LBrace || - tokenType == TokenType::LParent || - tokenType == TokenType::LBracket; + return tokenType == TokenType::LBrace || tokenType == TokenType::LParent || + tokenType == TokenType::LBracket; } static bool _isBalancedClose(TokenType tokenType) { - return tokenType == TokenType::RBrace || - tokenType == TokenType::RParent || - tokenType == TokenType::RBracket; + return tokenType == TokenType::RBrace || tokenType == TokenType::RParent || + tokenType == TokenType::RBracket; } static TokenType _getBalancedClose(TokenType tokenType) @@ -1220,10 +1259,10 @@ static TokenType _getBalancedClose(TokenType tokenType) SLANG_ASSERT(_isBalancedOpen(tokenType)); switch (tokenType) { - case TokenType::LBrace: return TokenType::RBrace; - case TokenType::LParent: return TokenType::RParent; - case TokenType::LBracket: return TokenType::RBracket; - default: return TokenType::Unknown; + case TokenType::LBrace: return TokenType::RBrace; + case TokenType::LParent: return TokenType::RParent; + case TokenType::LBracket: return TokenType::RBracket; + default: return TokenType::Unknown; } } @@ -1251,7 +1290,7 @@ SlangResult Parser::_parseBalanced(DiagnosticSink* sink) return SLANG_OK; } - // If we hit a balanced open, recurse + // If we hit a balanced open, recurse if (_isBalancedOpen(tokenType)) { SLANG_RETURN_ON_FAIL(_parseBalanced(sink)); @@ -1300,12 +1339,12 @@ SlangResult Parser::_consumeBalancedParens() switch (tokenType) { - case TokenType::LParent: + case TokenType::LParent: { parenCount++; break; } - case TokenType::RParent: + case TokenType::RParent: { --parenCount; // If no more parens then we are done @@ -1316,12 +1355,12 @@ SlangResult Parser::_consumeBalancedParens() } break; } - case TokenType::EndOfFile: + case TokenType::EndOfFile: { // If we hit the end of the file, then not balanced return SLANG_FAIL; } - default: break; + default: break; } m_reader.advanceToken(); @@ -1333,9 +1372,10 @@ SlangResult Parser::_parseExpression(List<Token>& outExprTokens) Index parenCount = 0; Index bracketCount = 0; - // TODO(JS): NOTE! This doesn't handle an expression that contains a template params in Something<Arg1, 3>, - // because without knowing what Something is, it's not known if < is a comparison or or a 'template' bracket - // + // TODO(JS): NOTE! This doesn't handle an expression that contains a template params in + // Something<Arg1, 3>, because without knowing what Something is, it's not known if < is a + // comparison or or a 'template' bracket + // // This can be worked around in the originating source by placing in parens while (true) @@ -1344,12 +1384,12 @@ SlangResult Parser::_parseExpression(List<Token>& outExprTokens) switch (tokenType) { - case TokenType::LParent: + case TokenType::LParent: { parenCount++; break; } - case TokenType::RParent: + case TokenType::RParent: { // If no parens, and nothing else is open then we are done if (parenCount == 0) @@ -1365,12 +1405,12 @@ SlangResult Parser::_parseExpression(List<Token>& outExprTokens) --parenCount; break; } - case TokenType::LBracket: + case TokenType::LBracket: { bracketCount++; break; } - case TokenType::RBracket: + case TokenType::RBracket: { // If no brackets are open we are done if (bracketCount == 0) @@ -1385,7 +1425,7 @@ SlangResult Parser::_parseExpression(List<Token>& outExprTokens) --bracketCount; break; } - case TokenType::EndOfFile: + case TokenType::EndOfFile: { if ((bracketCount | parenCount) == 0) { @@ -1394,9 +1434,9 @@ SlangResult Parser::_parseExpression(List<Token>& outExprTokens) m_sink->diagnose(m_reader.peekLoc(), CPPDiagnostics::cannotParseExpression); return SLANG_FAIL; } - case TokenType::RBrace: - case TokenType::Semicolon: - case TokenType::Comma: + case TokenType::RBrace: + case TokenType::Semicolon: + case TokenType::Comma: { if ((bracketCount | parenCount) == 0) { @@ -1405,7 +1445,7 @@ SlangResult Parser::_parseExpression(List<Token>& outExprTokens) break; } - default: break; + default: break; } outExprTokens.add(m_reader.advanceToken()); @@ -1436,7 +1476,10 @@ SlangResult Parser::_parseTypeDef() if (Node::lookupNameInScope(m_currentScope, nameToken.getContent())) { - m_sink->diagnose(nameToken.loc, CPPDiagnostics::identifierAlreadyDefined, nameToken.getContent()); + m_sink->diagnose( + nameToken.loc, + CPPDiagnostics::identifierAlreadyDefined, + nameToken.getContent()); return SLANG_FAIL; } @@ -1484,7 +1527,8 @@ SlangResult Parser::_maybeParseContained(Node** outNode) while (m_reader.peekTokenType() == TokenType::Identifier) { - const IdentifierStyle style = m_nodeTree->m_identifierLookup->get(m_reader.peekToken().getContent()); + const IdentifierStyle style = + m_nodeTree->m_identifierLookup->get(m_reader.peekToken().getContent()); // Check for virtualness if (style == IdentifierStyle::Virtual) @@ -1494,13 +1538,13 @@ SlangResult Parser::_maybeParseContained(Node** outNode) continue; } - // Check if static + // Check if static if (style == IdentifierStyle::Static) { isStatic = true; m_reader.advanceToken(); continue; - } + } break; } @@ -1511,7 +1555,7 @@ SlangResult Parser::_maybeParseContained(Node** outNode) Token nameToken; bool isConstructor = false; - + if (m_currentScope->isClassLike()) { // If it's a dtor @@ -1524,7 +1568,10 @@ SlangResult Parser::_maybeParseContained(Node** outNode) if (tok.getContent() != m_currentScope->m_name.getContent()) { - m_sink->diagnose(m_reader.peekLoc(), CPPDiagnostics::destructorNameDoesntMatch, m_currentScope->m_name.getContent()); + m_sink->diagnose( + m_reader.peekLoc(), + CPPDiagnostics::destructorNameDoesntMatch, + m_currentScope->m_name.getContent()); return SLANG_FAIL; } } @@ -1563,7 +1610,7 @@ SlangResult Parser::_maybeParseContained(Node** outNode) return SLANG_OK; } } - + // Handles other scenarios, but here for catching operator overloading if (nameToken.type == TokenType::Identifier) { @@ -1582,7 +1629,7 @@ SlangResult Parser::_maybeParseContained(Node** outNode) SLANG_RETURN_ON_FAIL(_consumeBalancedParens()); // Consume everything up to ; or { SLANG_RETURN_ON_FAIL(_consumeToSync()); - + return SLANG_OK; } @@ -1639,7 +1686,7 @@ SlangResult Parser::_maybeParseContained(Node** outNode) continue; } } - + m_sink->diagnose(m_reader.peekLoc(), CPPDiagnostics::expectingToken, ", or ) or ="); return SLANG_FAIL; } @@ -1662,8 +1709,7 @@ SlangResult Parser::_maybeParseContained(Node** outNode) while (true) { auto peekType = m_reader.peekTokenType(); - if (peekType == TokenType::Semicolon || - peekType == TokenType::LBrace || + if (peekType == TokenType::Semicolon || peekType == TokenType::LBrace || peekType == TokenType::EndOfFile) { break; @@ -1673,14 +1719,15 @@ SlangResult Parser::_maybeParseContained(Node** outNode) } } } - + // = 0 ? or = default if (advanceIfToken(TokenType::OpAssign)) { if (m_reader.peekTokenType() == TokenType::IntegerLiteral) { Int value = -1; - if (SLANG_SUCCEEDED(StringUtil::parseInt(m_reader.peekToken().getContent(), value)) && + if (SLANG_SUCCEEDED( + StringUtil::parseInt(m_reader.peekToken().getContent(), value)) && value == 0) { isPure = true; @@ -1693,7 +1740,7 @@ SlangResult Parser::_maybeParseContained(Node** outNode) } } else if (advanceIfStyle(IdentifierStyle::Default)) - { + { } else { @@ -1768,7 +1815,7 @@ SlangResult Parser::_maybeParseContained(Node** outNode) List<Token> exprTokens; SLANG_RETURN_ON_FAIL(_parseExpression(exprTokens)); } - + // Hit end of field/variable if (m_reader.peekTokenType() == TokenType::Semicolon) { @@ -1785,21 +1832,21 @@ SlangResult Parser::_maybeParseContained(Node** outNode) return SLANG_OK; } } - + _consumeToSync(); return SLANG_OK; } -/* static */Node::Kind Parser::_toNodeKind(IdentifierStyle style) +/* static */ Node::Kind Parser::_toNodeKind(IdentifierStyle style) { switch (style) { - case IdentifierStyle::Class: return Node::Kind::ClassType; - case IdentifierStyle::Struct: return Node::Kind::StructType; - case IdentifierStyle::Namespace: return Node::Kind::Namespace; - case IdentifierStyle::Enum: return Node::Kind::Enum; - case IdentifierStyle::TypeDef: return Node::Kind::TypeDef; - default: return Node::Kind::Invalid; + case IdentifierStyle::Class: return Node::Kind::ClassType; + case IdentifierStyle::Struct: return Node::Kind::StructType; + case IdentifierStyle::Namespace: return Node::Kind::Namespace; + case IdentifierStyle::Enum: return Node::Kind::Enum; + case IdentifierStyle::TypeDef: return Node::Kind::TypeDef; + default: return Node::Kind::Invalid; } } @@ -1839,7 +1886,10 @@ SlangResult Parser::_parsePreDeclare() if (style != IdentifierStyle::Struct && style != IdentifierStyle::Class) { - m_sink->diagnose(typeToken, CPPDiagnostics::expectingTypeKeyword, typeToken.getContent()); + m_sink->diagnose( + typeToken, + CPPDiagnostics::expectingTypeKeyword, + typeToken.getContent()); return SLANG_FAIL; } nodeKind = _toNodeKind(style); @@ -1859,8 +1909,8 @@ SlangResult Parser::_parsePreDeclare() switch (nodeKind) { - case Node::Kind::ClassType: - case Node::Kind::StructType: + case Node::Kind::ClassType: + case Node::Kind::StructType: { RefPtr<ClassLikeNode> node(new ClassLikeNode(nodeKind)); @@ -1876,7 +1926,7 @@ SlangResult Parser::_parsePreDeclare() popScope(); break; } - default: + default: { return SLANG_FAIL; } @@ -1930,7 +1980,7 @@ SlangResult Parser::parse(SourceOrigin* sourceOrigin, const Options* options) // Set up the scope stack m_scopeStack.clear(); - + m_currentScope = m_nodeTree->m_rootNode; m_scopeStack.add(m_currentScope); @@ -1953,7 +2003,7 @@ SlangResult Parser::parse(SourceOrigin* sourceOrigin, const Options* options) { switch (m_reader.peekTokenType()) { - case TokenType::OpBitNot: + case TokenType::OpBitNot: { // Handle dtor if (m_currentScope->isClassLike()) @@ -1968,13 +2018,14 @@ SlangResult Parser::parse(SourceOrigin* sourceOrigin, const Options* options) } break; } - case TokenType::Identifier: + case TokenType::Identifier: { - const IdentifierStyle style = m_nodeTree->m_identifierLookup->get(m_reader.peekToken().getContent()); + const IdentifierStyle style = + m_nodeTree->m_identifierLookup->get(m_reader.peekToken().getContent()); switch (style) { - case IdentifierStyle::Extern: + case IdentifierStyle::Extern: { m_reader.advanceToken(); @@ -1988,22 +2039,22 @@ SlangResult Parser::parse(SourceOrigin* sourceOrigin, const Options* options) } break; } - case IdentifierStyle::Template: + case IdentifierStyle::Template: { SLANG_RETURN_ON_FAIL(_consumeTemplate()); break; } - case IdentifierStyle::PreDeclare: + case IdentifierStyle::PreDeclare: { SLANG_RETURN_ON_FAIL(_parsePreDeclare()); break; } - case IdentifierStyle::TypeSet: + case IdentifierStyle::TypeSet: { SLANG_RETURN_ON_FAIL(_parseTypeSet()); break; } - case IdentifierStyle::Reflected: + case IdentifierStyle::Reflected: { m_reader.advanceToken(); if (m_currentScope) @@ -2012,7 +2063,7 @@ SlangResult Parser::parse(SourceOrigin* sourceOrigin, const Options* options) } break; } - case IdentifierStyle::Unreflected: + case IdentifierStyle::Unreflected: { m_reader.advanceToken(); if (m_currentScope) @@ -2021,13 +2072,13 @@ SlangResult Parser::parse(SourceOrigin* sourceOrigin, const Options* options) } break; } - case IdentifierStyle::Access: + case IdentifierStyle::Access: { m_reader.advanceToken(); SLANG_RETURN_ON_FAIL(expect(TokenType::Colon)); break; } - case IdentifierStyle::TypeDef: + case IdentifierStyle::TypeDef: { if (isTypeEnabled(Node::Kind::TypeDef)) { @@ -2040,7 +2091,7 @@ SlangResult Parser::parse(SourceOrigin* sourceOrigin, const Options* options) } break; } - default: + default: { IdentifierFlags flags = getFlags(style); @@ -2067,7 +2118,9 @@ SlangResult Parser::parse(SourceOrigin* sourceOrigin, const Options* options) { if (!m_currentScope->isClassLike()) { - m_sink->diagnose(m_reader.peekLoc(), CPPDiagnostics::classMarkerOutsideOfClass); + m_sink->diagnose( + m_reader.peekLoc(), + CPPDiagnostics::classMarkerOutsideOfClass); return SLANG_FAIL; } @@ -2075,16 +2128,18 @@ SlangResult Parser::parse(SourceOrigin* sourceOrigin, const Options* options) break; } - if (m_options->m_markPrefix.getLength() > 0 && content.startsWith(m_options->m_markPrefix.getUnownedSlice())) + if (m_options->m_markPrefix.getLength() > 0 && + content.startsWith(m_options->m_markPrefix.getUnownedSlice())) { SLANG_RETURN_ON_FAIL(_parseSpecialMacro()); break; } - // Special case the node that's the root of the hierarchy (as far as reflection is concerned) - // This could be a field - if (m_currentScope->canContainFields() || m_currentScope->canContainCallable()) + // Special case the node that's the root of the hierarchy (as far as + // reflection is concerned) This could be a field + if (m_currentScope->canContainFields() || + m_currentScope->canContainCallable()) { Node* containedNode = nullptr; SLANG_RETURN_ON_FAIL(_maybeParseContained(&containedNode)); @@ -2099,18 +2154,18 @@ SlangResult Parser::parse(SourceOrigin* sourceOrigin, const Options* options) } break; } - case TokenType::LBrace: + case TokenType::LBrace: { SLANG_RETURN_ON_FAIL(consumeToClosingBrace()); break; } - case TokenType::RBrace: + case TokenType::RBrace: { SLANG_RETURN_ON_FAIL(popScope()); m_reader.advanceToken(); break; } - case TokenType::EndOfFile: + case TokenType::EndOfFile: { // Okay we need to confirm that we are in the root node, and with no open braces if (m_currentScope != m_nodeTree->getRootNode()) @@ -2121,7 +2176,7 @@ SlangResult Parser::parse(SourceOrigin* sourceOrigin, const Options* options) return SLANG_OK; } - case TokenType::Pound: + case TokenType::Pound: { Token token = m_reader.peekToken(); if (token.flags & TokenFlag::AtStartOfLine) @@ -2143,7 +2198,7 @@ SlangResult Parser::parse(SourceOrigin* sourceOrigin, const Options* options) m_reader.advanceToken(); break; } - default: + default: { // Skip it then m_reader.advanceToken(); diff --git a/tools/slang-cpp-extractor/parser.h b/tools/slang-cpp-extractor/parser.h index cf0147e8b..605a0d8be 100644 --- a/tools/slang-cpp-extractor/parser.h +++ b/tools/slang-cpp-extractor/parser.h @@ -1,20 +1,19 @@ #ifndef CPP_EXTRACT_PARSER_H #define CPP_EXTRACT_PARSER_H +#include "../../source/compiler-core/slang-lexer.h" #include "diagnostics.h" -#include "node.h" #include "identifier-lookup.h" #include "node-tree.h" +#include "node.h" -#include "../../source/compiler-core/slang-lexer.h" - -namespace CppExtract { +namespace CppExtract +{ using namespace Slang; class Parser { public: - typedef uint32_t NodeTypeBitType; SlangResult expect(TokenType type, Token* outToken = nullptr); @@ -28,11 +27,14 @@ public: SlangResult consumeToClosingBrace(const Token* openBraceToken = nullptr); SlangResult popScope(); - /// Parse the contents of the source file + /// Parse the contents of the source file SlangResult parse(SourceOrigin* sourceOrigin, const Options* options); void setKindEnabled(Node::Kind kind, bool isEnabled = true); - bool isTypeEnabled(Node::Kind kind) { return (m_nodeTypeEnabled & (NodeTypeBitType(1) << int(kind))) != 0; } + bool isTypeEnabled(Node::Kind kind) + { + return (m_nodeTypeEnabled & (NodeTypeBitType(1) << int(kind))) != 0; + } void setKindsEnabled(const Node::Kind* kinds, Index kindsCount, bool isEnabled = true); @@ -61,29 +63,34 @@ protected: SlangResult _maybeParseType(Index& ioTemplateDepth, TokenReader::ParsingCursor& outCursor); SlangResult _parseExpression(List<Token>& outExprTokens); - + SlangResult _maybeParseTemplateArgs(Index& ioTemplateDepth); SlangResult _maybeParseTemplateArg(Index& ioTemplateDepth); - /// Parse balanced - if a sink is set will report to that sink + /// Parse balanced - if a sink is set will report to that sink SlangResult _parseBalanced(DiagnosticSink* sink); bool _isCtor(); - /// Concatenate all tokens from start to the current position + /// Concatenate all tokens from start to the current position UnownedStringSlice _concatTokens(TokenReader::ParsingCursor start); UnownedStringSlice _concatTokens(const Token* toks, Index toksCount); - UnownedStringSlice _concatType(TokenReader::ParsingCursor start, TokenReader::ParsingCursor nameCursor); + UnownedStringSlice _concatType( + TokenReader::ParsingCursor start, + TokenReader::ParsingCursor nameCursor); - void _getTypeTokens(TokenReader::ParsingCursor start, TokenReader::ParsingCursor nameCursor, List<Token>& outToks); + void _getTypeTokens( + TokenReader::ParsingCursor start, + TokenReader::ParsingCursor nameCursor, + List<Token>& outToks); - /// Consume what looks like a template definition + /// Consume what looks like a template definition SlangResult _consumeTemplate(); SlangResult _maybeConsume(IdentifierStyle style); SlangResult _consumeToSync(); - /// Consumes balanced parens. Will return an error if not matched. Assumes starts on opening ( + /// Consumes balanced parens. Will return an error if not matched. Assumes starts on opening ( SlangResult _consumeBalancedParens(); NodeTypeBitType m_nodeTypeEnabled; @@ -93,16 +100,16 @@ protected: List<ScopeNode*> m_scopeStack; - ScopeNode* m_currentScope; ///< The current scope being processed - SourceOrigin* m_sourceOrigin; ///< The source origin that all tokens are in + ScopeNode* m_currentScope; ///< The current scope being processed + SourceOrigin* m_sourceOrigin; ///< The source origin that all tokens are in - DiagnosticSink* m_sink; ///< Diagnostic sink + DiagnosticSink* m_sink; ///< Diagnostic sink - NodeTree* m_nodeTree; ///< Shared state between parses. Nodes will be added to this + NodeTree* m_nodeTree; ///< Shared state between parses. Nodes will be added to this const Options* m_options; }; -} // CppExtract +} // namespace CppExtract #endif diff --git a/tools/slang-cpp-extractor/unit-test.cpp b/tools/slang-cpp-extractor/unit-test.cpp index 16b4c10dc..3c0edac3c 100644 --- a/tools/slang-cpp-extractor/unit-test.cpp +++ b/tools/slang-cpp-extractor/unit-test.cpp @@ -1,26 +1,23 @@ #include "unit-test.h" - +#include "../../source/compiler-core/slang-lexer.h" #include "../../source/compiler-core/slang-name-convention-util.h" - -#include "../../source/core/slang-io.h" - #include "../../source/compiler-core/slang-source-loc.h" -#include "../../source/compiler-core/slang-lexer.h" - +#include "../../source/core/slang-io.h" #include "identifier-lookup.h" #include "node-tree.h" -#include "parser.h" #include "options.h" +#include "parser.h" -namespace CppExtract { +namespace CppExtract +{ using namespace Slang; struct TestState { - TestState(): - m_slicePool(StringSlicePool::Style::Default) + TestState() + : m_slicePool(StringSlicePool::Style::Default) { m_identifierLookup.initDefault(UnownedStringSlice::fromLiteral("SLANG_")); @@ -43,48 +40,47 @@ struct TestState IdentifierLookup m_identifierLookup; }; -static const char someSource[] = -"class ISomeInterface\n" -"{\n" -" public:\n" -" virtual int SLANG_MCALL someMethod(int a, int b) const = 0;\n" -" virtual float SLANG_MCALL anotherMethod(float a) = 0;\n" -"};\n" -"\n" -"struct SomeStruct\n" -"{\n" -" SomeStruct() = default;\n" -" SomeStruct(float v = 0.0f):b(v) {}\n" -" ~SomeStruct() {}\n" -" int a = 10; \n" -" float b; \n" -" int another[10];\n" -" const char* yetAnother = nullptr;\n" -"};\n" -"\n" -"enum SomeEnum\n" -"{\n" -" Value,\n" -" Another = 10,\n" -"};\n" -"\n" -"typedef int (*SomeFunc)(int a);\n" -"\n" -"typedef SomeEnum AliasEnum;\n" -"void someFunc(int a, float b) { }\n" -"namespace Blah {\n" -"int add(int a, int b) { return a + b; }\n" -"unsigned add(unsigned a, unsigned b) { return a + b; }\n" -"}\n"; - - -/* static */SlangResult UnitTestUtil::run() +static const char someSource[] = "class ISomeInterface\n" + "{\n" + " public:\n" + " virtual int SLANG_MCALL someMethod(int a, int b) const = 0;\n" + " virtual float SLANG_MCALL anotherMethod(float a) = 0;\n" + "};\n" + "\n" + "struct SomeStruct\n" + "{\n" + " SomeStruct() = default;\n" + " SomeStruct(float v = 0.0f):b(v) {}\n" + " ~SomeStruct() {}\n" + " int a = 10; \n" + " float b; \n" + " int another[10];\n" + " const char* yetAnother = nullptr;\n" + "};\n" + "\n" + "enum SomeEnum\n" + "{\n" + " Value,\n" + " Another = 10,\n" + "};\n" + "\n" + "typedef int (*SomeFunc)(int a);\n" + "\n" + "typedef SomeEnum AliasEnum;\n" + "void someFunc(int a, float b) { }\n" + "namespace Blah {\n" + "int add(int a, int b) { return a + b; }\n" + "unsigned add(unsigned a, unsigned b) { return a + b; }\n" + "}\n"; + + +/* static */ SlangResult UnitTestUtil::run() { { TestState state; NodeTree tree(&state.m_slicePool, &state.m_namePool, &state.m_identifierLookup); - + UnownedStringSlice contents = UnownedStringSlice::fromLiteral(someSource); PathInfo pathInfo = PathInfo::makeFromString("source.h"); @@ -95,9 +91,13 @@ static const char someSource[] = Parser parser(&tree, &state.m_sink); - + { - const Node::Kind enableKinds[] = { Node::Kind::Enum, Node::Kind::EnumClass, Node::Kind::EnumCase, Node::Kind::TypeDef }; + const Node::Kind enableKinds[] = { + Node::Kind::Enum, + Node::Kind::EnumClass, + Node::Kind::EnumCase, + Node::Kind::TypeDef}; parser.setKindsEnabled(enableKinds, SLANG_COUNT_OF(enableKinds)); } diff --git a/tools/slang-cpp-extractor/unit-test.h b/tools/slang-cpp-extractor/unit-test.h index 9c8d9b08c..fd3ab1328 100644 --- a/tools/slang-cpp-extractor/unit-test.h +++ b/tools/slang-cpp-extractor/unit-test.h @@ -3,7 +3,8 @@ #include "diagnostics.h" -namespace CppExtract { +namespace CppExtract +{ using namespace Slang; struct UnitTestUtil @@ -11,6 +12,6 @@ struct UnitTestUtil static SlangResult run(); }; -} // CppExtract +} // namespace CppExtract #endif |
