From fbe5672746bed3920afc6265f5b8a0bce0f5e70b Mon Sep 17 00:00:00 2001 From: Dmitri Gribenko Date: Thu, 13 Feb 2014 16:51:38 +0000 Subject: [PATCH] libclang: fix a bug in processing invalid arguments, introduced in r201249, pointed out by Daniel Jasper in r201329 llvm-svn: 201346 --- clang/tools/libclang/CIndex.cpp | 17 +++++++---------- clang/unittests/CMakeLists.txt | 1 + clang/unittests/Makefile | 2 +- clang/unittests/libclang/CMakeLists.txt | 7 +++++++ clang/unittests/libclang/LibclangTest.cpp | 17 +++++++++++++++++ clang/unittests/libclang/Makefile | 25 +++++++++++++++++++++++++ 6 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 clang/unittests/libclang/CMakeLists.txt create mode 100644 clang/unittests/libclang/LibclangTest.cpp create mode 100644 clang/unittests/libclang/Makefile diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp index a74feab..e862889 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -2686,6 +2686,11 @@ static void clang_parseTranslationUnit_Impl(void *UserData) { unsigned options = PTUI->options; CXTranslationUnit *out_TU = PTUI->out_TU; + // Set up the initial return values. + if (out_TU) + *out_TU = NULL; + PTUI->result = CXError_Failure; + // Check arguments. if (!CIdx || !out_TU || (unsaved_files == NULL && num_unsaved_files != 0)) { @@ -2693,10 +2698,6 @@ static void clang_parseTranslationUnit_Impl(void *UserData) { return; } - // Set up the initial return values. - *out_TU = NULL; - PTUI->result = CXError_Failure; - CIndexer *CXXIdx = static_cast(CIdx); if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing)) @@ -2826,12 +2827,8 @@ clang_parseTranslationUnit(CXIndex CIdx, CIdx, source_filename, command_line_args, num_command_line_args, unsaved_files, num_unsaved_files, options, &TU); (void)Result; - - // FIXME: This probably papers over a problem. If the result is not success, - // no TU should be set. - if (Result != CXError_Success) - return 0; - + assert((TU && Result == CXError_Success) || + (!TU && Result != CXError_Success)); return TU; } diff --git a/clang/unittests/CMakeLists.txt b/clang/unittests/CMakeLists.txt index f00008e..9cb0800 100644 --- a/clang/unittests/CMakeLists.txt +++ b/clang/unittests/CMakeLists.txt @@ -22,3 +22,4 @@ if(CLANG_ENABLE_REWRITER) add_subdirectory(Format) add_subdirectory(Sema) endif() +add_subdirectory(libclang) diff --git a/clang/unittests/Makefile b/clang/unittests/Makefile index e4fbe58..9b95a6e 100644 --- a/clang/unittests/Makefile +++ b/clang/unittests/Makefile @@ -14,7 +14,7 @@ ifndef CLANG_LEVEL IS_UNITTEST_LEVEL := 1 CLANG_LEVEL := .. -PARALLEL_DIRS = Basic Lex Driver +PARALLEL_DIRS = Basic Lex Driver libclang include $(CLANG_LEVEL)/../..//Makefile.config diff --git a/clang/unittests/libclang/CMakeLists.txt b/clang/unittests/libclang/CMakeLists.txt new file mode 100644 index 0000000..1cdc45e2 --- /dev/null +++ b/clang/unittests/libclang/CMakeLists.txt @@ -0,0 +1,7 @@ +add_clang_unittest(libclangTests + LibclangTest.cpp + ) + +target_link_libraries(libclangTests + libclang + ) diff --git a/clang/unittests/libclang/LibclangTest.cpp b/clang/unittests/libclang/LibclangTest.cpp new file mode 100644 index 0000000..7048af9 --- /dev/null +++ b/clang/unittests/libclang/LibclangTest.cpp @@ -0,0 +1,17 @@ +//===- unittests/libclang/LibclangTest.cpp --- libclang tests -------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "clang-c/Index.h" +#include "gtest/gtest.h" + +TEST(libclang, TestInvalidArgs) { + EXPECT_EQ(CXError_InvalidArguments, + clang_parseTranslationUnit2(0, 0, 0, 0, 0, 0, 0, 0)); +} + diff --git a/clang/unittests/libclang/Makefile b/clang/unittests/libclang/Makefile new file mode 100644 index 0000000..e96c925 --- /dev/null +++ b/clang/unittests/libclang/Makefile @@ -0,0 +1,25 @@ +##===- unittests/libclang/Makefile -------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +CLANG_LEVEL = ../.. +TESTNAME = libclang +include $(CLANG_LEVEL)/../../Makefile.config +LINK_COMPONENTS := $(TARGETS_TO_BUILD) asmparser bitreader support mc option + +# Note that 'USEDLIBS' must include all of the core clang libraries +# when -static is given to linker on cygming. +USEDLIBS = clang.a \ + clangIndex.a clangFormat.a clangRewriteCore.a \ + clangFrontend.a clangDriver.a \ + clangTooling.a \ + clangSerialization.a clangParse.a clangSema.a \ + clangAnalysis.a clangEdit.a clangAST.a clangLex.a \ + clangBasic.a + +include $(CLANG_LEVEL)/unittests/Makefile -- 2.7.4