Skip to content

Commit

Permalink
[clang-format] fix crash involving invalid preprocessor line
Browse files Browse the repository at this point in the history
Summary:
This (invalid) fragment is crashing clang-format:
```
#if 1
int x;
#elif
int y;
#endif
```

The reason being that the parser expects a token after `#elif`, and the
subsequent parsing of the next line does not check if `CurrentToken` is null.

Reviewers: gribozavr

Reviewed By: gribozavr

Subscribers: cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D65940

llvm-svn: 368280
  • Loading branch information
krasimirgg committed Aug 8, 2019
1 parent 0de33de commit 9ab051b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,8 @@ class AnnotatingParser {

public:
LineType parseLine() {
if (!CurrentToken)
return LT_Invalid;
NonTemplateLess.clear();
if (CurrentToken->is(tok::hash))
return parsePreprocessorDirective();
Expand Down
9 changes: 9 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3087,6 +3087,15 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) {
"#endif\n"
"#endif\n",
Style);
// Don't crash if there is an #elif directive without a condition.
verifyFormat("#if 1\n"
"int x;\n"
"#elif\n"
"int y;\n"
"#else\n"
"int z;\n"
"#endif",
Style);
// FIXME: This doesn't handle the case where there's code between the
// #ifndef and #define but all other conditions hold. This is because when
// the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
Expand Down

0 comments on commit 9ab051b

Please sign in to comment.