diff options
author | Vy Nguyen <vyng@google.com> | 2020-07-09 23:19:06 -0400 |
---|---|---|
committer | Vy Nguyen <vyng@google.com> | 2020-07-10 21:31:16 -0400 |
commit | 17ea41e472566823e16d3a04661221fbd18d9fae (patch) | |
tree | 64f5aaa441a1c5ce14216b9211cecb271f5a1484 /clang/unittests/AST/SourceLocationTest.cpp | |
parent | b59c6fcaf3fc8fd4c42daeecf0545e47b37b1aa7 (diff) | |
download | llvm-17ea41e472566823e16d3a04661221fbd18d9fae.zip llvm-17ea41e472566823e16d3a04661221fbd18d9fae.tar.gz llvm-17ea41e472566823e16d3a04661221fbd18d9fae.tar.bz2 |
Summary: [clang] Provide a way for WhileStmt to report the location of its LParen and RParen.
Summary: This helps avoiding hacks downstream.
Reviewers: shafik
Subscribers: martong, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D83529
Diffstat (limited to 'clang/unittests/AST/SourceLocationTest.cpp')
-rw-r--r-- | clang/unittests/AST/SourceLocationTest.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/clang/unittests/AST/SourceLocationTest.cpp b/clang/unittests/AST/SourceLocationTest.cpp index cb96afe..32dc382 100644 --- a/clang/unittests/AST/SourceLocationTest.cpp +++ b/clang/unittests/AST/SourceLocationTest.cpp @@ -60,6 +60,59 @@ TEST(RangeVerifier, WrongRange) { EXPECT_FALSE(Verifier.match("int i;", varDecl())); } +class WhileParenLocationVerifier : public MatchVerifier<WhileStmt> { + unsigned ExpectLParenLine = 0, ExpectLParenColumn = 0; + unsigned ExpectRParenLine = 0, ExpectRParenColumn = 0; + +public: + void expectLocations(unsigned LParenLine, unsigned LParenColumn, + unsigned RParenLine, unsigned RParenColumn) { + ExpectLParenLine = LParenLine; + ExpectLParenColumn = LParenColumn; + ExpectRParenLine = RParenLine; + ExpectRParenColumn = RParenColumn; + } + +protected: + void verify(const MatchFinder::MatchResult &Result, + const WhileStmt &Node) override { + SourceLocation LParenLoc = Node.getLParenLoc(); + SourceLocation RParenLoc = Node.getRParenLoc(); + unsigned LParenLine = + Result.SourceManager->getSpellingLineNumber(LParenLoc); + unsigned LParenColumn = + Result.SourceManager->getSpellingColumnNumber(LParenLoc); + unsigned RParenLine = + Result.SourceManager->getSpellingLineNumber(RParenLoc); + unsigned RParenColumn = + Result.SourceManager->getSpellingColumnNumber(RParenLoc); + + if (LParenLine != ExpectLParenLine || LParenColumn != ExpectLParenColumn || + RParenLine != ExpectRParenLine || RParenColumn != ExpectRParenColumn) { + std::string MsgStr; + llvm::raw_string_ostream Msg(MsgStr); + Msg << "Expected LParen Location <" << ExpectLParenLine << ":" + << ExpectLParenColumn << ">, found <"; + LParenLoc.print(Msg, *Result.SourceManager); + Msg << ">\n"; + + Msg << "Expected RParen Location <" << ExpectRParenLine << ":" + << ExpectRParenColumn << ">, found <"; + RParenLoc.print(Msg, *Result.SourceManager); + Msg << ">"; + + this->setFailure(Msg.str()); + } + } +}; + +TEST(LocationVerifier, WhileParenLoc) { + WhileParenLocationVerifier Verifier; + Verifier.expectLocations(1, 17, 1, 38); + EXPECT_TRUE(Verifier.match("void f() { while(true/*some comment*/) {} }", + whileStmt())); +} + class LabelDeclRangeVerifier : public RangeVerifier<LabelStmt> { protected: SourceRange getRange(const LabelStmt &Node) override { |