Skip to content

Commit

Permalink
Make it compatible with Clang 17.
Browse files Browse the repository at this point in the history
  • Loading branch information
higuoxing committed Apr 13, 2024
1 parent 2c06088 commit 098c819
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Install LLVM and Clang
uses: KyleMayes/install-llvm-action@v1
with:
version: "15.0"
version: "17.0"
directory: ${{ runner.temp }}/llvm
- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
Expand Down
57 changes: 17 additions & 40 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,65 +1,42 @@
#===============================================================================
# This file is derived from: https://github.com/banach-space/clang-tutor
#===============================================================================
cmake_minimum_required(VERSION 3.13.4)
project(postgres-checkers)

#===============================================================================
# 0. GET CLANG INSTALLATION DIR
#===============================================================================
# In clang-tutor, `CT_Clang_INSTALL_DIR` is the key CMake variable - it points
# to a Clang installation directory. For the sake of completeness,
# <PackageName>_DIR (i.e. `Clang_DIR`) and <PackageName>_ROOT (i.e.
# `Clang_ROOT`) are also supported. Visit CMake documentation for more details:
# https://cmake.org/cmake/help/latest/command/find_package.html
# Use only _one_ of these variables.

if(DEFINED Clang_ROOT)
set(CT_CLANG_PACKAGE_DIR "${Clang_ROOT}/../../..")
elseif(DEFINED Clang_DIR)
set(CT_CLANG_PACKAGE_DIR "${Clang_DIR}/../../..")
find_program(LLVM_CONFIG "llvm-config")
if(${LLVM_CONFIG} STREQUAL LLVM_CONFIG-NOTFOUND)
message(FATAL_ERROR "llvm-config not found")
endif()
mark_as_advanced(CT_CLANG_PACKAGE_DIR)

# Set this to a valid Clang installation directory. This is most likely where
# LLVM is installed on your system.
set(CT_Clang_INSTALL_DIR "${CT_CLANG_PACKAGE_DIR}" CACHE PATH
"Clang installation directory")

#===============================================================================
# 1. VERIFY CLANG INSTALLATION DIR
#===============================================================================
set(CT_LLVM_INCLUDE_DIR "${CT_Clang_INSTALL_DIR}/include/llvm")
if(NOT EXISTS "${CT_LLVM_INCLUDE_DIR}")
message(FATAL_ERROR
" CT_Clang_INSTALL_DIR (${CT_LLVM_INCLUDE_DIR}) is invalid.")
execute_process(COMMAND llvm-config --includedir
OUTPUT_VARIABLE LLVM_INCLUDE_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT EXISTS "${LLVM_INCLUDE_DIR}/llvm")
message(FATAL_ERROR "LLVM header files not found")
endif()

set(CT_LLVM_CMAKE_FILE
"${CT_Clang_INSTALL_DIR}/lib/cmake/clang/ClangConfig.cmake")
if(NOT EXISTS "${CT_LLVM_CMAKE_FILE}")
execute_process(COMMAND llvm-config --cmakedir
OUTPUT_VARIABLE LLVM_CMAKE_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE)
set(LLVM_CMAKE_FILE
"${LLVM_CMAKE_DIR}/../clang/ClangConfig.cmake")
if(NOT EXISTS "${LLVM_CMAKE_FILE}")
message(FATAL_ERROR
" CT_LLVM_CMAKE_FILE (${CT_LLVM_CMAKE_FILE}) is invalid.")
"LLVM CMake files not found")
endif()

#===============================================================================
# 2. LOAD CLANG CONFIGURATION
# Extracted from:
# http://llvm.org/docs/CMake.html#embedding-llvm-in-your-project
#===============================================================================
list(APPEND CMAKE_PREFIX_PATH "${CT_Clang_INSTALL_DIR}/lib/cmake/clang/")
list(APPEND CMAKE_PREFIX_PATH "${LLVM_CMAKE_DIR}/clang/")

find_package(Clang REQUIRED CONFIG)

# Sanity check. As Clang does not expose e.g. `CLANG_VERSION_MAJOR` through
# AddClang.cmake, we have to use LLVM_VERSION_MAJOR instead.
# TODO: Revisit when next version is released.
if(NOT "15" VERSION_EQUAL "${LLVM_VERSION_MAJOR}")
message(FATAL_ERROR "Found LLVM ${LLVM_VERSION_MAJOR}, but need LLVM 15")
endif()

message(STATUS "Found Clang ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using ClangConfig.cmake in: ${CT_Clang_INSTALL_DIR}")
message(STATUS "Using ClangConfig.cmake in: ${LLVM_CMAKE_DIR}/clang/")

message("CLANG STATUS:
Includes (clang) ${CLANG_INCLUDE_DIRS}
Expand Down
4 changes: 2 additions & 2 deletions lib/ListFreeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SimpleListFreeChecker : public Checker<check::PreCall> {

} // end anonymous namespace

SimpleListFreeChecker::SimpleListFreeChecker() : FreeFn("pfree") {
SimpleListFreeChecker::SimpleListFreeChecker() : FreeFn({"pfree"}) {
// Initialize the bug types.
FreeListWithPFreeBugType.reset(
new BugType(this, "Applying pfree() on a list", "Postgres API Error"));
Expand Down Expand Up @@ -103,7 +103,7 @@ void SimpleListFreeChecker::reportInconsistentListFree(
// You can double check that it is working/found by listing the available
// checkers with the -analyzer-checker-help option.
extern "C" __attribute__((visibility("default")))
const char clang_analyzerAPIVersionString[] = "15.0.0";
const char clang_analyzerAPIVersionString[] = "17.0.0";

extern "C" __attribute__((visibility("default"))) void
clang_registerCheckers(CheckerRegistry &registry) {
Expand Down
7 changes: 1 addition & 6 deletions lib/ReturnInPgTryBlockChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,17 @@
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/Expr.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/Stmt.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchersInternal.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/raw_ostream.h"

#include <queue>
#include <unordered_set>
Expand Down Expand Up @@ -207,7 +202,7 @@ class ReturnInPgTryBlockChecker : public Checker<check::EndOfTranslationUnit> {
// Registration
//-----------------------------------------------------------------------------
extern "C" __attribute__((visibility("default")))
const char clang_analyzerAPIVersionString[] = "15.0.0";
const char clang_analyzerAPIVersionString[] = "17.0.0";

extern "C" __attribute__((visibility("default"))) void
clang_registerCheckers(CheckerRegistry &registry) {
Expand Down

0 comments on commit 098c819

Please sign in to comment.