From c21efa2a22a28bf4fab56b9468fa946149880bd0 Mon Sep 17 00:00:00 2001 From: lianghuajian Date: Sun, 4 Aug 2024 17:32:27 +0800 Subject: [PATCH] Support Xcode 15 on ARM64 Mac --- README.md | 7 +++ oclint-core/cmake/OCLintConfig.cmake | 12 ++++-- oclint-driver/include/oclint/ConfigFile.h | 18 ++++---- oclint-driver/lib/CompilerInstance.cpp | 3 +- oclint-driver/lib/ConfigFile.cpp | 32 +++++++------- oclint-driver/lib/Driver.cpp | 52 +++++++++++++++++++---- oclint-driver/lib/Options.cpp | 6 +-- 7 files changed, 89 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 855f94c0..18864288 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,10 @@ It looks for potential problems that aren't visible to compilers, for example: * ... For more information, visit https://oclint.org + +Clone the repository branch that supports Xcode 15 on arm64 Mac +``` +git clone -b support_xcode15 https://github.com/Lianghuajian/oclint.git oclint + +cd oclint/oclint-scripts && ./make && cd .. +``` \ No newline at end of file diff --git a/oclint-core/cmake/OCLintConfig.cmake b/oclint-core/cmake/OCLintConfig.cmake index 389400e4..373a819c 100644 --- a/oclint-core/cmake/OCLintConfig.cmake +++ b/oclint-core/cmake/OCLintConfig.cmake @@ -6,12 +6,12 @@ SET(CMAKE_BUILD_TYPE None) IF(CMAKE_CXX_COMPILER_ID MATCHES "Clang") SET(CMAKE_CXX_FLAGS "-fcolor-diagnostics") ENDIF() -SET(CMAKE_CXX_FLAGS "-std=c++14 ${CMAKE_CXX_LINKER_FLAGS} -fno-rtti -fPIC ${CMAKE_CXX_FLAGS}") +SET(CMAKE_CXX_FLAGS "-std=c++17 ${CMAKE_CXX_LINKER_FLAGS} -fno-rtti -fPIC ${CMAKE_CXX_FLAGS}") SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_CXX_LINKER_FLAGS} -fno-rtti") IF(APPLE) - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden -mmacosx-version-min=12.5") - SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -mmacosx-version-min=12.5") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden") + SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") ENDIF() IF(OCLINT_BUILD_TYPE STREQUAL "Release") @@ -22,6 +22,8 @@ ELSE() SET(CMAKE_SHARED_LINKER_FLAGS "-g ${CMAKE_SHARED_LINKER_FLAGS}") ENDIF() +find_package(Zstd REQUIRED) + SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) SET(OCLINT_VERSION_RELEASE "24.0") @@ -50,7 +52,8 @@ STRING(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" LLVM_VERSION_RELEASE ${LLVM_PAC MESSAGE(STATUS "Found LLVM LLVM_PACKAGE_VERSION: ${LLVM_PACKAGE_VERSION} - LLVM_VERSION_RELEASE: ${LLVM_VERSION_RELEASE}") MESSAGE(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") -LLVM_MAP_COMPONENTS_TO_LIBNAMES(REQ_LLVM_LIBRARIES asmparser bitreader instrumentation mcparser option support frontendopenmp) + +LLVM_MAP_COMPONENTS_TO_LIBNAMES(REQ_LLVM_LIBRARIES asmparser bitreader instrumentation mcparser option support frontendopenmp WindowsDriver) SET(CLANG_LIBRARIES clangToolingCore @@ -59,6 +62,7 @@ SET(CLANG_LIBRARIES clangDriver clangSerialization clangParse + clangSupport clangSema clangAnalysis clangEdit diff --git a/oclint-driver/include/oclint/ConfigFile.h b/oclint-driver/include/oclint/ConfigFile.h index 9d9533d0..a59e30de 100644 --- a/oclint-driver/include/oclint/ConfigFile.h +++ b/oclint-driver/include/oclint/ConfigFile.h @@ -3,7 +3,7 @@ #include -#include +// #include #include #include @@ -52,14 +52,14 @@ class ConfigFile const std::vector &disableRules() const; const std::vector &rulePaths() const; const std::vector &ruleConfigurations() const; - llvm::Optional output() const; - llvm::Optional reportType() const; - llvm::Optional maxP1() const; - llvm::Optional maxP2() const; - llvm::Optional maxP3() const; - llvm::Optional clangChecker() const; - llvm::Optional allowDuplicatedViolations() const; - llvm::Optional enableGlobalAnalysis() const; + std::optional output() const; + std::optional reportType() const; + std::optional maxP1() const; + std::optional maxP2() const; + std::optional maxP3() const; + std::optional clangChecker() const; + std::optional allowDuplicatedViolations() const; + std::optional enableGlobalAnalysis() const; void mapping(llvm::yaml::IO& io); }; diff --git a/oclint-driver/lib/CompilerInstance.cpp b/oclint-driver/lib/CompilerInstance.cpp index c56c751d..204f73cf 100644 --- a/oclint-driver/lib/CompilerInstance.cpp +++ b/oclint-driver/lib/CompilerInstance.cpp @@ -62,7 +62,8 @@ static clang::FrontendAction *getFrontendAction() { } void CompilerInstance::setupTarget() { - if ((getLangOpts().CUDA || getLangOpts().OpenMPIsDevice) && + // getLangOpts().OpenMPIsDevice is deparecated + if ((getLangOpts().CUDA) && !getFrontendOpts().AuxTriple.empty()) { auto targetOptions = std::make_shared(); diff --git a/oclint-driver/lib/ConfigFile.cpp b/oclint-driver/lib/ConfigFile.cpp index a33f3a5d..65a03a1e 100644 --- a/oclint-driver/lib/ConfigFile.cpp +++ b/oclint-driver/lib/ConfigFile.cpp @@ -131,66 +131,66 @@ const std::vector &oclint::option::ConfigFile::ruleConfig return _ruleConfigurations; } -static llvm::Optional createOptionalString(const llvm::StringRef &value) +static std::optional createOptionalString(const llvm::StringRef &value) { const std::string &string = value.str(); - return string.size() ? llvm::Optional(string) : llvm::Optional(); + return string.size() ? std::optional(string) : std::optional(); } -llvm::Optional oclint::option::ConfigFile::output() const +std::optional oclint::option::ConfigFile::output() const { return createOptionalString(_output); } -llvm::Optional oclint::option::ConfigFile::reportType() const +std::optional oclint::option::ConfigFile::reportType() const { return createOptionalString(_reportType); } -static llvm::Optional createOptionalInt(int value) +static std::optional createOptionalInt(int value) { - return value == INT_MIN ? llvm::Optional() : llvm::Optional(value); + return value == INT_MIN ? std::optional() : std::optional(value); } -llvm::Optional oclint::option::ConfigFile::maxP1() const +std::optional oclint::option::ConfigFile::maxP1() const { return createOptionalInt(_maxP1); } -llvm::Optional oclint::option::ConfigFile::maxP2() const +std::optional oclint::option::ConfigFile::maxP2() const { return createOptionalInt(_maxP2); } -llvm::Optional oclint::option::ConfigFile::maxP3() const +std::optional oclint::option::ConfigFile::maxP3() const { return createOptionalInt(_maxP3); } -static llvm::Optional createOptionalBool(const TriState value) +static std::optional createOptionalBool(const TriState value) { switch (value) { case FALSE: - return llvm::Optional(false); + return std::optional(false); case TRUE: - return llvm::Optional(true); + return std::optional(true); case UNDEFINED: - return llvm::Optional(); + return std::optional(); } } -llvm::Optional oclint::option::ConfigFile::clangChecker() const +std::optional oclint::option::ConfigFile::clangChecker() const { return createOptionalBool(_clangChecker); } -llvm::Optional oclint::option::ConfigFile::allowDuplicatedViolations() const +std::optional oclint::option::ConfigFile::allowDuplicatedViolations() const { return createOptionalBool(_allowDuplicatedViolations); } -llvm::Optional oclint::option::ConfigFile::enableGlobalAnalysis() const +std::optional oclint::option::ConfigFile::enableGlobalAnalysis() const { return createOptionalBool(_enableGlobalAnalysis); } diff --git a/oclint-driver/lib/Driver.cpp b/oclint-driver/lib/Driver.cpp index 1dcabc45..5ea8219b 100644 --- a/oclint-driver/lib/Driver.cpp +++ b/oclint-driver/lib/Driver.cpp @@ -48,6 +48,11 @@ #include +#include +#include +#include +#include + #include #include @@ -135,8 +140,9 @@ static const llvm::opt::ArgStringList *getCC1Arguments(clang::driver::Compilatio if (jobSize == 0) { + LOG_VERBOSE("Error: compilation contains no job"); throw oclint::GenericException("compilation contains no job:\n" + - compilationJobsToString(jobList) + "\n"); + compilationJobsToString(jobList) + "\n"); } bool offloadCompilation = false; @@ -155,24 +161,48 @@ static const llvm::opt::ArgStringList *getCC1Arguments(clang::driver::Compilatio } if (jobSize > 1 && !offloadCompilation) { + LOG_VERBOSE("Error: compilation contains multiple jobs"); throw oclint::GenericException("compilation contains multiple jobs:\n" + - compilationJobsToString(jobList) + "\n"); + compilationJobsToString(jobList) + "\n"); } if (!clang::isa(*jobList.begin())) { + LOG_VERBOSE("Error: compilation job does not contain correct command"); throw oclint::GenericException("compilation job does not contain correct command:\n" + - compilationJobsToString(jobList) + "\n"); + compilationJobsToString(jobList) + "\n"); } const clang::driver::Command &cmd = clang::cast(*jobList.begin()); if (llvm::StringRef(cmd.getCreator().getName()) != "clang") { + LOG_VERBOSE("Error: expected a command for clang compiler"); throw oclint::GenericException("expected a command for clang compiler"); } return &cmd.getArguments(); } +// Filter out arguments like -ivfsstatcache to prevent "compilation contains multiple jobs" error. +// See issue https://github.com/facebook/infer/issues/1749 +static void filterCommandLine(std::vector& commandLine) +{ + for (auto it = commandLine.begin(); it != commandLine.end();) + { + if (it->find("-ivfsstatcache") != std::string::npos) + { + it = commandLine.erase(it); // 删除 -ivfsstatcache + if (it != commandLine.end()) + { + it = commandLine.erase(it); // 删除紧随其后的路径参数 + } + } + else + { + ++it; // 移动到下一个元素 + } + } + +} static clang::CompilerInvocation *newCompilerInvocation( std::string &mainExecutable, @@ -181,6 +211,9 @@ static clang::CompilerInvocation *newCompilerInvocation( { assert(!commandLine.empty() && "Command line must not be empty!"); commandLine[0] = mainExecutable; + // Filter out arguments like -ivfsstatcache to prevent "compilation contains multiple jobs" error. + // See issue https://github.com/facebook/infer/issues/1749 + filterCommandLine(commandLine); std::vector argv; int start = 0, end = commandLine.size(); @@ -201,8 +234,7 @@ static clang::CompilerInvocation *newCompilerInvocation( argv.push_back("-D__OCLINT__"); // create diagnostic engine - llvm::IntrusiveRefCntPtr diagOpts = - new clang::DiagnosticOptions(); + llvm::IntrusiveRefCntPtr diagOpts = new clang::DiagnosticOptions(); clang::DiagnosticsEngine diagnosticsEngine( llvm::IntrusiveRefCntPtr(new clang::DiagnosticIDs()), &*diagOpts, @@ -210,28 +242,32 @@ static clang::CompilerInvocation *newCompilerInvocation( // create driver const char *const mainBinaryPath = argv[0]; - const std::unique_ptr driver( - newDriver(&diagnosticsEngine, mainBinaryPath)); + const std::unique_ptr driver(newDriver(&diagnosticsEngine, mainBinaryPath)); driver->setCheckInputsExist(false); // create compilation invocation const std::unique_ptr compilation( driver->BuildCompilation(llvm::makeArrayRef(argv))); auto cc1Args = getCC1Arguments(compilation.get()); + return newInvocation(&diagnosticsEngine, *cc1Args); } static oclint::CompilerInstance *newCompilerInstance(clang::CompilerInvocation *compilerInvocation, - bool runClangChecker = false) + bool runClangChecker = false) { + auto compilerInstance = new oclint::CompilerInstance(); auto invocation = std::make_shared(*compilerInvocation); compilerInstance->setInvocation(std::move(invocation)); compilerInstance->createDiagnostics(new DiagnosticDispatcher(runClangChecker)); + if (!compilerInstance->hasDiagnostics()) { + LOG_VERBOSE("Error: cannot create compiler diagnostics"); throw oclint::GenericException("cannot create compiler diagnostics"); } + return compilerInstance; } diff --git a/oclint-driver/lib/Options.cpp b/oclint-driver/lib/Options.cpp index 95fdac07..86221367 100644 --- a/oclint-driver/lib/Options.cpp +++ b/oclint-driver/lib/Options.cpp @@ -114,11 +114,11 @@ static std::string absoluteWorkingPath(""); static std::string executablePath(""); template -void updateArgIfSet(llvm::cl::opt &argValue, const llvm::Optional &configValue) +void updateArgIfSet(llvm::cl::opt &argValue, const std::optional &configValue) { - if (configValue.hasValue() && argValue.getNumOccurrences() == 0) + if (configValue.has_value() && argValue.getNumOccurrences() == 0) { - argValue.setValue(configValue.getValue()); + argValue.setValue(configValue.value()); } }