Skip to content

Commit 95054f1

Browse files
author
Jianchun Xu
committed
[MERGE #1847 @jianchun] swb,plugin: cleanup, support OSX and plugin arguments
Merge pull request #1847 from jianchun:wbarg Make the clang plugin work on OSX. OSX note: Must run the same clang binary for which the plugin was built for. e.g.: ``` ./build.sh -n -d --icu=/usr/local/opt/icu4c/include/ \ --cxx=/usr/local/opt/llvm/bin/clang++ \ --cc=/usr/local/opt/llvm/bin/clang \ --wb-check lib/Runtime/Base/FunctionBody.cpp ``` Clean up plugin build script and sources. Add plugin arguments support. Arguments can be directly passed from top level "build.sh". Changed --wb-check default output to "ERROR" only. To output verbose, use plugin argument "-verbose": ``` build.sh -n -d --wb-check lib/Runtime/Base/FunctionBody.cpp \ --wb-args=-verbose ```
2 parents 06c09ab + 6a2f715 commit 95054f1

File tree

13 files changed

+441
-315
lines changed

13 files changed

+441
-315
lines changed

CMakeLists.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,17 @@ endif(WITHOUT_FEATURES_SH)
310310

311311
enable_language(ASM)
312312

313+
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
314+
set(DYN_LIB_EXT "dylib")
315+
else()
316+
set(DYN_LIB_EXT "so")
317+
endif()
318+
313319
################# Write-barrier check/analyze ##################
314320
if (WB_CHECK_SH OR WB_ANALYZE_SH)
315321
add_definitions(
316322
-Xclang -load
317-
-Xclang ${CMAKE_CURRENT_SOURCE_DIR}/tools/RecyclerChecker/Build/libclangRecyclerChecker.so
323+
-Xclang ${CMAKE_CURRENT_SOURCE_DIR}/tools/RecyclerChecker/Build/libclangRecyclerChecker.${DYN_LIB_EXT}
318324
)
319325
endif()
320326
if (WB_CHECK_SH)
@@ -331,6 +337,15 @@ if (WB_ANALYZE_SH)
331337
-Xclang -analyzer-checker=chakra.RecyclerChecker
332338
)
333339
endif()
340+
if (WB_ARGS_SH)
341+
foreach(wb_arg IN LISTS WB_ARGS_SH)
342+
add_definitions(
343+
-Xclang -plugin-arg-check-recycler
344+
-Xclang ${wb_arg}
345+
)
346+
endforeach(wb_arg)
347+
unset(WB_ARGS_SH CACHE) # don't cache
348+
endif()
334349

335350
include_directories(
336351
.

bin/ChakraCore/CMakeLists.txt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,11 @@ endif()
6363
target_link_libraries (ChakraCore ${lib_target})
6464

6565
if(NOT CC_XCODE_PROJECT)
66-
set(CC_LIB_EXT "so")
6766
# Post build step to copy the built shared library
6867
# to BuildLinux (or whatever the CMakeBuildDir is)
69-
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
70-
set(CC_LIB_EXT "dylib")
71-
endif()
72-
7368
add_custom_command(TARGET ChakraCore POST_BUILD
7469
COMMAND ${CMAKE_COMMAND} -E copy_if_different
75-
"${CHAKRACORE_BINARY_DIR}/bin/ChakraCore/libChakraCore.${CC_LIB_EXT}"
70+
"${CHAKRACORE_BINARY_DIR}/bin/ChakraCore/libChakraCore.${DYN_LIB_EXT}"
7671
${CHAKRACORE_BINARY_DIR}/
7772
)
7873
endif()

build.sh

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ PRINT_USAGE() {
5353
echo " Write-barrier check given CPPFILE (git path)"
5454
echo " --wb-analyze CPPFILE"
5555
echo " Write-barrier analyze given CPPFILE (git path)"
56+
echo " --wb-args=PLUGIN_ARGS"
57+
echo " Write-barrier clang plugin args"
5658
echo ""
5759
echo "example:"
5860
echo " ./build.sh --cxx=/path/to/clang++ --cc=/path/to/clang -j"
@@ -81,6 +83,7 @@ OS_APT_GET=0
8183
OS_UNIX=0
8284
WB_CHECK=
8385
WB_ANALYZE=
86+
WB_ARGS=
8487

8588
if [ -f "/proc/version" ]; then
8689
OS_LINUX=1
@@ -258,6 +261,12 @@ while [[ $# -gt 0 ]]; do
258261
fi
259262
;;
260263

264+
--wb-args=*)
265+
WB_ARGS=$1
266+
WB_ARGS=${WB_ARGS:10}
267+
WB_ARGS=${WB_ARGS// /;} # replace space with ; to generate a cmake list
268+
;;
269+
261270
*)
262271
echo "Unknown option $1"
263272
PRINT_USAGE
@@ -349,6 +358,10 @@ if [[ $WB_CHECK || $WB_ANALYZE ]]; then
349358
WB_FILE=$WB_ANALYZE
350359
fi
351360

361+
if [[ $WB_ARGS ]]; then
362+
WB_ARGS="-DWB_ARGS_SH=$WB_ARGS"
363+
fi
364+
352365
if [[ -f $CHAKRACORE_DIR/$WB_FILE ]]; then
353366
touch $CHAKRACORE_DIR/$WB_FILE
354367
else
@@ -360,7 +373,7 @@ if [[ $WB_CHECK || $WB_ANALYZE ]]; then
360373

361374
WB_FILE_CMAKELISTS="$CHAKRACORE_DIR/$WB_FILE_DIR/CMakeLists.txt"
362375
if [[ -f $WB_FILE_CMAKELISTS ]]; then
363-
SUBDIR=$(grep -i add_library $WB_FILE_CMAKELISTS | sed -r "s/.*\((\S+) .*/\1/")
376+
SUBDIR=$(grep -i add_library $WB_FILE_CMAKELISTS | sed "s/.*(\(.*\) .*/\1/")
364377
else
365378
echo "$WB_FILE_CMAKELISTS not found." && exit 1
366379
fi
@@ -384,7 +397,7 @@ fi
384397
echo Generating $BUILD_TYPE makefiles
385398
cmake $CMAKE_GEN $CC_PREFIX $ICU_PATH $STATIC_LIBRARY $ARCH \
386399
-DCMAKE_BUILD_TYPE=$BUILD_TYPE $SANITIZE $NO_JIT $WITHOUT_FEATURES \
387-
$WB_FLAG \
400+
$WB_FLAG $WB_ARGS \
388401
../..
389402

390403
_RET=$?

tools/RecyclerChecker/CMakeLists.txt

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,33 @@ set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
66
project(clangRecyclerChecker CXX)
77

88
Find_Package(LLVM REQUIRED)
9-
link_directories( ${LLVM_LIB_DIR} )
9+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LLVM_COMPILE_FLAGS}")
10+
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LLVM_LDFLAGS}")
1011

1112
Find_Package(Clang REQUIRED)
1213
include_directories( ${CLANG_INCLUDE_DIRS} )
1314

1415
add_definitions("-fno-rtti -std=c++11")
15-
add_definitions("-D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS ")
16+
add_definitions("-D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS")
1617

17-
# target_link_libraries(clangRecyclerChecker ${CLANG_LIBS} ${LLVM_LIBS_CORE} )
18-
add_library(clangRecyclerChecker SHARED RecyclerChecker.cpp RecyclerAnalyzer.cpp)
18+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \
19+
-Wno-strict-aliasing \
20+
"
21+
)
1922

