Skip to content

Commit 5838320

Browse files
authored
fix build with clang -std=c++20 (#216)
* apply llvm-namespace-comment * Make jinja2cpp compilable under clang 12 with -std=c++20 the problem arose from the fact that libfmt switched to consteval check of format string(that is great, but makes us obliged to provide format_to with constexpr format_string or fmt::runtime overload) unfortunatelly I couldn't make it work with constexpr std::string so multistringliteral returns std::basic_string_view except for cases where it needs std::string fix #213
1 parent af50a9c commit 5838320

21 files changed

+90
-48
lines changed

.github/workflows/linux-build.yml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ name: CI-linux-build
33

44
on:
55
push:
6+
branches:
7+
- master
8+
- main
69
paths-ignore:
710
- 'docs/**'
811
- '**.md'
912
pull_request:
13+
branches:
14+
- master
15+
- main
1016
paths-ignore:
1117
- 'docs/**'
1218
- '**.md'
@@ -96,7 +102,7 @@ jobs:
96102
fail-fast: false
97103
max-parallel: 8
98104
matrix:
99-
compiler: [10, 11]
105+
compiler: [10, 11, 12]
100106
base-flags: ["", -DJINJA2CPP_CXX_STANDARD=17]
101107
build-config: [Release, Debug]
102108
build-shared: [TRUE, FALSE]
@@ -106,6 +112,9 @@ jobs:
106112
docker-image: conanio/clang10
107113
- compiler: 11
108114
docker-image: conanio/clang11
115+
- compiler: 12
116+
docker-image: conanio/clang12-ubuntu16.04:1.39.0
117+
109118

110119
steps:
111120
- uses: actions/checkout@v1
@@ -119,12 +128,17 @@ jobs:
119128
INPUT_BUILD_SHARED: ${{ matrix.build-shared }}
120129
HOME: /home/conan
121130
run: |
131+
#!/bin/bash
122132
set -ex
123133
export BUILD_TARGET=all
124134
export CMAKE_OPTS=-DCMAKE_VERBOSE_MAKEFILE=OFF
125135
export BUILD_CONFIG=${INPUT_BASE_CONFIG}
126136
export WORKSPACE=$GITHUB_WORKSPACE
127-
$CXX --version
137+
#if [ "${INPUT_COMPILER}" != "" ]; then export CXX=${INPUT_COMPILER}; fi
138+
if [ "${INPUT_COMPILER}" == "clang-12" ] ; then
139+
export INPUT_BASE_FLAGS="-DJINJA2CPP_CXX_STANDARD=20" ;
140+
fi
141+
#$CXX --version
128142
export EXTRA_FLAGS="${INPUT_BASE_FLAGS} ${INPUT_EXTRA_FLAGS}"
129143
mkdir $BUILD_DIRECTORY && cd $BUILD_DIRECTORY
130144
sudo chmod gou+rw -R $WORKSPACE

.github/workflows/windows-build.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ name: CI-windows-build
22

33
on:
44
push:
5+
branches:
6+
- master
7+
- main
58
paths-ignore:
69
- 'docs/**'
710
- '**.md'
811
pull_request:
12+
branches:
13+
- master
14+
- main
915
paths-ignore:
1016
- 'docs/**'
1117
- '**.md'

src/error_info.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ struct ValueRenderer
1818
{
1919
}
2020

21-
void operator()(bool val) const { fmt::format_to(ctx->out(), (val ? UNIVERSAL_STR("True") : UNIVERSAL_STR("False")).GetValue<CharT>()); }
21+
constexpr void operator()(bool val) const {
22+
fmt::format_to(
23+
ctx->out(),
24+
UNIVERSAL_STR("{}").GetValue<CharT>(),
25+
(val ? UNIVERSAL_STR("True").GetValue<CharT>(): UNIVERSAL_STR("False").GetValue<CharT>()));
26+
}
2227
void operator()(const jinja2::EmptyValue&) const { fmt::format_to(ctx->out(), UNIVERSAL_STR("").GetValue<CharT>()); }
2328
template<typename CharU>
2429
void operator()(const std::basic_string<CharU>& val) const
@@ -83,7 +88,7 @@ struct ValueRenderer
8388
fmt::format_to(ctx->out(), UNIVERSAL_STR("{}").GetValue<CharT>(), val);
8489
}
8590
};
86-
}
91+
} // namespace
8792

