From dafe651ecf21f2dce7f156179af785adca08ced0 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 28 Sep 2022 13:30:37 -0400 Subject: Improvements around diagnostic controls (#2414) * #include an absolute path didn't work - because paths were taken to always be relative. * Test for disabling warnings. * Output diagnostic if argument parsing fails in render test. * More improvements around disabling diagnostics. * Add support for re enabling a warning. * Add warning controls to help text. * Tidy up around NameConventionUtil. * Make NameConvention an enum. * Handle leading underscores. * Update comment, and remove intial handling of _ prefix. --- .../compiler-core/slang-name-convention-util.cpp | 152 ++++++++++++++++----- 1 file changed, 117 insertions(+), 35 deletions(-) (limited to 'source/compiler-core/slang-name-convention-util.cpp') diff --git a/source/compiler-core/slang-name-convention-util.cpp b/source/compiler-core/slang-name-convention-util.cpp index 930e00e18..c3d08326c 100644 --- a/source/compiler-core/slang-name-convention-util.cpp +++ b/source/compiler-core/slang-name-convention-util.cpp @@ -7,35 +7,110 @@ namespace Slang { -/* static */NameConvention NameConventionUtil::getConvention(const UnownedStringSlice& slice) +/* static */NameConvention NameConventionUtil::inferConventionFromText(const UnownedStringSlice& slice) { + // If no chars, or first char isn't alpha we don't know what it is + if (slice.getLength() <= 0 || !CharUtil::isAlpha(slice[0])) + { + return NameConvention::Invalid; + } + + typedef int Flags; + struct Flag + { + enum Enum : Flags + { + Underscore = 0x1, + Dash = 0x2, + Upper = 0x4, + Lower = 0x8, + }; + }; + + Flags flags = 0; + for (const char c : slice) { switch (c) { - case '-': return NameConvention::Kabab; - case '_': return NameConvention::Snake; + case '-': flags |= Flag::Dash; break; + case '_': flags |= Flag::Underscore; break; + default: + { + if (CharUtil::isLower(c)) + { + flags |= Flag::Lower; + } + else if (CharUtil::isUpper(c)) + { + flags |= Flag::Upper; + } + else if (CharUtil::isDigit(c)) + { + // Is okay as long as not first char (which we already tested is alpha) + } + else + { + // Don't know what this style is + return NameConvention::Invalid; + } + } + } + } + + // Use flags to determine what convention is used + switch (flags) + { + // We'll assume it's lower camel. + case Flag::Lower: return NameConvention::LowerCamel; + // We'll assume it's upper snake. It almost certainly isn't camel, and snake is more usual + // than kabab. + case Flag::Upper: return NameConvention::UpperSnake; + case Flag::Upper | Flag::Lower: + { + // Looks like camel, choose the right case based on first char + return CharUtil::isUpper(slice[0]) ? NameConvention::UpperCamel : NameConvention::LowerCamel; + } + case Flag::Lower | Flag::Dash: return NameConvention::LowerKabab; + case Flag::Upper | Flag::Dash: return NameConvention::UpperKabab; + case Flag::Lower | Flag::Underscore: return NameConvention::LowerSnake; + case Flag::Upper | Flag::Underscore: return NameConvention::UpperSnake; + default: break; + } + + // Don't know what this style is + return NameConvention::Invalid; +} + +/* static */NameStyle NameConventionUtil::inferStyleFromText(const UnownedStringSlice& slice) +{ + for (const char c : slice) + { + switch (c) + { + case '-': return NameStyle::Kabab; + case '_': return NameStyle::Snake; default: break; } } - return NameConvention::Camel; + return NameStyle::Camel; } -/* static */void NameConventionUtil::split(NameConvention convention, const UnownedStringSlice& slice, List& out) +/* static */void NameConventionUtil::split(NameStyle style, const UnownedStringSlice& slice, List& out) { - switch (convention) + switch (style) { - case NameConvention::Kabab: + case NameStyle::Kabab: { StringUtil::split(slice, '-', out); break; } - case NameConvention::Snake: + case NameStyle::Snake: { StringUtil::split(slice, '_', out); break; } - case NameConvention::Camel: + case NameStyle::Camel: { typedef CharUtil::Flags CharFlags; typedef CharUtil::Flag CharFlag; @@ -87,15 +162,20 @@ namespace Slang } break; } + case NameStyle::Unknown: + { + out.add(slice); + break; + } } } void NameConventionUtil::split(const UnownedStringSlice& slice, List& out) { - split(getConvention(slice), slice, out); + split(inferStyleFromText(slice), slice, out); } -/* static */void NameConventionUtil::join(const UnownedStringSlice* slices, Index slicesCount, CharCase charCase, char joinChar, StringBuilder& out) +/* static */void NameConventionUtil::join(const UnownedStringSlice* slices, Index slicesCount, NameConvention convention, char joinChar, StringBuilder& out) { if (slicesCount <= 0) { @@ -111,6 +191,8 @@ void NameConventionUtil::split(const UnownedStringSlice& slice, List 0 && !(i == 0 && charCase == CharCase::Lower)) + if (count > 0 && !(i == 0 && isLower(convention))) { // Capitalize first letter of each word, unless on first word and 'lower' dst[j] = CharUtil::toUpper(src[j]); @@ -189,24 +268,27 @@ void NameConventionUtil::split(const UnownedStringSlice& slice, List slices; - split(fromConvention, slice, slices); + split(fromStyle, slice, slices); // Join the slices in the toConvention - join(slices.getBuffer(), slices.getCount(), charCase, toConvention, out); + join(slices.getBuffer(), slices.getCount(), toConvention, out); } -/* static */void NameConventionUtil::convert(const UnownedStringSlice& slice, CharCase charCase, NameConvention toConvention, StringBuilder& out) +/* static */void NameConventionUtil::convert(const UnownedStringSlice& slice, NameConvention toConvention, StringBuilder& out) { - convert(getConvention(slice), slice, charCase, toConvention, out); + convert(inferStyleFromText(slice), slice, toConvention, out); } } -- cgit v1.2.3