20-
# set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --unresolved-symbols=report-all")
21-
target_link_libraries(clangRecyclerChecker ${CLANG_LIBS})
23+
add_library(clangRecyclerChecker SHARED
24+
Helpers.cpp
25+
RecyclerChecker.cpp
26+
RecyclerAnalyzer.cpp
27+
)
28+
29+
# OSX requires following libs.
30+
# Ubuntu complains/fails if these libs specified.
31+
#
32+
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
33+
target_link_libraries(clangRecyclerChecker
34+
${CLANG_LIBS}
35+
${LLVM_LIBS_CORE}
36+
${LLVM_LIBS_SYS}
37+
)
38+
endif()

tools/RecyclerChecker/Helpers.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//-------------------------------------------------------------------------------------------------------
2+
// Copyright (C) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4+
//-------------------------------------------------------------------------------------------------------
5+
#include "Helpers.h"
6+
7+
// Default log level: Error
8+
Log::LogLevel Log::s_logLevel = Log::LogLevel::Error;

tools/RecyclerChecker/Helpers.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//-------------------------------------------------------------------------------------------------------
2+
// Copyright (C) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4+
//-------------------------------------------------------------------------------------------------------
5+
#pragma once
6+
7+
#include "llvm/Support/raw_ostream.h"
8+
using std::string;
9+
using llvm::raw_ostream;
10+
11+
class Log
12+
{
13+
public:
14+
enum class LogLevel { Error, Info, Verbose };
15+
16+
private:
17+
static LogLevel s_logLevel;
18+
19+
public:
20+
static void SetLevel(LogLevel level) { s_logLevel = level; }
21+
22+
static raw_ostream& errs()
23+
{
24+
return llvm::outs(); // use same outs stream for better output order
25+
}
26+
27+
static raw_ostream& outs()
28+
{
29+
return s_logLevel >= LogLevel::Info ? llvm::outs() : llvm::nulls();
30+
}
31+
32+
static raw_ostream& verbose()
33+
{
34+
return s_logLevel >= LogLevel::Verbose ? llvm::outs() : llvm::nulls();
35+
}
36+
};
37+
38+
template <size_t N>
39+
bool StartsWith(const string& s, const char (&prefix)[N])
40+
{
41+
return s.length() >= N - 1
42+
&& s.compare(0, string::npos, prefix, N - 1) == 0;
43+
}
44+
45+
template <class Set, class Item>
46+
bool Contains(const Set& s, const Item& item)
47+
{
48+
return s.find(item) != s.end();
49+
}

tools/RecyclerChecker/RecyclerAnalyzer.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,6 @@ class RecyclerChecker
3131
void checkPostStmt(const CXXNewExpr *newExpr, CheckerContext& ctx) const;
3232
};
3333

34-
/*
35-
llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const PHPNativeType &type) {
36-
os << type.getName() << " ";
37-
for (int i = 0; i < type.getPointerLevel(); ++i) {
38-
os << "*";
39-
}
40-
return os;
41-
}
42-
*/
43-
44-
static raw_ostream &debug_stream() {
45-
// #ifdef DEBUG_RECYCLER_CHECKER
46-
return llvm::outs();
47-
/*
48-
#else
49-
return llvm::nulls();
50-
#endif
51-
*/
52-
}
53-
5434
static BugType* createRecyclerCheckerError(StringRef name) {
5535
return new BugType(
5636
new CheckerBase(),
@@ -150,7 +130,7 @@ void RecyclerChecker::checkPostStmt(const CXXNewExpr* newExpr, CheckerContext& c
150130
}
151131

152132
static void initRecyclerChecker(CheckerManager &mgr) {
153-
RecyclerChecker *checker = mgr.registerChecker<RecyclerChecker>();
133+
mgr.registerChecker<RecyclerChecker>();
154134
}
155135

156136

0 commit comments

Comments
 (0)