Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
45c15d87
Commit
45c15d87
authored
Apr 21, 2015
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Playing around with some clang plugins
parent
a1100ede
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
112 additions
and
3 deletions
+112
-3
.gitignore
.gitignore
+2
-0
Makefile
Makefile
+24
-3
plugins/clang_capi.cpp
plugins/clang_capi.cpp
+86
-0
No files found.
.gitignore
View file @
45c15d87
...
...
@@ -47,6 +47,8 @@ gmon.out
find_problem.status
*.expected_cache
plugins/clang_capi
*.so
*.pch
...
...
Makefile
View file @
45c15d87
...
...
@@ -1182,6 +1182,27 @@ $(FROM_CPYTHON_SRCS:.c=.prof.o): %.prof.o: %.c $(BUILD_SYSTEM_DEPS)
$(ECHO)
Compiling C file to
$@
$(VERB)
$(CC_PROFILE)
$(EXT_CFLAGS_PROFILE)
-c
$<
-o
$@
-g
-MMD
-MP
-MF
$(
patsubst
%.o,%.d,
$@
)
# These are necessary until we support unicode:
../from_cpython/Modules/_sre.o
:
EXT_CFLAGS += -Wno-sometimes-uninitialized
../from_cpython/Modules/_sre.release.o
:
EXT_CFLAGS += -Wno-sometimes-uninitialized
# TESTING:
_plugins/clang_capi.so
:
plugins/clang_capi.cpp $(BUILD_SYSTEM_DEPS)
@
#
$(CXX)
$<
-o
$@
-c
-I
/usr/lib/llvm-3.5/include
-std
=
c++11
-D__STDC_CONSTANT_MACROS
-D__STDC_LIMIT_MACROS
$(CXX)
$<
-o
$@
-std
=
c++11
$(LLVM_CXXFLAGS)
-I
$(LLVM_SRC)
/tools/clang/include
-I
$(LLVM_BUILD)
/tools/clang/include
-shared
plugins/clang_capi.o
:
plugins/clang_capi.cpp $(BUILD_SYSTEM_DEPS)
@
#
$(CXX)
$<
-o
$@
-c
-I
/usr/lib/llvm-3.5/include
-std
=
c++11
-D__STDC_CONSTANT_MACROS
-D__STDC_LIMIT_MACROS
$(CXX)
$<
-o
$@
-std
=
c++11
$(LLVM_CXXFLAGS)
-O0
-I
$(LLVM_SRC)
/tools/clang/include
-I
$(LLVM_BUILD)
/tools/clang/include
-c
plugins/clang_capi
:
plugins/clang_capi.o $(BUILD_SYSTEM_DEPS)
@
#
$(CXX)
$<
-o
$@
-c
-I
/usr/lib/llvm-3.5/include
-std
=
c++11
-D__STDC_CONSTANT_MACROS
-D__STDC_LIMIT_MACROS
$(CXX)
$<
-o
$@
-L
$(LLVM_BUILD)
/Release/lib
-lclangASTMatchers
-lclangRewrite
-lclangFrontend
-lclangDriver
-lclangTooling
-lclangParse
-lclangSema
-lclangAnalysis
-lclangAST
-lclangEdit
-lclangLex
-lclangBasic
-lclangSerialization
$(
shell
$(LLVM_BUILD)
/Release+Asserts/bin/llvm-config
--ldflags
--system-libs
--libs
all
)
plugins/clang_capi.so
:
plugins/clang_capi.o $(BUILD_SYSTEM_DEPS)
@
#
$(CXX)
$<
-o
$@
-c
-I
/usr/lib/llvm-3.5/include
-std
=
c++11
-D__STDC_CONSTANT_MACROS
-D__STDC_LIMIT_MACROS
$(CXX)
$<
-o
$@
-shared
.PHONY
:
plugin_test
plugin_test
:
plugins/clang_capi.so
$(CLANG_CXX)
-Xclang
-load
-Xclang
plugins/clang_capi.so
-Xclang
-add-plugin
-Xclang
print-fns
test
/test.cpp
-c
-S
-o
-
.PHONY
:
tool_test
tool_test
:
plugins/clang_capi
plugins/clang_capi
test
/test.cpp
--
plugins/clang_capi.cpp
0 → 100644
View file @
45c15d87
//------------------------------------------------------------------------------
// Tooling sample. Demonstrates:
//
// * How to write a simple source tool using libTooling.
// * How to use RecursiveASTVisitor to find interesting AST nodes.
// * How to use the Rewriter API to rewrite the source code.
//
// Eli Bendersky (eliben@gmail.com)
// This code is in the public domain
//------------------------------------------------------------------------------
#include <sstream>
#include <string>
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Frontend/ASTConsumers.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "llvm/Support/raw_ostream.h"
using
namespace
llvm
;
using
namespace
clang
;
using
namespace
clang
::
driver
;
using
namespace
clang
::
tooling
;
using
namespace
clang
::
ast_matchers
;
static
llvm
::
cl
::
OptionCategory
ToolingSampleCategory
(
"Tooling Sample"
);
//auto matcher = memberExpr().bind("member_expr");
auto
matcher
=
memberExpr
(
member
(
fieldDecl
(
hasName
(
"ob_refcnt"
)).
bind
(
"field"
))).
bind
(
"member_expr"
);
MatchFinder
Finder
;
Rewriter
TheRewriter
;
class
Replacer
:
public
MatchFinder
::
MatchCallback
{
public:
virtual
void
run
(
const
MatchFinder
::
MatchResult
&
Result
)
{
errs
()
<<
"matched!
\n
"
;
if
(
const
MemberExpr
*
ME
=
Result
.
Nodes
.
getNodeAs
<
clang
::
MemberExpr
>
(
"member_expr"
))
{
ME
->
dump
();
//cast<FieldDecl>(ME->getMemberDecl())->dump();
TheRewriter
.
InsertTextBefore
(
ME
->
getSourceRange
().
getBegin
(),
"/* Pyston change, was: "
);
TheRewriter
.
InsertTextAfter
(
ME
->
getSourceRange
().
getEnd
(),
"*/ 2"
);
for
(
auto
c
:
ME
->
Stmt
::
children
())
{
c
->
dump
();
}
}
if
(
const
FieldDecl
*
ME
=
Result
.
Nodes
.
getNodeAs
<
clang
::
FieldDecl
>
(
"field"
))
{
ME
->
dump
();
TheRewriter
.
InsertTextAfter
(
ME
->
getLocStart
(),
"/**/"
);
}
//Result.dump();
}
};
class
MyFrontendAction
:
public
ASTFrontendAction
{
public:
MyFrontendAction
()
{}
void
EndSourceFileAction
()
override
{
SourceManager
&
SM
=
TheRewriter
.
getSourceMgr
();
// Now emit the rewritten buffer.
TheRewriter
.
getEditBuffer
(
SM
.
getMainFileID
()).
write
(
llvm
::
outs
());
}
std
::
unique_ptr
<
ASTConsumer
>
CreateASTConsumer
(
CompilerInstance
&
CI
,
StringRef
file
)
override
{
TheRewriter
.
setSourceMgr
(
CI
.
getSourceManager
(),
CI
.
getLangOpts
());
return
Finder
.
newASTConsumer
();
}
};
int
main
(
int
argc
,
const
char
**
argv
)
{
CommonOptionsParser
op
(
argc
,
argv
,
ToolingSampleCategory
);
ClangTool
Tool
(
op
.
getCompilations
(),
op
.
getSourcePathList
());
Replacer
replacer
;
Finder
.
addMatcher
(
matcher
,
&
replacer
);
return
Tool
.
run
(
newFrontendActionFactory
<
MyFrontendAction
>
().
get
());
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment