r201155 - libclang: audit all APIs that accept a CXTranslationUnit to make sure that
Dmitri Gribenko
gribozavr at gmail.com
Tue Feb 11 06:34:14 PST 2014
Author: gribozavr
Date: Tue Feb 11 08:34:14 2014
New Revision: 201155
URL: http://llvm.org/viewvc/llvm-project?rev=201155&view=rev
Log:
libclang: audit all APIs that accept a CXTranslationUnit to make sure that
checks for invalid translation unit are in place. Also, while there, add log
output for this case.
Modified:
cfe/trunk/tools/libclang/CIndex.cpp
cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp
cfe/trunk/tools/libclang/CIndexDiagnostic.cpp
cfe/trunk/tools/libclang/CIndexHigh.cpp
cfe/trunk/tools/libclang/CIndexInclusionStack.cpp
cfe/trunk/tools/libclang/CXSourceLocation.cpp
cfe/trunk/tools/libclang/CXTranslationUnit.h
cfe/trunk/tools/libclang/Indexing.cpp
Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=201155&r1=201154&r2=201155&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Tue Feb 11 08:34:14 2014
@@ -2845,8 +2845,10 @@ int clang_saveTranslationUnit(CXTranslat
*Log << TU << ' ' << FileName;
}
- if (!TU)
+ if (isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
return CXSaveError_InvalidTU;
+ }
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
ASTUnit::ConcurrencyCheck Check(*CXXUnit);
@@ -2916,9 +2918,13 @@ struct ReparseTranslationUnitInfo {
static void clang_reparseTranslationUnit_Impl(void *UserData) {
ReparseTranslationUnitInfo *RTUI =
static_cast<ReparseTranslationUnitInfo*>(UserData);
+ RTUI->result = 1; // Error.
+
CXTranslationUnit TU = RTUI->TU;
- if (!TU)
+ if (isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
return;
+ }
// Reset the associated diagnostics.
delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics);
@@ -2928,7 +2934,6 @@ static void clang_reparseTranslationUnit
struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files;
unsigned options = RTUI->options;
(void) options;
- RTUI->result = 1;
CIndexer *CXXIdx = TU->CIdx;
if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing))
@@ -2986,16 +2991,20 @@ int clang_reparseTranslationUnit(CXTrans
CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
- if (!CTUnit)
+ if (isNotUseableTU(CTUnit)) {
+ LOG_BAD_TU(CTUnit);
return cxstring::createEmpty();
+ }
ASTUnit *CXXUnit = cxtu::getASTUnit(CTUnit);
return cxstring::createDup(CXXUnit->getOriginalSourceFileName());
}
CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
- if (!TU)
+ if (isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
return clang_getNullCursor();
+ }
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
return MakeCXCursor(CXXUnit->getASTContext().getTranslationUnitDecl(), TU);
@@ -3025,8 +3034,10 @@ time_t clang_getFileTime(CXFile SFile) {
}
CXFile clang_getFile(CXTranslationUnit TU, const char *file_name) {
- if (!TU)
+ if (isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
return 0;
+ }
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
@@ -3034,8 +3045,14 @@ CXFile clang_getFile(CXTranslationUnit T
return const_cast<FileEntry *>(FMgr.getFile(file_name));
}
-unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit TU, CXFile file) {
- if (!TU || !file)
+unsigned clang_isFileMultipleIncludeGuarded(CXTranslationUnit TU,
+ CXFile file) {
+ if (isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
+ return 0;
+ }
+
+ if (!file)
return 0;
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
@@ -3928,8 +3945,10 @@ static enum CXChildVisitResult GetCursor
}
CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
- if (!TU)
+ if (isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
return clang_getNullCursor();
+ }
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
ASTUnit::ConcurrencyCheck Check(*CXXUnit);
@@ -4889,6 +4908,11 @@ CXString clang_getTokenSpelling(CXTransl
break;
}
+ if (isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
+ return cxstring::createEmpty();
+ }
+
// We have to find the starting buffer pointer the hard way, by
// deconstructing the source location.
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
@@ -4908,6 +4932,11 @@ CXString clang_getTokenSpelling(CXTransl
}
CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
+ if (isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
+ return clang_getNullLocation();
+ }
+
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
if (!CXXUnit)
return clang_getNullLocation();
@@ -4917,6 +4946,11 @@ CXSourceLocation clang_getTokenLocation(
}
CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
+ if (isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
+ return clang_getNullRange();
+ }
+
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
if (!CXXUnit)
return clang_getNullRange();
@@ -5008,8 +5042,10 @@ void clang_tokenize(CXTranslationUnit TU
if (NumTokens)
*NumTokens = 0;
- if (!TU)
+ if (isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
return;
+ }
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
if (!CXXUnit || !Tokens || !NumTokens)
@@ -5729,7 +5765,11 @@ extern "C" {
void clang_annotateTokens(CXTranslationUnit TU,
CXToken *Tokens, unsigned NumTokens,
CXCursor *Cursors) {
- if (!TU || NumTokens == 0 || !Tokens || !Cursors) {
+ if (isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
+ return;
+ }
+ if (NumTokens == 0 || !Tokens || !Cursors) {
LOG_FUNC_SECTION { *Log << "<null input>"; }
return;
}
@@ -6232,7 +6272,11 @@ CXString clang_Module_getFullName(CXModu
unsigned clang_Module_getNumTopLevelHeaders(CXTranslationUnit TU,
CXModule CXMod) {
- if (!TU || !CXMod)
+ if (isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
+ return 0;
+ }
+ if (!CXMod)
return 0;
Module *Mod = static_cast<Module*>(CXMod);
FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager();
@@ -6242,7 +6286,11 @@ unsigned clang_Module_getNumTopLevelHead
CXFile clang_Module_getTopLevelHeader(CXTranslationUnit TU,
CXModule CXMod, unsigned Index) {
- if (!TU || !CXMod)
+ if (isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
+ return 0;
+ }
+ if (!CXMod)
return 0;
Module *Mod = static_cast<Module*>(CXMod);
FileManager &FileMgr = cxtu::getASTUnit(TU)->getFileManager();
@@ -6373,7 +6421,8 @@ const char *clang_getTUResourceUsageName
}
CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) {
- if (!TU) {
+ if (isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
CXTUResourceUsage usage = { (void*) 0, 0, 0 };
return usage;
}
@@ -6474,6 +6523,11 @@ CXSourceRangeList *clang_getSkippedRange
skipped->count = 0;
skipped->ranges = 0;
+ if (isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
+ return skipped;
+ }
+
if (!file)
return skipped;
Modified: cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp?rev=201155&r1=201154&r2=201155&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp (original)
+++ cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp Tue Feb 11 08:34:14 2014
@@ -680,7 +680,12 @@ void clang_codeCompleteAt_Impl(void *Use
#endif
bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0;
-
+
+ if (cxtu::isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
+ return;
+ }
+
ASTUnit *AST = cxtu::getASTUnit(TU);
if (!AST)
return;
Modified: cfe/trunk/tools/libclang/CIndexDiagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexDiagnostic.cpp?rev=201155&r1=201154&r2=201155&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndexDiagnostic.cpp (original)
+++ cfe/trunk/tools/libclang/CIndexDiagnostic.cpp Tue Feb 11 08:34:14 2014
@@ -208,12 +208,21 @@ CXDiagnosticSetImpl *cxdiag::lazyCreateD
extern "C" {
unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) {
+ if (cxtu::isNotUseableTU(Unit)) {
+ LOG_BAD_TU(Unit);
+ return 0;
+ }
if (!cxtu::getASTUnit(Unit))
return 0;
return lazyCreateDiags(Unit, /*checkIfChanged=*/true)->getNumDiagnostics();
}
CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) {
+ if (cxtu::isNotUseableTU(Unit)) {
+ LOG_BAD_TU(Unit);
+ return 0;
+ }
+
CXDiagnosticSet D = clang_getDiagnosticSetFromTU(Unit);
if (!D)
return 0;
@@ -224,8 +233,12 @@ CXDiagnostic clang_getDiagnostic(CXTrans
return Diags->getDiagnostic(Index);
}
-
+
CXDiagnosticSet clang_getDiagnosticSetFromTU(CXTranslationUnit Unit) {
+ if (cxtu::isNotUseableTU(Unit)) {
+ LOG_BAD_TU(Unit);
+ return 0;
+ }
if (!cxtu::getASTUnit(Unit))
return 0;
return static_cast<CXDiagnostic>(lazyCreateDiags(Unit));
Modified: cfe/trunk/tools/libclang/CIndexHigh.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexHigh.cpp?rev=201155&r1=201154&r2=201155&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndexHigh.cpp (original)
+++ cfe/trunk/tools/libclang/CIndexHigh.cpp Tue Feb 11 08:34:14 2014
@@ -480,13 +480,12 @@ CXResult clang_findReferencesInFile(CXCu
CXResult clang_findIncludesInFile(CXTranslationUnit TU, CXFile file,
CXCursorAndRangeVisitor visitor) {
- LogRef Log = Logger::make(LLVM_FUNCTION_NAME);
-
- if (!TU) {
- if (Log)
- *Log << "Null CXTranslationUnit";
+ if (cxtu::isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
return CXResult_Invalid;
}
+
+ LogRef Log = Logger::make(LLVM_FUNCTION_NAME);
if (!file) {
if (Log)
*Log << "Null file";
Modified: cfe/trunk/tools/libclang/CIndexInclusionStack.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexInclusionStack.cpp?rev=201155&r1=201154&r2=201155&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndexInclusionStack.cpp (original)
+++ cfe/trunk/tools/libclang/CIndexInclusionStack.cpp Tue Feb 11 08:34:14 2014
@@ -24,7 +24,11 @@ using namespace clang;
extern "C" {
void clang_getInclusions(CXTranslationUnit TU, CXInclusionVisitor CB,
CXClientData clientData) {
-
+ if (cxtu::isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
+ return;
+ }
+
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
SourceManager &SM = CXXUnit->getSourceManager();
ASTContext &Ctx = CXXUnit->getASTContext();
Modified: cfe/trunk/tools/libclang/CXSourceLocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXSourceLocation.cpp?rev=201155&r1=201154&r2=201155&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXSourceLocation.cpp (original)
+++ cfe/trunk/tools/libclang/CXSourceLocation.cpp Tue Feb 11 08:34:14 2014
@@ -122,7 +122,11 @@ CXSourceLocation clang_getLocation(CXTra
CXFile file,
unsigned line,
unsigned column) {
- if (!TU || !file)
+ if (cxtu::isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
+ return clang_getNullLocation();
+ }
+ if (!file)
return clang_getNullLocation();
if (line == 0 || column == 0)
return clang_getNullLocation();
@@ -151,9 +155,13 @@ CXSourceLocation clang_getLocation(CXTra
CXSourceLocation clang_getLocationForOffset(CXTranslationUnit TU,
CXFile file,
unsigned offset) {
- if (!TU || !file)
+ if (cxtu::isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
return clang_getNullLocation();
-
+ }
+ if (!file)
+ return clang_getNullLocation();
+
ASTUnit *CXXUnit = cxtu::getASTUnit(TU);
SourceLocation SLoc
Modified: cfe/trunk/tools/libclang/CXTranslationUnit.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXTranslationUnit.h?rev=201155&r1=201154&r2=201155&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXTranslationUnit.h (original)
+++ cfe/trunk/tools/libclang/CXTranslationUnit.h Tue Feb 11 08:34:14 2014
@@ -15,6 +15,7 @@
#define LLVM_CLANG_CXTRANSLATIONUNIT_H
#include "CXString.h"
+#include "CLog.h"
#include "clang-c/Index.h"
namespace clang {
@@ -45,6 +46,17 @@ static inline ASTUnit *getASTUnit(CXTran
return TU->TheASTUnit;
}
+static inline bool isNotUseableTU(CXTranslationUnit TU) {
+ return !TU;
+}
+
+#define LOG_BAD_TU(TU) \
+ do { \
+ LOG_FUNC_SECTION { \
+ *Log << "called with a bad TU: " << TU; \
+ } \
+ } while(false)
+
class CXTUOwner {
CXTranslationUnitImpl *TU;
Modified: cfe/trunk/tools/libclang/Indexing.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/Indexing.cpp?rev=201155&r1=201154&r2=201155&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/Indexing.cpp (original)
+++ cfe/trunk/tools/libclang/Indexing.cpp Tue Feb 11 08:34:14 2014
@@ -756,8 +756,10 @@ static void clang_indexTranslationUnit_I
unsigned index_options = ITUI->index_options;
ITUI->result = 1; // init as error.
- if (!TU)
+ if (isNotUseableTU(TU)) {
+ LOG_BAD_TU(TU);
return;
+ }
if (!client_index_callbacks || index_callbacks_size == 0)
return;
More information about the cfe-commits
mailing list