Permalink
Browse files

Mark usage of class template

  • Loading branch information...
1 parent 9119986 commit 4bc8e863dce662d94dbbcda72ecb384dcdf7d66b @pukkaone committed Apr 12, 2011
View
@@ -1,12 +1,22 @@
# $Id$
project(find-unnecessary-includes)
cmake_minimum_required(VERSION 2.8)
-enable_testing()
+
+set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
+include(configure_files)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
+if(CMAKE_GENERATOR MATCHES "Visual Studio.*")
+ # Disable compiler warnings.
+ add_definitions("-D_SCL_SECURE_NO_WARNINGS /wd4146 /wd4244 /wd4275 /wd4345 /wd4355 /wd4800")
+endif()
+
+# For make-based builds, defines target "test".
+enable_testing()
+
# Visual C++ 2008 Express Edition does not support solution folders.
set_property(GLOBAL PROPERTY USE_FOLDERS OFF)
@@ -33,3 +43,27 @@ add_subdirectory(clang)
add_subdirectory(src)
add_subdirectory(test)
+
+find_package(Git)
+if(GIT_FOUND)
+ # Get the version number from the git tag.
+ execute_process(
+ COMMAND ${GIT_EXECUTABLE} describe --abbrev=0
+ OUTPUT_VARIABLE FUI_VERSION
+ OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+ # Get the build number by counting commits from the first commit in the
+ # repository which is tagged "build".
+ execute_process(
+ COMMAND ${GIT_EXECUTABLE} describe --match build
+ OUTPUT_VARIABLE DESCRIBE_BUILD
+ OUTPUT_STRIP_TRAILING_WHITESPACE)
+ string(REGEX MATCH "[0-9]+" BUILD_NUMBER ${DESCRIBE_BUILD})
+else()
+ set(FUI_VERSION "0.0")
+ set(BUILD_NUMBER 0)
+endif()
+
+# Write version and build number into version.h file.
+configure_files(
+ ${CMAKE_SOURCE_DIR}/template/include ${CMAKE_BINARY_DIR}/include)
View
@@ -6,13 +6,13 @@ unnecessary `#include` directives in the main source file. Nested
considered.
-### Unnecessary #include directive
+### Unnecessary #include directives
Definition: An `#include` directive is *unnecessary* if the translation unit
compiles without errors after you remove it.
-### Optional #include directive
+### Optional #include directives
Definition: An `#include` directive is *optional* if removing it causes a
compile error but the compile error can be corrected by adding other `#include`
@@ -37,7 +37,8 @@ include `Base.h` instead.
### Check out source
-After cloning the project Git repository, run the commands:
+The tool uses clang to preprocess and parse C++ source code. Download the llvm
+and clang sources to the working tree by running the commands:
git submodule init
git submodule update
@@ -0,0 +1,21 @@
+# $Id$
+
+# Copy files from source directory to destination directory, substituting any
+# variables. Create destination directory if it does not exist.
+
+macro(configure_files srcDir destDir)
+ message(STATUS "Configuring directory ${destDir}")
+ file(MAKE_DIRECTORY ${destDir})
+
+ file(GLOB templateFiles RELATIVE ${srcDir} ${srcDir}/*)
+ foreach(templateFile ${templateFiles})
+ set(srcTemplatePath ${srcDir}/${templateFile})
+ if(NOT IS_DIRECTORY ${srcTemplatePath})
+ message(STATUS "Configuring file ${templateFile}")
+ configure_file(
+ ${srcTemplatePath}
+ ${destDir}/${templateFile}
+ @ONLY)
+ endif()
+ endforeach()
+endmacro()
View
@@ -14,6 +14,7 @@ include_directories(
${CMAKE_SOURCE_DIR}/llvm/include
${CMAKE_BINARY_DIR}/clang/include
${CMAKE_SOURCE_DIR}/clang/include
+ ${CMAKE_BINARY_DIR}/include
)
set(LLVM_USED_LIBS
View
@@ -221,6 +221,16 @@ UnusedHeaderFinder::VisitTagTypeLoc(TagTypeLoc typeLoc)
}
bool
+UnusedHeaderFinder::VisitTemplateSpecializationTypeLoc (
+ TemplateSpecializationTypeLoc typeLoc)
+{
+ markUsed(
+ typeLoc.getTypePtr()->getAsCXXRecordDecl()->getLocation(),
+ typeLoc.getTemplateNameLoc());
+ return true;
+}
+
+bool
UnusedHeaderFinder::VisitDeclRefExpr (DeclRefExpr* pExpr)
{
markUsed(pExpr->getDecl()->getLocation(), pExpr->getLocation());
View
@@ -133,6 +133,10 @@ class UnusedHeaderFinder:
// Called when a enum, struct or class is used.
bool VisitTagTypeLoc(clang::TagTypeLoc typeLoc);
+
+ // Called class template is used.
+ bool VisitTemplateSpecializationTypeLoc(
+ clang::TemplateSpecializationTypeLoc typeLoc);
// Called when a variable, function, or enum constant is used.
bool VisitDeclRefExpr(clang::DeclRefExpr* pExpr);
View
@@ -6,6 +6,7 @@
#include "clang/Lex/Preprocessor.h"
#include "llvm/Support/CommandLine.h"
#include "UnusedHeaderFinder.h"
+#include "version.h"
#include <algorithm>
#include <cstdlib>
#include <iostream>
@@ -15,18 +16,19 @@ using namespace llvm;
namespace {
+const std::string PROGRAM_NAME("find-unnecessary-includes");
+
void
showHelp ()
{
std::cout <<
- "USAGE: find-unnecessary-includes [options] <inputs>\n"
+ "USAGE: " << PROGRAM_NAME << " [options] <inputs>\n"
"\n"
"OPTIONS:\n"
" --help show help\n"
" --version show version\n"
" -D<macro>[=def] define preprocessor macro\n"
" -I<dir> add include directory\n"
- " -@<file> read options from file\n"
"\n"
"Many clang options are also supported. "
"See the clang manual for more options.\n";
@@ -37,7 +39,8 @@ handleFrontEndOptions (FrontendOptions& opt)
{
if (opt.ShowVersion)
{
- std::cout << "hello based on " << getClangFullVersion() << std::endl;
+ std::cout << PROGRAM_NAME << ' ' << FUI_VERSION
+ << "\nbased on " << getClangFullVersion() << std::endl;
return false;
}
@@ -0,0 +1,7 @@
+// $Id$
+#ifndef VERSION_H
+#define VERSION_H
+
+#define FUI_VERSION "@FUI_VERSION@.@BUILD_NUMBER@"
+
+#endif
View
@@ -5,12 +5,18 @@ typedef int Identifier;
enum Season { WINTER, SPRING, SUMMER, FALL };
-class Base
-{
+class Base {
public:
void memberFunction();
};
extern Base base;
+template<typename V>
+V
+max (V x, V y)
+{
+ return (x > y) ? x : y;
+}
+
#endif
View
@@ -15,6 +15,8 @@ macro(add_compare_test inputFile)
)
endmacro()
+add_compare_test(class-template-unused.cpp)
+add_compare_test(class-template-used.cpp)
add_compare_test(class-unused.cpp)
add_compare_test(class-used.cpp)
add_compare_test(enum-constant-unused.cpp)
View
@@ -0,0 +1,9 @@
+#ifndef LIST_H
+#define LIST_H
+
+template<typename E>
+class List {
+ E value;
+};
+
+#endif
@@ -0,0 +1,12 @@
+#include "List.h"
+
+namespace {
+
+template<typename E>
+class List {
+ E value;
+};
+
+List<int> list;
+
+}//namespace
@@ -0,0 +1 @@
+class-template-unused.cpp:1:1: warning: #include "List.h" is unnecessary
@@ -0,0 +1,3 @@
+#include "List.h"
+
+List<int> list;
No changes.
@@ -0,0 +1,14 @@
+#include "Base.h"
+
+namespace {
+
+template<typename V>
+V
+max (V v1, V v2)
+{
+ return (v1 > v2) ? v1 : v2;
+}
+
+int i = max(1, 2);
+
+}//namespace
@@ -0,0 +1,3 @@
+#include "Base.h"
+
+int i = max(1, 2);

0 comments on commit 4bc8e86

Please sign in to comment.