diff options
author | Krasimir Georgiev <krasimir@google.com> | 2019-08-08 11:56:18 +0000 |
---|---|---|
committer | Krasimir Georgiev <krasimir@google.com> | 2019-08-08 11:56:18 +0000 |
commit | 9ab051bdda8de83df9abbaf00e76500875c3669e (patch) | |
tree | 93d08a71ab6f1f19c4162dd93110406ca0170633 | |
parent | 0de33de81338304492526053d0c1cfa616e58992 (diff) | |
download | bcm5719-llvm-9ab051bdda8de83df9abbaf00e76500875c3669e.tar.gz bcm5719-llvm-9ab051bdda8de83df9abbaf00e76500875c3669e.zip |
[clang-format] fix crash involving invalid preprocessor line
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
-rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 2 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 9 |
2 files changed, 11 insertions, 0 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 6e0369f27e7..cc0a954dbfb 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1099,6 +1099,8 @@ private: public: LineType parseLine() { + if (!CurrentToken) + return LT_Invalid; NonTemplateLess.clear(); if (CurrentToken->is(tok::hash)) return parsePreprocessorDirective(); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 528ad4b78c0..4e1b6f22bc3 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -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 |