Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
5b95b5d7
Commit
5b95b5d7
authored
Nov 11, 2017
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Test compile-time errors
parent
246bdafc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
13 deletions
+58
-13
include/fmt/format.h
include/fmt/format.h
+16
-13
test/format-test.cc
test/format-test.cc
+42
-0
No files found.
include/fmt/format.h
View file @
5b95b5d7
...
@@ -1708,6 +1708,7 @@ typedef basic_format_specs<char> format_specs;
...
@@ -1708,6 +1708,7 @@ typedef basic_format_specs<char> format_specs;
namespace
internal
{
namespace
internal
{
struct
error_handler
{
struct
error_handler
{
// This function is intentionally not constexpr to give a compile-time error.
void
on_error
(
const
char
*
message
)
{
void
on_error
(
const
char
*
message
)
{
FMT_THROW
(
format_error
(
message
));
FMT_THROW
(
format_error
(
message
));
}
}
...
@@ -2586,10 +2587,11 @@ constexpr const Char *parse_format_specs(parse_context<Char> &ctx) {
...
@@ -2586,10 +2587,11 @@ constexpr const Char *parse_format_specs(parse_context<Char> &ctx) {
return
f
.
parse
(
ctx
);
return
f
.
parse
(
ctx
);
}
}
template
<
typename
Char
,
typename
...
Args
>
template
<
typename
Char
,
typename
ErrorHandler
,
typename
...
Args
>
struct
format_string_check
er
{
class
format_string_checker
:
public
ErrorHandl
er
{
public:
public:
explicit
constexpr
format_string_checker
(
const
Char
*
end
)
:
end_
(
end
)
{}
explicit
constexpr
format_string_checker
(
ErrorHandler
eh
,
const
Char
*
end
)
:
ErrorHandler
(
std
::
move
(
eh
)),
end_
(
end
)
{}
constexpr
void
on_text
(
const
Char
*
,
const
Char
*
)
{}
constexpr
void
on_text
(
const
Char
*
,
const
Char
*
)
{}
...
@@ -2610,13 +2612,10 @@ struct format_string_checker {
...
@@ -2610,13 +2612,10 @@ struct format_string_checker {
return
parse_funcs_
[
arg_index_
](
ctx
);
return
parse_funcs_
[
arg_index_
](
ctx
);
}
}
// This function is intentionally not constexpr to give a compile-time error.
void
on_error
(
const
char
*
);
private:
private:
constexpr
void
check_arg_index
()
{
constexpr
void
check_arg_index
()
{
if
(
arg_index_
<
0
||
arg_index_
>=
sizeof
...(
Args
))
if
(
arg_index_
<
0
||
arg_index_
>=
sizeof
...(
Args
))
on_error
(
"argument index out of range"
);
this
->
on_error
(
"argument index out of range"
);
}
}
// Format specifier parsing function.
// Format specifier parsing function.
...
@@ -2629,10 +2628,12 @@ struct format_string_checker {
...
@@ -2629,10 +2628,12 @@ struct format_string_checker {
};
};
};
};
template
<
typename
Char
,
typename
...
Args
>
template
<
typename
Char
,
typename
ErrorHandler
,
typename
...
Args
>
constexpr
bool
check_format_string
(
basic_string_view
<
Char
>
s
)
{
constexpr
bool
check_format_string
(
format_string_checker
<
Char
,
Args
...
>
checker
(
s
.
end
());
basic_string_view
<
Char
>
s
,
ErrorHandler
eh
=
ErrorHandler
())
{
internal
::
parse_format_string
(
s
.
begin
(),
checker
);
format_string_checker
<
Char
,
ErrorHandler
,
Args
...
>
checker
(
std
::
move
(
eh
),
s
.
end
());
parse_format_string
(
s
.
begin
(),
checker
);
return
true
;
return
true
;
}
}
...
@@ -3538,7 +3539,8 @@ template <typename String, typename... Args>
...
@@ -3538,7 +3539,8 @@ template <typename String, typename... Args>
inline
typename
std
::
enable_if
<
inline
typename
std
::
enable_if
<
std
::
is_base_of
<
internal
::
format_string
,
String
>::
value
,
std
::
string
>::
type
std
::
is_base_of
<
internal
::
format_string
,
String
>::
value
,
std
::
string
>::
type
format
(
String
format_str
,
const
Args
&
...
args
)
{
format
(
String
format_str
,
const
Args
&
...
args
)
{
constexpr
bool
invalid_format
=
internal
::
check_format_string
<
char
,
Args
...
>
(
constexpr
bool
invalid_format
=
internal
::
check_format_string
<
char
,
internal
::
error_handler
,
Args
...
>
(
string_view
(
format_str
.
value
(),
format_str
.
size
()));
string_view
(
format_str
.
value
(),
format_str
.
size
()));
return
vformat
(
format_str
.
value
(),
make_args
(
args
...));
return
vformat
(
format_str
.
value
(),
make_args
(
args
...));
}
}
...
@@ -3919,7 +3921,8 @@ class udl_formatter {
...
@@ -3919,7 +3921,8 @@ class udl_formatter {
template
<
typename
...
Args
>
template
<
typename
...
Args
>
std
::
basic_string
<
Char
>
operator
()(
const
Args
&
...
args
)
const
{
std
::
basic_string
<
Char
>
operator
()(
const
Args
&
...
args
)
const
{
constexpr
Char
s
[]
=
{
CHARS
...,
'\0'
};
constexpr
Char
s
[]
=
{
CHARS
...,
'\0'
};
constexpr
bool
invalid_format
=
check_format_string
<
Char
,
Args
...
>
(
constexpr
bool
invalid_format
=
check_format_string
<
Char
,
error_handler
,
Args
...
>
(
basic_string_view
<
Char
>
(
s
,
sizeof
...(
CHARS
)));
basic_string_view
<
Char
>
(
s
,
sizeof
...(
CHARS
)));
return
format
(
s
,
args
...);
return
format
(
s
,
args
...);
}
}
...
...
test/format-test.cc
View file @
5b95b5d7
...
@@ -1817,3 +1817,45 @@ TEST(FormatTest, UdlTemplate) {
...
@@ -1817,3 +1817,45 @@ TEST(FormatTest, UdlTemplate) {
EXPECT_EQ
(
" 42"
,
"{0:10}"
_format
(
42
));
EXPECT_EQ
(
" 42"
,
"{0:10}"
_format
(
42
));
EXPECT_EQ
(
"42"
,
fmt
::
format
(
FMT_STRING
(
"{}"
),
42
));
EXPECT_EQ
(
"42"
,
fmt
::
format
(
FMT_STRING
(
"{}"
),
42
));
}
}
struct
test_error_handler
{
const
char
*&
error
;
constexpr
void
on_error
(
const
char
*
message
)
{
error
=
message
;
}
};
constexpr
size_t
len
(
const
char
*
s
)
{
size_t
len
=
0
;
while
(
*
s
++
)
++
len
;
return
len
;
}
constexpr
bool
eq
(
const
char
*
s1
,
const
char
*
s2
)
{
if
(
!
s1
&&
!
s2
)
return
true
;
while
(
*
s1
&&
*
s1
==
*
s2
)
{
++
s1
;
++
s2
;
}
return
*
s1
==
*
s2
;
}
template
<
typename
...
Args
>
constexpr
bool
test_error
(
const
char
*
fmt
,
const
char
*
expected_error
)
{
const
char
*
actual_error
=
nullptr
;
test_error_handler
eh
{
actual_error
};
using
ref
=
std
::
reference_wrapper
<
test_error_handler
>
;
fmt
::
internal
::
check_format_string
<
char
,
test_error_handler
,
Args
...
>
(
string_view
(
fmt
,
len
(
fmt
)),
eh
);
return
eq
(
actual_error
,
expected_error
);
}
#define EXPECT_ERROR(fmt, error, ...) \
static_assert(test_error<__VA_ARGS__>(fmt, error), "")
TEST
(
FormatTest
,
FormatStringErrors
)
{
EXPECT_ERROR
(
"foo"
,
nullptr
);
EXPECT_ERROR
(
"}"
,
"unmatched '}' in format string"
);
EXPECT_ERROR
(
"{0:s"
,
"unknown format specifier"
,
Date
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment