Skip to content

Commit

Permalink
Merge remote-tracking branch 'rpavlik/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
pukkaone committed Jun 14, 2011
2 parents 71515bf + 9cc1ede commit 969cbdd
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 27 deletions.
56 changes: 37 additions & 19 deletions CMakeLists.txt
Expand Up @@ -18,27 +18,45 @@ enable_testing()
# Visual C++ Express Editions do not support solution folders.
set_property(GLOBAL PROPERTY USE_FOLDERS OFF)

# Patch LLVM CMake script to set USE_FOLDERS property to OFF.
configure_file(
patch/llvm/CMakeLists.txt
${CMAKE_SOURCE_DIR}/llvm/CMakeLists.txt
COPYONLY)
add_subdirectory(llvm)

set(CLANG_BUILD_STANDALONE 1
CACHE INTERNAL "Build clang as standalone.")
set(CLANG_PATH_TO_LLVM_SOURCE "${CMAKE_SOURCE_DIR}/llvm"
CACHE INTERNAL "Path to LLVM source directory.")
set(CLANG_PATH_TO_LLVM_BUILD "${CMAKE_BINARY_DIR}/llvm"
CACHE INTERNAL "Path to LLVM build directory.")
option(USE_INSTALLED_CLANG "Use an installed clang/llvm?" OFF)
if(USE_INSTALLED_CLANG)
find_package(LLVM REQUIRED)
include(AddLLVM)
macro(add_clang_executable name)
add_llvm_executable( ${name} ${ARGN} )
set_target_properties(${name} PROPERTIES FOLDER "Clang executables")
endmacro(add_clang_executable)
set(CLANG_INCLUDE_DIRS)
link_directories(${LLVM_LIBRARY_DIRS})
# Assume we're using newer than r130246 by default
option(CLANG_NEWER_THAN_R130246 "Is the version of Clang in use trunk r130246 or newer?" ON)
if(CLANG_NEWER_THAN_R130246)
add_definitions(-DCLANG_POST_R130246)
endif()
else()
# Patch LLVM CMake script to set USE_FOLDERS property to OFF.
configure_file(
patch/llvm/CMakeLists.txt
${CMAKE_SOURCE_DIR}/llvm/CMakeLists.txt
COPYONLY)
add_subdirectory(llvm)

# Patch clang CMake script to build it standalone.
configure_file(
patch/clang/CMakeLists.txt
${CMAKE_SOURCE_DIR}/clang/CMakeLists.txt
COPYONLY)
add_subdirectory(clang)
set(CLANG_BUILD_STANDALONE 1
CACHE INTERNAL "Build clang as standalone.")
set(CLANG_PATH_TO_LLVM_SOURCE "${CMAKE_SOURCE_DIR}/llvm"
CACHE INTERNAL "Path to LLVM source directory.")
set(CLANG_PATH_TO_LLVM_BUILD "${CMAKE_BINARY_DIR}/llvm"
CACHE INTERNAL "Path to LLVM build directory.")

# Patch clang CMake script to build it standalone.
configure_file(
patch/clang/CMakeLists.txt
${CMAKE_SOURCE_DIR}/clang/CMakeLists.txt
COPYONLY)
add_subdirectory(clang)
set(LLVM_INCLUDE_DIRS ${CMAKE_BINARY_DIR}/llvm/include ${CMAKE_SOURCE_DIR}/llvm/include)
set(CLANG_INCLUDE_DIRS ${CMAKE_BINARY_DIR}/clang/include ${CMAKE_SOURCE_DIR}/clang/include)
endif()
add_subdirectory(src)
add_subdirectory(test)

Expand Down
14 changes: 9 additions & 5 deletions src/CMakeLists.txt
Expand Up @@ -2,26 +2,30 @@

include(HandleLLVMOptions)

include_directories(
${CMAKE_BINARY_DIR}/llvm/include
${CMAKE_SOURCE_DIR}/llvm/include
${CMAKE_BINARY_DIR}/clang/include
${CMAKE_SOURCE_DIR}/clang/include
include_directories(${LLVM_INCLUDE_DIRS}
${CLANG_INCLUDE_DIRS}
${CMAKE_BINARY_DIR}/include
)

set(LLVM_USED_LIBS
clangFrontend
clangSerialization
clangDriver
clangSema
clangAnalysis
clangAST
clangParse
clangSema
clangLex
clangBasic
LLVMSupport
LLVMMC
)

add_clang_executable(find-unnecessary-includes
main.cpp
UnnecessaryIncludeFinder.cpp
)

install(TARGETS find-unnecessary-includes
RUNTIME DESTINATION bin)
32 changes: 29 additions & 3 deletions src/UnnecessaryIncludeFinder.cpp
Expand Up @@ -174,6 +174,21 @@ class PreprocessorCallbacks: public clang::PPCallbacks
delegate_(delegate)
{ }

#ifdef CLANG_POST_R130246
virtual void InclusionDirective(
clang::SourceLocation hashLoc,
const clang::Token& includeToken,
llvm::StringRef fileName,
bool isAngled,
const clang::FileEntry* pFile,
clang::SourceLocation endLoc,
llvm::StringRef SearchPath,
llvm::StringRef RelativePath)
{
delegate_.InclusionDirective(
hashLoc, includeToken, fileName, isAngled, pFile, endLoc, SearchPath, RelativePath);
}
#else
virtual void InclusionDirective (
SourceLocation hashLoc,
const clang::Token& includeToken,
Expand All @@ -186,7 +201,7 @@ class PreprocessorCallbacks: public clang::PPCallbacks
delegate_.InclusionDirective(
hashLoc, includeToken, fileName, isAngled, pFile, endLoc, rawPath);
}

#endif
virtual void FileChanged (
SourceLocation newLocation,
PPCallbacks::FileChangeReason reason,
Expand Down Expand Up @@ -248,7 +263,17 @@ UnnecessaryIncludeFinder::markUsed (
action_.allUsedHeaders_.insert(fileName);
}
}

#ifdef CLANG_POST_R130246
void UnnecessaryIncludeFinder::InclusionDirective(
clang::SourceLocation hashLoc,
const clang::Token& includeToken,
llvm::StringRef fileName,
bool isAngled,
const clang::FileEntry* pFile,
clang::SourceLocation endLoc,
llvm::StringRef SearchPath,
llvm::StringRef RelativePath)
#else
void
UnnecessaryIncludeFinder::InclusionDirective (
SourceLocation hashLoc,
Expand All @@ -258,6 +283,7 @@ UnnecessaryIncludeFinder::InclusionDirective (
const FileEntry* pFile,
SourceLocation endLoc,
const SmallVectorImpl<char>& rawPath)
#endif
{
std::string directiveLocation;
raw_string_ostream rso(directiveLocation);
Expand Down Expand Up @@ -370,7 +396,7 @@ UnnecessaryIncludeFinder::HandleTranslationUnit (ASTContext& astContext)
bool
UnnecessaryIncludeFinder::VisitTypedefTypeLoc (TypedefTypeLoc typeLoc)
{
markUsed(typeLoc.getTypedefDecl()->getLocation(), typeLoc.getBeginLoc());
markUsed(typeLoc.getTypePtr()->getDecl()->getLocation(), typeLoc.getBeginLoc());
return true;
}

Expand Down
12 changes: 12 additions & 0 deletions src/UnnecessaryIncludeFinder.h
Expand Up @@ -180,6 +180,17 @@ class UnnecessaryIncludeFinder:
*/
clang::PPCallbacks* createPreprocessorCallbacks();

#ifdef CLANG_POST_R130246
virtual void InclusionDirective(
clang::SourceLocation hashLoc,
const clang::Token& includeToken,
llvm::StringRef fileName,
bool isAngled,
const clang::FileEntry* pFile,
clang::SourceLocation endLoc,
llvm::StringRef SearchPath,
llvm::StringRef RelativePath);
#else
virtual void InclusionDirective(
clang::SourceLocation hashLoc,
const clang::Token& includeToken,
Expand All @@ -188,6 +199,7 @@ class UnnecessaryIncludeFinder:
const clang::FileEntry* pFile,
clang::SourceLocation endLoc,
const llvm::SmallVectorImpl<char>& rawPath);
#endif

virtual void FileChanged(
clang::SourceLocation newLocation,
Expand Down

0 comments on commit 969cbdd

Please sign in to comment.