Skip to content

Commit

Permalink
[pseudo] Strip comments for TokenStream.
Browse files Browse the repository at this point in the history
Add a utility function to strip comments from a "raw" tokenstream. The
derived stream will be fed to the GLR parser (for early testing).

Differential Revision: https://reviews.llvm.org/D121092
  • Loading branch information
hokein committed Mar 7, 2022
1 parent 6d9eb7e commit 2d01ac1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions clang/include/clang/Tooling/Syntax/Pseudo/Token.h
Expand Up @@ -195,6 +195,9 @@ enum class LexFlags : uint8_t {
/// (And having cooked token kinds in PP-disabled sections is useful for us).
TokenStream cook(const TokenStream &, const clang::LangOptions &);

/// Drops comment tokens.
TokenStream stripComments(const TokenStream &);

} // namespace pseudo
} // namespace syntax
} // namespace clang
Expand Down
11 changes: 11 additions & 0 deletions clang/lib/Tooling/Syntax/Pseudo/Token.cpp
Expand Up @@ -93,6 +93,17 @@ void TokenStream::print(llvm::raw_ostream &OS) const {
OS << '\n';
}

TokenStream stripComments(const TokenStream &Input) {
TokenStream Out;
for (const Token &T : Input.tokens()) {
if (T.Kind == tok::comment)
continue;
Out.push(T);
}
Out.finalize();
return Out;
}

} // namespace pseudo
} // namespace syntax
} // namespace clang
17 changes: 17 additions & 0 deletions clang/unittests/Tooling/Syntax/Pseudo/TokenTest.cpp
Expand Up @@ -172,6 +172,23 @@ no_indent \
}));
}

TEST(TokenTest, DropComments) {
LangOptions Opts;
std::string Code = R"cpp(
// comment
int /*abc*/;
)cpp";
TokenStream Raw = cook(lex(Code, Opts), Opts);
TokenStream Stripped = stripComments(Raw);
EXPECT_THAT(Raw.tokens(),
ElementsAreArray(
{token("// comment", tok::comment), token("int", tok::kw_int),
token("/*abc*/", tok::comment), token(";", tok::semi)}));

EXPECT_THAT(Stripped.tokens(), ElementsAreArray({token("int", tok::kw_int),
token(";", tok::semi)}));
}

} // namespace
} // namespace pseudo
} // namespace syntax
Expand Down

0 comments on commit 2d01ac1

Please sign in to comment.