8893
namespace fmt
8994
{
@@ -103,7 +108,7 @@ struct formatter<jinja2::Value, CharT>
103108
return fmt::format_to(ctx.out(), UNIVERSAL_STR("").GetValue<CharT>());
104109
}
105110
};
106-
}
111+
} // namespace fmt
107112

108113
namespace jinja2
109114
{
@@ -281,4 +286,4 @@ std::wostream& operator << (std::wostream& os, const ErrorInfoW& res)
281286
os << res.ToString();
282287
return os;
283288
}
284-
} // jinja2
289+
} // namespace jinja2

src/expression_evaluator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,5 +596,5 @@ CallParams EvaluateCallParams(const CallParamsInfo& info, RenderContext& context
596596
return result;
597597
}
598598

599-
}
600-
}
599+
} // namespace helpers
600+
} // namespace jinja2

src/expression_parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,4 +603,4 @@ ExpressionParser::ParseResult<ExpressionEvaluatorPtr<IfExpression>> ExpressionPa
603603
return result;
604604
}
605605

606-
} // jinja2
606+
} // namespace jinja2

src/filesystem_handler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,4 @@ CharFileStreamPtr RealFileSystem::OpenByteStream(const std::string& name) const
144144
return CharFileStreamPtr(nullptr, [](std::istream*){});
145145
}
146146

147-
} // jinja2
147+
} // namespace jinja2

src/filters.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,5 +1082,5 @@ InternalValue UserDefinedFilter::Filter(const InternalValue& baseVal, RenderCont
10821082
return callable->GetExpressionCallable()(callParams, context);
10831083
}
10841084

1085-
} // filters
1086-
} // jinja2
1085+
} // namespace filters
1086+
} // namespace jinja2

src/helpers.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,27 @@ struct MultiStringLiteral
1515
const char* charValue;
1616
const wchar_t* wcharValue;
1717

18+
constexpr MultiStringLiteral(const char* val, const wchar_t* wval)
19+
: charValue(val)
20+
, wcharValue(wval)
21+
{
22+
}
23+
24+
template<typename CharT>
25+
constexpr auto GetValue() const
26+
{
27+
#if __cplusplus < 202002L
28+
return GetValueStr<CharT>();
29+
#else
30+
constexpr auto memPtr = SelectMemberPtr<CharT, &MultiStringLiteral::charValue, &MultiStringLiteral::wcharValue>::GetPtr();
31+
return nonstd::basic_string_view<CharT>(this->*memPtr);
32+
#endif
33+
}
34+
1835
template<typename CharT>
19-
auto GetValue() const
36+
constexpr auto GetValueStr() const
2037
{
21-
auto memPtr = SelectMemberPtr<CharT, &MultiStringLiteral::charValue, &MultiStringLiteral::wcharValue>::GetPtr();
38+
constexpr auto memPtr = SelectMemberPtr<CharT, &MultiStringLiteral::charValue, &MultiStringLiteral::wcharValue>::GetPtr();
2239
return std::basic_string<CharT>(this->*memPtr);
2340
}
2441

@@ -28,13 +45,13 @@ struct MultiStringLiteral
2845
template<const char* (MultiStringLiteral::*charMemPtr), const wchar_t* (MultiStringLiteral::*wcharMemPtr)>
2946
struct SelectMemberPtr<char, charMemPtr, wcharMemPtr>
3047
{
31-
static auto GetPtr() {return charMemPtr;}
48+
static constexpr auto GetPtr() {return charMemPtr;}
3249
};
3350

3451
template<const char* (MultiStringLiteral::*charMemPtr), const wchar_t* (MultiStringLiteral::*wcharMemPtr)>
3552
struct SelectMemberPtr<wchar_t, charMemPtr, wcharMemPtr>
3653
{
37-
static auto GetPtr() {return wcharMemPtr;}
54+
static constexpr auto GetPtr() {return wcharMemPtr;}
3855
};
3956

4057
template<typename CharT>

src/internal_value.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,6 @@ InputValueConvertor::result_t InputValueConvertor::ConvertUserCallable(const Use
859859
}));
860860
}
861861

862-
} // visitors
862+
} // namespace visitors
863863

864-
} // jinja2
864+
} // namespace jinja2

src/lexer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,4 @@ bool Lexer::ProcessString(const lexertk::token&, Token& newToken)
119119
return true;
120120
}
121121

