Skip to content

Commit f5bafec

Browse files
author
Hal Finkel
committed
remember to commit the example source
1 parent 839f97a commit f5bafec

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
add_llvm_library(PrintTokensSyntax MODULE PrintTokensSyntax.cpp PLUGIN_TOOL clang)
2+
3+
if(LLVM_ENABLE_PLUGINS)
4+
set(LLVM_LINK_COMPONENTS
5+
Support
6+
)
7+
clang_target_link_libraries(PrintTokensSyntax PRIVATE
8+
clangAST
9+
clangBasic
10+
clangParse
11+
clangLex
12+
)
13+
endif()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//===- PrintTokensSyntax.cpp ----------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Example clang syntax plugin which enables the printing of all tokens in a
10+
// function body.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "clang/Parse/Parser.h"
15+
using namespace clang;
16+
17+
namespace {
18+
19+
class PrintTokensHandler : public SyntaxHandler {
20+
public:
21+
PrintTokensHandler() : SyntaxHandler("tokens") { }
22+
23+
void GetReplacement(Preprocessor &PP, Declarator &D,
24+
CachedTokens &Toks,
25+
llvm::raw_string_ostream &OS) override {
26+
for (auto &Tok : Toks) {
27+
OS << "printf(\"%s\\n\", \"";
28+
OS.write_escaped(PP.getSpelling(Tok));
29+
OS << "\");\n";
30+
}
31+
}
32+
33+
void AddToPredefines(llvm::raw_string_ostream &OS) override {
34+
OS << "#include <stdio.h>\n";
35+
}
36+
};
37+
38+
}
39+
40+
static SyntaxHandlerRegistry::Add<PrintTokensHandler>
41+
X("tokens", "collect all tokens");
42+

0 commit comments

Comments
 (0)