]> granicus.if.org Git - clang/commitdiff
libclang: ensure clang_createTranslationUnit2 always initializes *TU
authorDmitri Gribenko <gribozavr@gmail.com>
Wed, 19 Feb 2014 10:24:00 +0000 (10:24 +0000)
committerDmitri Gribenko <gribozavr@gmail.com>
Wed, 19 Feb 2014 10:24:00 +0000 (10:24 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@201657 91177308-0d34-0410-b5e6-96231b3b80d8

tools/libclang/CIndex.cpp
unittests/libclang/LibclangTest.cpp

index c5f41a8a7422e1447484bf81f1a18aaf2a486509..f217d795dc938bdbc7964943a6c2916dfaad1703 100644 (file)
@@ -2612,7 +2612,7 @@ void clang_toggleCrashRecovery(unsigned isEnabled) {
 
 CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
                                               const char *ast_filename) {
-  CXTranslationUnit TU = NULL;
+  CXTranslationUnit TU;
   enum CXErrorCode Result =
       clang_createTranslationUnit2(CIdx, ast_filename, &TU);
   (void)Result;
@@ -2624,6 +2624,9 @@ CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
 enum CXErrorCode clang_createTranslationUnit2(CXIndex CIdx,
                                               const char *ast_filename,
                                               CXTranslationUnit *out_TU) {
+  if (out_TU)
+    *out_TU = NULL;
+
   if (!CIdx || !ast_filename || !out_TU)
     return CXError_InvalidArguments;
 
index 7048af95842d70f00c5c2d63c0ff5934233842e4..4278d10a1f80366978841189426baf9171ab24be 100644 (file)
 #include "clang-c/Index.h"
 #include "gtest/gtest.h"
 
-TEST(libclang, TestInvalidArgs) {
+TEST(libclang, clang_parseTranslationUnit2_InvalidArgs) {
   EXPECT_EQ(CXError_InvalidArguments,
             clang_parseTranslationUnit2(0, 0, 0, 0, 0, 0, 0, 0));
 }
 
+TEST(libclang, clang_createTranslationUnit_InvalidArgs) {
+  EXPECT_EQ(0, clang_createTranslationUnit(0, 0));
+}
+
+TEST(libclang, clang_createTranslationUnit2_InvalidArgs) {
+  EXPECT_EQ(CXError_InvalidArguments,
+            clang_createTranslationUnit2(0, 0, 0));
+
+  CXTranslationUnit TU = reinterpret_cast<CXTranslationUnit>(1);
+  EXPECT_EQ(CXError_InvalidArguments,
+            clang_createTranslationUnit2(0, 0, &TU));
+  EXPECT_EQ(0, TU);
+}