122-
} // jinja2
122+
} // namespace jinja2

src/serialize_filters.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ struct FormatArgumentConverter : visitors::BaseVisitor<FormatArgument>
241241
bool m_named = false;
242242
};
243243

244-
}
244+
} // namespace
245245

246246
InternalValue StringFormat::Filter(const InternalValue& baseVal, RenderContext& context)
247247
{
@@ -437,5 +437,5 @@ InternalValue XmlAttrFilter::Filter(const InternalValue& baseVal, RenderContext&
437437
return Apply<XmlAttrPrinter>(baseVal, &context, true);
438438
}
439439

440-
}
441-
}
440+
} // namespace filters
441+
} // namespace jinja2

src/statements.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,4 +798,4 @@ void FilterStatement::Render(OutStream& os, RenderContext& values)
798798
const auto result = m_expr->Evaluate(std::move(arg), values);
799799
os.WriteValue(result);
800800
}
801-
} // jinja2
801+
} // namespace jinja2

src/string_converter_filter.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -352,17 +352,17 @@ InternalValue StringConverter::Filter(const InternalValue& baseVal, RenderContex
352352
auto str = sv_to_string(srcStr);
353353
using StringT = decltype(str);
354354
using CharT = typename StringT::value_type;
355-
static const std::basic_regex<CharT> STRIPTAGS_RE(UNIVERSAL_STR("(<!--.*?-->|<[^>]*>)").GetValue<CharT>());
356-
str = std::regex_replace(str, STRIPTAGS_RE, UNIVERSAL_STR("").GetValue<CharT>());
355+
static const std::basic_regex<CharT> STRIPTAGS_RE(UNIVERSAL_STR("(<!--.*?-->|<[^>]*>)").GetValueStr<CharT>());
356+
str = std::regex_replace(str, STRIPTAGS_RE, UNIVERSAL_STR("").GetValueStr<CharT>());
357357
ba::trim_all(str);
358358
static const StringT html_entities [] {
359-
UNIVERSAL_STR("&amp;").GetValue<CharT>(), UNIVERSAL_STR("&").GetValue<CharT>(),
360-
UNIVERSAL_STR("&apos;").GetValue<CharT>(), UNIVERSAL_STR("\'").GetValue<CharT>(),
361-
UNIVERSAL_STR("&gt;").GetValue<CharT>(), UNIVERSAL_STR(">").GetValue<CharT>(),
362-
UNIVERSAL_STR("&lt;").GetValue<CharT>(), UNIVERSAL_STR("<").GetValue<CharT>(),
363-
UNIVERSAL_STR("&quot;").GetValue<CharT>(), UNIVERSAL_STR("\"").GetValue<CharT>(),
364-
UNIVERSAL_STR("&#39;").GetValue<CharT>(), UNIVERSAL_STR("\'").GetValue<CharT>(),
365-
UNIVERSAL_STR("&#34;").GetValue<CharT>(), UNIVERSAL_STR("\"").GetValue<CharT>(),
359+
UNIVERSAL_STR("&amp;").GetValueStr<CharT>(), UNIVERSAL_STR("&").GetValueStr<CharT>(),
360+
UNIVERSAL_STR("&apos;").GetValueStr<CharT>(), UNIVERSAL_STR("\'").GetValueStr<CharT>(),
361+
UNIVERSAL_STR("&gt;").GetValueStr<CharT>(), UNIVERSAL_STR(">").GetValueStr<CharT>(),
362+
UNIVERSAL_STR("&lt;").GetValueStr<CharT>(), UNIVERSAL_STR("<").GetValueStr<CharT>(),
363+
UNIVERSAL_STR("&quot;").GetValueStr<CharT>(), UNIVERSAL_STR("\"").GetValueStr<CharT>(),
364+
UNIVERSAL_STR("&#39;").GetValueStr<CharT>(), UNIVERSAL_STR("\'").GetValueStr<CharT>(),
365+
UNIVERSAL_STR("&#34;").GetValueStr<CharT>(), UNIVERSAL_STR("\"").GetValueStr<CharT>(),
366366
};
367367
for (auto it = std::begin(html_entities), end = std::end(html_entities); it < end; it += 2)
368368
{
@@ -391,5 +391,5 @@ InternalValue StringConverter::Filter(const InternalValue& baseVal, RenderContex
391391
return std::move(result);
392392
}
393393

394-
}
395-
}
394+
} // namespace filters
395+
} // namespace jinja2

