summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2021-04-22 09:32:25 -0400
committerGitHub <noreply@github.com>2021-04-22 09:32:25 -0400
commitda0d295d6c8b6fb03245dea0583437c198890349 (patch)
treeed17baba750b15f6ace1427f04cf19690269161e /source
parent34fba7b5e726136c6eee8a318ab9a75381399c00 (diff)
C++ extractor improvements (#1803)
* #include an absolute path didn't work - because paths were taken to always be relative. * Split of NodeTree. Split out FileUtil. Split out MacroWriter. * Rename slang-cpp-extractor-main.cpp -> cpp-extractor-main.cpp * First pass at extractor unit-tests * Initial parsing of enum. * Ability to disable/enable parsing of scope types. * Initial support for typedef. * Added operator== != to ArrayVIew. Added test for splitting to unit tests. * Improve comment in StringUtil. * Fix comment. * Fix typo.
Diffstat (limited to 'source')
-rw-r--r--source/compiler-core/slang-diagnostic-sink.cpp17
-rw-r--r--source/compiler-core/slang-diagnostic-sink.h16
-rw-r--r--source/core/slang-array-view.h29
-rw-r--r--source/core/slang-string-util.cpp46
-rw-r--r--source/core/slang-string-util.h3
5 files changed, 103 insertions, 8 deletions
diff --git a/source/compiler-core/slang-diagnostic-sink.cpp b/source/compiler-core/slang-diagnostic-sink.cpp
index 2adf31f69..727c322a5 100644
--- a/source/compiler-core/slang-diagnostic-sink.cpp
+++ b/source/compiler-core/slang-diagnostic-sink.cpp
@@ -385,6 +385,23 @@ static void formatDiagnostic(
}
}
+void DiagnosticSink::init(SourceManager* sourceManager, SourceLocationLexer sourceLocationLexer)
+{
+ m_errorCount = 0;
+ m_internalErrorLocsNoted = 0;
+
+ m_flags = 0;
+
+ m_sourceManager = sourceManager;
+ m_sourceLocationLexer = sourceLocationLexer;
+
+ // If we have a source location lexer, we'll by default enable source location output
+ if (sourceLocationLexer)
+ {
+ setFlag(Flag::SourceLocationLine);
+ }
+}
+
void DiagnosticSink::diagnoseImpl(SourceLoc const& pos, DiagnosticInfo const& info, int argCount, DiagnosticArg const* const* args)
{
StringBuilder sb;
diff --git a/source/compiler-core/slang-diagnostic-sink.h b/source/compiler-core/slang-diagnostic-sink.h
index 84001e6f5..347ff761c 100644
--- a/source/compiler-core/slang-diagnostic-sink.h
+++ b/source/compiler-core/slang-diagnostic-sink.h
@@ -217,16 +217,16 @@ public:
/// character caret at location
SourceLocationLexer getSourceLocationLexer() const { return m_sourceLocationLexer; }
+ /// Initialize state.
+ void init(SourceManager* sourceManager, SourceLocationLexer sourceLocationLexer);
+
/// Ctor
- DiagnosticSink(SourceManager* sourceManager, SourceLocationLexer sourceLocationLexer)
- : m_sourceManager(sourceManager),
- m_sourceLocationLexer(sourceLocationLexer)
+ DiagnosticSink(SourceManager* sourceManager, SourceLocationLexer sourceLocationLexer) { init(sourceManager, sourceLocationLexer); }
+ /// Default Ctor
+ DiagnosticSink():
+ m_sourceManager(nullptr),
+ m_sourceLocationLexer (nullptr)
{
- // If we have a source location lexer, we'll by default enable source location output
- if (sourceLocationLexer)
- {
- setFlag(Flag::SourceLocationLine);
- }
}
// Public members
diff --git a/source/core/slang-array-view.h b/source/core/slang-array-view.h
index 56c936073..c67a53337 100644
--- a/source/core/slang-array-view.h
+++ b/source/core/slang-array-view.h
@@ -12,6 +12,8 @@ namespace Slang
class ConstArrayView
{
public:
+ typedef ConstArrayView ThisType;
+
const T* begin() const { return m_buffer; }
const T* end() const { return m_buffer + m_count; }
@@ -70,6 +72,30 @@ namespace Slang
return -1;
}
+ bool operator==(const ThisType& rhs) const
+ {
+ if (&rhs == this)
+ {
+ return true;
+ }
+ const Index count = getCount();
+ if (count != rhs.getCount())
+ {
+ return false;
+ }
+ const T* thisEle = getBuffer();
+ const T* rhsEle = rhs.getBuffer();
+ for (Index i = 0; i < count; ++i)
+ {
+ if (thisEle[i] != rhsEle[i])
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+ SLANG_FORCE_INLINE bool operator!=(const ThisType& rhs) const { return !(*this == rhs); }
+
ConstArrayView() :
m_buffer(nullptr),
m_count(0)
@@ -111,6 +137,8 @@ namespace Slang
class ArrayView: public ConstArrayView<T>
{
public:
+ typedef ArrayView ThisType;
+
typedef ConstArrayView<T> Super;
using Super::m_buffer;
@@ -147,6 +175,7 @@ namespace Slang
{
return ArrayView<T>(buffer, count);
}
+
}
#endif
diff --git a/source/core/slang-string-util.cpp b/source/core/slang-string-util.cpp
index b4c767c87..8108bdc98 100644
--- a/source/core/slang-string-util.cpp
+++ b/source/core/slang-string-util.cpp
@@ -56,6 +56,52 @@ namespace Slang {
}
}
+/* static */void StringUtil::split(const UnownedStringSlice& in, const UnownedStringSlice& splitSlice, List<UnownedStringSlice>& outSlices)
+{
+ const Index splitLen = splitSlice.getLength();
+
+ if (splitLen == 1)
+ {
+ return split(in, splitSlice[0], outSlices);
+ }
+
+ outSlices.clear();
+
+ SLANG_ASSERT(splitLen > 0);
+ if (splitLen <= 0)
+ {
+ return;
+ }
+
+ const char* start = in.begin();
+ const char* end = in.end();
+
+ const char splitChar = splitSlice[0];
+
+ while (start < end)
+ {
+ // Move cur so it's either at the end or at next splitSlice
+ const char* cur = start;
+ while (cur < end)
+ {
+ if (*cur == splitChar &&
+ (cur + splitLen <= end && UnownedStringSlice(cur, splitLen) == splitSlice))
+ {
+ // We hit a split
+ break;
+ }
+
+ cur++;
+ }
+
+ // Add to output
+ outSlices.add(UnownedStringSlice(start, cur));
+
+ // Skip the split, if at end we are okay anyway
+ start = cur + splitLen;
+ }
+}
+
/* static */Index StringUtil::split(const UnownedStringSlice& in, char splitChar, Index maxSlices, UnownedStringSlice* outSlices)
{
Index index = 0;
diff --git a/source/core/slang-string-util.h b/source/core/slang-string-util.h
index 2850c3b4f..8031f5c8c 100644
--- a/source/core/slang-string-util.h
+++ b/source/core/slang-string-util.h
@@ -24,6 +24,9 @@ struct StringUtil
/// Split in, by specified splitChar into slices out
/// Slices contents will directly address into in, so contents will only stay valid as long as in does.
static void split(const UnownedStringSlice& in, char splitChar, List<UnownedStringSlice>& slicesOut);
+ /// Split in by the specified splitSlice
+ /// Slices contents will directly address into in, so contents will only stay valid as long as in does.
+ static void split(const UnownedStringSlice& in, const UnownedStringSlice& splitSlice, List<UnownedStringSlice>& slicesOut);
/// Splits in into outSlices, up to maxSlices. May not consume all of in (for example if it runs out of space).
static Index split(const UnownedStringSlice& in, char splitChar, Index maxSlices, UnownedStringSlice* outSlices);