From 8e0df501faf82d32fd9af1971d93468b4773b16d Mon Sep 17 00:00:00 2001 From: Sam Panzer Date: Fri, 24 Aug 2012 23:29:33 +0000 Subject: [PATCH] Reverted to correct commit this time. llvm-svn: 162624 --- clang-tools-extra/CMakeLists.txt | 1 + clang-tools-extra/LICENSE.TXT | 63 +++++++++++ clang-tools-extra/Makefile | 40 +++++++ clang-tools-extra/README | 4 - clang-tools-extra/README.txt | 22 ++++ clang-tools-extra/remove-cstr-calls/Makefile | 13 ++- .../remove-cstr-calls/RemoveCStrCalls.cpp | 26 ++--- clang-tools-extra/test/Makefile | 38 +++---- .../tool-template/CMakeLists.txt | 6 + clang-tools-extra/tool-template/Makefile | 24 ++++ .../tool-template/ToolTemplate.cpp | 106 ++++++++++++++++++ 11 files changed, 297 insertions(+), 46 deletions(-) create mode 100644 clang-tools-extra/LICENSE.TXT create mode 100644 clang-tools-extra/Makefile delete mode 100644 clang-tools-extra/README create mode 100644 clang-tools-extra/README.txt create mode 100644 clang-tools-extra/tool-template/CMakeLists.txt create mode 100644 clang-tools-extra/tool-template/Makefile create mode 100644 clang-tools-extra/tool-template/ToolTemplate.cpp diff --git a/clang-tools-extra/CMakeLists.txt b/clang-tools-extra/CMakeLists.txt index 5876cb5c0402..e980b59f7974 100644 --- a/clang-tools-extra/CMakeLists.txt +++ b/clang-tools-extra/CMakeLists.txt @@ -1,4 +1,5 @@ add_subdirectory(remove-cstr-calls) +add_subdirectory(tool-template) # Add the common testsuite after all the tools. add_subdirectory(test) diff --git a/clang-tools-extra/LICENSE.TXT b/clang-tools-extra/LICENSE.TXT new file mode 100644 index 000000000000..6c224f84c5bb --- /dev/null +++ b/clang-tools-extra/LICENSE.TXT @@ -0,0 +1,63 @@ +============================================================================== +LLVM Release License +============================================================================== +University of Illinois/NCSA +Open Source License + +Copyright (c) 2007-2012 University of Illinois at Urbana-Champaign. +All rights reserved. + +Developed by: + + LLVM Team + + University of Illinois at Urbana-Champaign + + http://llvm.org + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimers. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimers in the + documentation and/or other materials provided with the distribution. + + * Neither the names of the LLVM Team, University of Illinois at + Urbana-Champaign, nor the names of its contributors may be used to + endorse or promote products derived from this Software without specific + prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE +SOFTWARE. + +============================================================================== +The LLVM software contains code written by third parties. Such software will +have its own individual LICENSE.TXT file in the directory in which it appears. +This file will describe the copyrights, license, and restrictions which apply +to that code. + +The disclaimer of warranty in the University of Illinois Open Source License +applies to all code in the LLVM Distribution, and nothing in any of the +other licenses gives permission to use the names of the LLVM Team or the +University of Illinois to endorse or promote products derived from this +Software. + +The following pieces of software have additional or alternate copyrights, +licenses, and/or restrictions: + +Program Directory +------- --------- + + diff --git a/clang-tools-extra/Makefile b/clang-tools-extra/Makefile new file mode 100644 index 000000000000..7950470145bd --- /dev/null +++ b/clang-tools-extra/Makefile @@ -0,0 +1,40 @@ +##===- tools/extra/Makefile --------------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +CLANG_LEVEL := ../.. + +include $(CLANG_LEVEL)/../../Makefile.config + +PARALLEL_DIRS := remove-cstr-calls tool-template + +include $(CLANG_LEVEL)/Makefile + +### +# Handle the nested test suite. + +ifneq ($(PROJ_SRC_ROOT),$(PROJ_OBJ_ROOT)) +$(RecursiveTargets):: + $(Verb) for dir in test; do \ + if [ -f $(PROJ_SRC_DIR)/$${dir}/Makefile ] && [ ! -f $${dir}/Makefile ]; then \ + $(MKDIR) $${dir}; \ + $(CP) $(PROJ_SRC_DIR)/$${dir}/Makefile $${dir}/Makefile; \ + fi \ + done +endif + +test:: + @ $(MAKE) -C test + +report:: + @ $(MAKE) -C test report + +clean:: + @ $(MAKE) -C test clean + +.PHONY: test report clean diff --git a/clang-tools-extra/README b/clang-tools-extra/README deleted file mode 100644 index 765d4dca71e3..000000000000 --- a/clang-tools-extra/README +++ /dev/null @@ -1,4 +0,0 @@ -This is the future home of the Clang tools "extra" repository, which will hold -tools above and beyond the core set that are inside Clang's repository. - -For now, this is a placeholder README as the repository gets set up. diff --git a/clang-tools-extra/README.txt b/clang-tools-extra/README.txt new file mode 100644 index 000000000000..2ed70d6531ae --- /dev/null +++ b/clang-tools-extra/README.txt @@ -0,0 +1,22 @@ +//===----------------------------------------------------------------------===// +// Clang Tools repository +//===----------------------------------------------------------------------===// + +Welcome to the repository of extra Clang Tools. This repository holds tools +that are developed as part of the LLVM compiler infrastructure project and the +Clang frontend. These tools are kept in a separate "extra" repository to +allow lighter weight checkouts of the core Clang codebase. + +This repository is only intended to be checked out inside of a full LLVM+Clang +tree, and in the 'tools/extra' subdirectory of the Clang checkout. + +All discussion regarding Clang, Clang-based tools, and code in this repository +should be held using the standard Clang mailing lists: + http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev + +Code review for this tree should take place on the standard Clang patch and +commit lists: + http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits + +If you find a bug in these tools, please file it in the LLVM bug tracker: + http://llvm.org/bugs/ diff --git a/clang-tools-extra/remove-cstr-calls/Makefile b/clang-tools-extra/remove-cstr-calls/Makefile index e08ee97d1184..80a4d0f5d4c6 100644 --- a/clang-tools-extra/remove-cstr-calls/Makefile +++ b/clang-tools-extra/remove-cstr-calls/Makefile @@ -1,4 +1,4 @@ -##===- tools/remove-cstr-calls/Makefile --------------------*- Makefile -*-===## +##===- tools/extra/remove-cstr-calls/Makefile --------------*- Makefile -*-===## # # The LLVM Compiler Infrastructure # @@ -7,7 +7,7 @@ # ##===----------------------------------------------------------------------===## -CLANG_LEVEL := ../.. +CLANG_LEVEL := ../../.. TOOLNAME = remove-cstr-calls NO_INSTALL = 1 @@ -15,9 +15,10 @@ NO_INSTALL = 1 # No plugins, optimize startup time. TOOL_NO_EXPORTS = 1 -LINK_COMPONENTS := support mc -USEDLIBS = clangEdit.a clangTooling.a clangFrontend.a clangSerialization.a clangDriver.a \ - clangRewrite.a clangParse.a clangSema.a clangAnalysis.a \ - clangAST.a clangASTMatchers.a clangLex.a clangBasic.a +include $(CLANG_LEVEL)/../../Makefile.config +LINK_COMPONENTS := $(TARGETS_TO_BUILD) asmparser support mc +USEDLIBS = clangTooling.a clangFrontend.a clangSerialization.a clangDriver.a \ + clangRewrite.a clangParse.a clangSema.a clangAnalysis.a \ + clangAST.a clangASTMatchers.a clangEdit.a clangLex.a clangBasic.a include $(CLANG_LEVEL)/Makefile diff --git a/clang-tools-extra/remove-cstr-calls/RemoveCStrCalls.cpp b/clang-tools-extra/remove-cstr-calls/RemoveCStrCalls.cpp index c9b16e022589..344fbfbe0cc1 100644 --- a/clang-tools-extra/remove-cstr-calls/RemoveCStrCalls.cpp +++ b/clang-tools-extra/remove-cstr-calls/RemoveCStrCalls.cpp @@ -191,8 +191,8 @@ int main(int argc, const char **argv) { ast_matchers::MatchFinder Finder; FixCStrCall Callback(&Tool.getReplacements()); Finder.addMatcher( - constructorCall( - hasDeclaration(method(hasName(StringConstructor))), + constructExpr( + hasDeclaration(methodDecl(hasName(StringConstructor))), argumentCountIs(2), // The first argument must have the form x.c_str() or p->c_str() // where the method is string::c_str(). We can use the copy @@ -200,23 +200,23 @@ int main(int argc, const char **argv) { // the string object). hasArgument( 0, - id("call", memberCall( - callee(id("member", memberExpression())), - callee(method(hasName(StringCStrMethod))), - on(id("arg", expression()))))), + id("call", memberCallExpr( + callee(id("member", memberExpr())), + callee(methodDecl(hasName(StringCStrMethod))), + on(id("arg", expr()))))), // The second argument is the alloc object which must not be // present explicitly. hasArgument( 1, - defaultArgument())), + defaultArgExpr())), &Callback); Finder.addMatcher( - constructorCall( + constructExpr( // Implicit constructors of these classes are overloaded // wrt. string types and they internally make a StringRef // referring to the argument. Passing a string directly to // them is preferred to passing a char pointer. - hasDeclaration(method(anyOf( + hasDeclaration(methodDecl(anyOf( hasName("::llvm::StringRef::StringRef"), hasName("::llvm::Twine::Twine")))), argumentCountIs(1), @@ -227,10 +227,10 @@ int main(int argc, const char **argv) { // directly. hasArgument( 0, - id("call", memberCall( - callee(id("member", memberExpression())), - callee(method(hasName(StringCStrMethod))), - on(id("arg", expression())))))), + id("call", memberCallExpr( + callee(id("member", memberExpr())), + callee(methodDecl(hasName(StringCStrMethod))), + on(id("arg", expr())))))), &Callback); return Tool.run(newFrontendActionFactory(&Finder)); } diff --git a/clang-tools-extra/test/Makefile b/clang-tools-extra/test/Makefile index 4fdafe1481b1..e75b68fe79c3 100644 --- a/clang-tools-extra/test/Makefile +++ b/clang-tools-extra/test/Makefile @@ -1,4 +1,13 @@ -CLANG_LEVEL := .. +##===- tools/extra/test/Makefile ---------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +CLANG_LEVEL := ../../.. include $(CLANG_LEVEL)/Makefile # Test in all immediate subdirectories if unset. @@ -29,42 +38,25 @@ ifdef VG LIT_ARGS += "--vg" endif -all:: lit.site.cfg Unit/lit.site.cfg - @ echo '--- Running clang tests for $(TARGET_TRIPLE) ---' +all:: lit.site.cfg + @ echo '--- Running the Clang extra tools tests for $(TARGET_TRIPLE) ---' @ $(PYTHON) $(LLVM_SRC_ROOT)/utils/lit/lit.py \ $(LIT_ARGS) $(TESTARGS) $(TESTDIRS) FORCE: lit.site.cfg: FORCE - @echo "Making Clang 'lit.site.cfg' file..." + @echo "Making Clang extra tools' 'lit.site.cfg' file..." @$(ECHOPATH) s=@LLVM_SOURCE_DIR@=$(LLVM_SRC_ROOT)=g > lit.tmp @$(ECHOPATH) s=@LLVM_BINARY_DIR@=$(LLVM_OBJ_ROOT)=g >> lit.tmp @$(ECHOPATH) s=@LLVM_TOOLS_DIR@=$(ToolDir)=g >> lit.tmp @$(ECHOPATH) s=@LLVM_LIBS_DIR@=$(LibDir)=g >> lit.tmp - @$(ECHOPATH) s=@CLANG_SOURCE_DIR@=$(PROJ_SRC_DIR)/..=g >> lit.tmp - @$(ECHOPATH) s=@CLANG_BINARY_DIR@=$(PROJ_OBJ_DIR)/..=g >> lit.tmp + @$(ECHOPATH) s=@CLANG_TOOLS_SOURCE_DIR@=$(PROJ_SRC_DIR)/..=g >> lit.tmp + @$(ECHOPATH) s=@CLANG_TOOLS_BINARY_DIR@=$(PROJ_OBJ_DIR)/..=g >> lit.tmp @$(ECHOPATH) s=@TARGET_TRIPLE@=$(TARGET_TRIPLE)=g >> lit.tmp @sed -f lit.tmp $(PROJ_SRC_DIR)/lit.site.cfg.in > $@ @-rm -f lit.tmp -Unit/lit.site.cfg: FORCE - @echo "Making Clang 'Unit/lit.site.cfg' file..." - @$(MKDIR) $(dir $@) - @$(ECHOPATH) s=@LLVM_SOURCE_DIR@=$(LLVM_SRC_ROOT)=g > unit.tmp - @$(ECHOPATH) s=@LLVM_BINARY_DIR@=$(LLVM_OBJ_ROOT)=g >> unit.tmp - @$(ECHOPATH) s=@LLVM_TOOLS_DIR@=$(ToolDir)=g >> unit.tmp - @$(ECHOPATH) s=@LLVM_LIBS_DIR@=$(LibDir)=g >> unit.tmp - @$(ECHOPATH) s=@CLANG_SOURCE_DIR@=$(PROJ_SRC_DIR)/..=g >> unit.tmp - @$(ECHOPATH) s=@CLANG_BINARY_DIR@=$(PROJ_OBJ_DIR)/..=g >> unit.tmp - @$(ECHOPATH) s=@TARGET_TRIPLE@=$(TARGET_TRIPLE)=g >> unit.tmp - @$(ECHOPATH) s=@LLVM_BUILD_MODE@=$(BuildMode)=g >> unit.tmp - @$(ECHOPATH) s=@ENABLE_SHARED@=$(ENABLE_SHARED)=g >> unit.tmp - @$(ECHOPATH) s=@SHLIBDIR@=$(SharedLibDir)=g >> unit.tmp - @$(ECHOPATH) s=@SHLIBPATH_VAR@=$(SHLIBPATH_VAR)=g >> unit.tmp - @sed -f unit.tmp $(PROJ_SRC_DIR)/Unit/lit.site.cfg.in > $@ - @-rm -f unit.tmp - clean:: @ find . -name Output | xargs rm -fr diff --git a/clang-tools-extra/tool-template/CMakeLists.txt b/clang-tools-extra/tool-template/CMakeLists.txt new file mode 100644 index 000000000000..cf14a9f524c4 --- /dev/null +++ b/clang-tools-extra/tool-template/CMakeLists.txt @@ -0,0 +1,6 @@ +add_clang_executable(tool-template + ToolTemplate.cpp + ) + +target_link_libraries(tool-template + clangEdit clangTooling clangBasic clangAST clangASTMatchers) diff --git a/clang-tools-extra/tool-template/Makefile b/clang-tools-extra/tool-template/Makefile new file mode 100644 index 000000000000..6126d3a4b398 --- /dev/null +++ b/clang-tools-extra/tool-template/Makefile @@ -0,0 +1,24 @@ +##===-------- tools/toolTemplate/Makefile ------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +CLANG_LEVEL := ../../.. + +TOOLNAME = tool-template +NO_INSTALL = 1 + +# No plugins, optimize startup time. +TOOL_NO_EXPORTS = 1 + +include $(CLANG_LEVEL)/../../Makefile.config +LINK_COMPONENTS := $(TARGETS_TO_BUILD) asmparser support mc +USEDLIBS = clangTooling.a clangFrontend.a clangSerialization.a clangDriver.a \ + clangRewrite.a clangParse.a clangSema.a clangAnalysis.a \ + clangAST.a clangASTMatchers.a clangEdit.a clangLex.a clangBasic.a + +include $(CLANG_LEVEL)/Makefile diff --git a/clang-tools-extra/tool-template/ToolTemplate.cpp b/clang-tools-extra/tool-template/ToolTemplate.cpp new file mode 100644 index 000000000000..93b018929a87 --- /dev/null +++ b/clang-tools-extra/tool-template/ToolTemplate.cpp @@ -0,0 +1,106 @@ +//===---- tools/extra/ToolTemplate.cpp - Template for refactoring tool ----===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements an empty refactoring tool using the clang tooling. +// The goal is to lower the "barrier to entry" for writing refactoring tools. +// +// Usage: +// tool-template ... +// +// Where is a CMake build directory in which a file named +// compile_commands.json exists (enable -DCMAKE_EXPORT_COMPILE_COMMANDS in +// CMake to get this output). +// +// ... specify the paths of files in the CMake source tree. This path +// is looked up in the compile command database. If the path of a file is +// absolute, it needs to point into CMake's source tree. If the path is +// relative, the current working directory needs to be in the CMake source +// tree and the file must be in a subdirectory of the current working +// directory. "./" prefixes in the relative files will be automatically +// removed, but the rest of a relative path must be a suffix of a path in +// the compile command line database. +// +// For example, to use tool-template on all files in a subtree of the +// source tree, use: +// +// /path/in/subtree $ find . -name '*.cpp'| +// xargs tool-template /path/to/build +// +//===----------------------------------------------------------------------===// + +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Frontend/FrontendActions.h" +#include "clang/Lex/Lexer.h" +#include "clang/Tooling/CompilationDatabase.h" +#include "clang/Tooling/Refactoring.h" +#include "clang/Tooling/Tooling.h" +#include "llvm/ADT/OwningPtr.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/MemoryBuffer.h" + +using namespace clang; +using namespace clang::ast_matchers; +using namespace clang::tooling; +using namespace llvm; + +namespace { +class ToolTemplateCallback : public MatchFinder::MatchCallback { + public: + ToolTemplateCallback(Replacements *Replace) : Replace(Replace) {} + + virtual void run(const MatchFinder::MatchResult &Result) { +// TODO: This routine will get called for each thing that the matchers find. +// At this point, you can examine the match, and do whatever you want, +// including replacing the matched text with other text + (void) Replace; // This to prevent an "unused member variable" warning; + } + + private: + Replacements *Replace; +}; +} // end anonymous namespace + +// Set up the command line options +cl::opt BuildPath( + cl::Positional, + cl::desc("")); + +cl::list SourcePaths( + cl::Positional, + cl::desc(" [... ]"), + cl::OneOrMore); + +int main(int argc, const char **argv) { + llvm::OwningPtr Compilations( + FixedCompilationDatabase::loadFromCommandLine(argc, argv)); + cl::ParseCommandLineOptions(argc, argv); + if (!Compilations) { // Couldn't find a compilation DB from the command line + std::string ErrorMessage; + Compilations.reset( + !BuildPath.empty() ? + CompilationDatabase::autoDetectFromDirectory(BuildPath, ErrorMessage) : + CompilationDatabase::autoDetectFromSource(SourcePaths[0], ErrorMessage) + ); + +// Still no compilation DB? - bail. + if (!Compilations) + llvm::report_fatal_error(ErrorMessage); + } + RefactoringTool Tool(*Compilations, SourcePaths); + ast_matchers::MatchFinder Finder; + ToolTemplateCallback Callback(&Tool.getReplacements()); + +// TODO: Put your matchers here. +// Use Finder.addMatcher(...) to define the patterns in the AST that you +// want to match against. You are not limited to just one matcher! + + return Tool.run(newFrontendActionFactory(&Finder)); +}