src/template.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,4 @@ ResultW<MetadataInfo<wchar_t>> TemplateW::GetMetadataRaw()
168168
// GetImpl<wchar_t>(m_impl)->GetMetadataRaw();
169169
;
170170
}
171-
} // jinga2
171+
} // namespace jinja2

src/template_env.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,4 @@ nonstd::expected<TemplateW, ErrorInfoW> TemplateEnv::LoadTemplateW(std::string f
9292
return LoadTemplateImpl<wchar_t>(this, std::move(fileName), m_filesystemHandlers, m_templateWCache);
9393
}
9494

95-
} // jinja2
95+
} // namespace jinja2

src/template_parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,4 +968,4 @@ StatementsParser::ParseResult StatementsParser::ParseEndFilter(LexScanner&, Stat
968968
return {};
969969
}
970970

971-
}
971+
} // namespace jinja2

src/template_parser.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ template<>
5959
struct ParserTraits<char> : public ParserTraitsBase<>
6060
{
6161
static std::regex GetRoughTokenizer()
62-
{ return std::regex(s_regexp.GetValue<char>()); }
62+
{ return std::regex(s_regexp.GetValueStr<char>()); }
6363
static std::regex GetKeywords()
6464
{
6565
std::string pattern;
@@ -110,7 +110,7 @@ template<>
110110
struct ParserTraits<wchar_t> : public ParserTraitsBase<>
111111
{
112112
static std::wregex GetRoughTokenizer()
113-
{ return std::wregex(s_regexp.GetValue<wchar_t>()); }
113+
{ return std::wregex(s_regexp.GetValueStr<wchar_t>()); }
114114
static std::wregex GetKeywords()
115115
{
116116
std::wstring pattern;
@@ -748,10 +748,10 @@ class TemplateParser : public LexerHelper
748748
{
749749
auto p = traits_t::s_tokens.find(tok.type);
750750
if (p != traits_t::s_tokens.end())
751-
return p->second.template GetValue<CharT>();
751+
return p->second.template GetValueStr<CharT>();
752752

753753
if (tok.range.size() != 0)
754-
return m_template->substr(tok.range.startOffset, tok.range.size());
754+
return string_t(m_template->substr(tok.range.startOffset, tok.range.size()));
755755
else if (tok.type == Token::Identifier)
756756
{
757757
if (!tok.value.IsEmpty())
@@ -760,10 +760,10 @@ class TemplateParser : public LexerHelper
760760
return GetAsSameString(tpl, tok.value).value_or(std::basic_string<CharT>());
761761
}
762762

763-
return UNIVERSAL_STR("<<Identifier>>").template GetValue<CharT>();
763+
return UNIVERSAL_STR("<<Identifier>>").template GetValueStr<CharT>();
764764
}
765765
else if (tok.type == Token::String)
766-
return UNIVERSAL_STR("<<String>>").template GetValue<CharT>();
766+
return UNIVERSAL_STR("<<String>>").template GetValueStr<CharT>();
767767

768768
return string_t();
769769
}

src/testers.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,5 +375,5 @@ bool UserDefinedTester::Test(const InternalValue& baseVal, RenderContext& contex
375375

376376
return ConvertToBool(callable->GetExpressionCallable()(callParams, context));
377377
}
378-
}
379-
}
378+
} // namespace testers
379+
} // namespace jinja2

test/filters_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ struct TypeReflection<TestValues> : TypeReflected<TestValues>
708708
}
709709
};
710710

711-
}
711+
} // namespace jinja2
712712

713713
TEST_F(XmlAttr, SerializeMapWithStringViewsAndNoneSerializebleValues)
714714
{

test/rapid_json_serializer_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jinja2::InternalValue MakeInternalValue(T&& v)
1010
{
1111
return jinja2::InternalValue(std::forward<T>(v));
1212
}
13-
}
13+
} // namespace
1414
TEST(RapidJsonSerializerTest, SerializeTrivialTypes)
1515
{
1616
const jinja2::rapidjson_serializer::DocumentWrapper document;

test/tojson_filter_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void PrintTo(const nlohmann::json& json, std::ostream* os)
7070
{
7171
*os << json.dump();
7272
}
73-
}
73+
} // namespace nlohmann
7474

7575
TEST_F(ToJson, SerializeObject)
7676
{

0 commit comments

Comments
 (0)