Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion clang/include/clang-c/CXString.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "clang-c/ExternC.h"
#include "clang-c/Platform.h"
#include <stddef.h>

LLVM_CLANG_C_EXTERN_C_BEGIN

Expand Down Expand Up @@ -44,6 +45,11 @@ typedef struct {
unsigned Count;
} CXStringSet;

typedef struct {
const char *string;
size_t length;
} CStringInfo;
Copy link
Author

@xTachyon xTachyon Jun 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we leave space for future expansion?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we can now feel a bit more free to add a different private_flags value, I think we're fine to not leave space for future expansion. WDYT?


/**
* Retrieve the character data associated with the given string.
*
Expand All @@ -53,6 +59,15 @@ typedef struct {
*/
CINDEX_LINKAGE const char *clang_getCString(CXString string);

/**
* Retrieve the character data associated with the given string and its length.
*
* The returned lenght might be bigger than strlen(.string) if the string
* contains nul bytes. This function has the same requirements and guarantees as
* clang_getCString.
*/
CINDEX_LINKAGE CStringInfo clang_getCStringInfo(CXString string);

/**
* Free the given string.
*/
Expand All @@ -70,4 +85,3 @@ CINDEX_LINKAGE void clang_disposeStringSet(CXStringSet *set);
LLVM_CLANG_C_EXTERN_C_END

#endif

17 changes: 13 additions & 4 deletions clang/include/clang-c/Index.h
Original file line number Diff line number Diff line change
Expand Up @@ -6058,12 +6058,21 @@ clang_EvalResult_getAsUnsigned(CXEvalResult E);
CINDEX_LINKAGE double clang_EvalResult_getAsDouble(CXEvalResult E);

/**
* Returns the evaluation result as a constant string if the
* kind is other than Int or float. User must not free this pointer,
* instead call clang_EvalResult_dispose on the CXEvalResult returned
* by clang_Cursor_Evaluate.
* This function behaves the same as clang_EvalResult_getAsCXString, with 2
* exceptions:
* - the string literal will be truncated if a nul byte is found in the string.
* For this reason clang_EvalResult_getAsCXString is recommended.
* - the caller must not free this pointer; instead call
* clang_EvalResult_dispose on the CXEvalResult returned by
* clang_Cursor_Evaluate.
*/
CINDEX_LINKAGE const char *clang_EvalResult_getAsStr(CXEvalResult E);
/**
* Returns the evaluation result as a CXString if the
* kind is other than Int or float. This might include zero bytes.
* The caller is responsible for freeing the CXString using clang_disposeString.
*/
CINDEX_LINKAGE CXString clang_EvalResult_getAsCXString(CXEvalResult E);

/**
* Disposes the created Eval memory.
Expand Down
44 changes: 17 additions & 27 deletions clang/tools/libclang/CIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4595,13 +4595,13 @@ struct ExprEvalResult {
unsigned long long unsignedVal;
long long intVal;
double floatVal;
char *stringVal;
CXString stringVal;
} EvalData;
bool IsUnsignedInt;
~ExprEvalResult() {
if (EvalType != CXEval_UnExposed && EvalType != CXEval_Float &&
EvalType != CXEval_Int) {
delete[] EvalData.stringVal;
clang_disposeString(EvalData.stringVal);
}
}
};
Expand Down Expand Up @@ -4657,7 +4657,15 @@ const char *clang_EvalResult_getAsStr(CXEvalResult E) {
if (!E) {
return nullptr;
}
return ((ExprEvalResult *)E)->EvalData.stringVal;
return clang_getCString(((ExprEvalResult *)E)->EvalData.stringVal);
}

CXString clang_EvalResult_getAsCXString(CXEvalResult E) {
if (!E) {
return cxstring::createNull();
}
auto data = clang_getCStringInfo(((ExprEvalResult *)E)->EvalData.stringVal);
return cxstring::createDup(StringRef(data.string, data.length));
}

static const ExprEvalResult *evaluateExpr(Expr *expr, CXCursor C) {
Expand Down Expand Up @@ -4721,11 +4729,7 @@ static const ExprEvalResult *evaluateExpr(Expr *expr, CXCursor C) {
result->EvalType = CXEval_StrLiteral;
}

std::string strRef(StrE->getString().str());
result->EvalData.stringVal = new char[strRef.size() + 1];
strncpy((char *)result->EvalData.stringVal, strRef.c_str(),
strRef.size());
result->EvalData.stringVal[strRef.size()] = '\0';
result->EvalData.stringVal = cxstring::createDup(StrE->getString());
return result.release();
}
} else if (expr->getStmtClass() == Stmt::ObjCStringLiteralClass ||
Expand All @@ -4742,10 +4746,7 @@ static const ExprEvalResult *evaluateExpr(Expr *expr, CXCursor C) {
result->EvalType = CXEval_StrLiteral;
}

std::string strRef(StrE->getString().str());
result->EvalData.stringVal = new char[strRef.size() + 1];
strncpy((char *)result->EvalData.stringVal, strRef.c_str(), strRef.size());
result->EvalData.stringVal[strRef.size()] = '\0';
result->EvalData.stringVal = cxstring::createDup(StrE->getString());
return result.release();
}

Expand All @@ -4759,13 +4760,8 @@ static const ExprEvalResult *evaluateExpr(Expr *expr, CXCursor C) {
callExpr = static_cast<CallExpr *>(CC->getSubExpr());
StringLiteral *S = getCFSTR_value(callExpr);
if (S) {
std::string strLiteral(S->getString().str());
result->EvalType = CXEval_CFStr;

result->EvalData.stringVal = new char[strLiteral.size() + 1];
strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(),
strLiteral.size());
result->EvalData.stringVal[strLiteral.size()] = '\0';
result->EvalData.stringVal = cxstring::createDup(S->getString());
return result.release();
}
}
Expand All @@ -4785,24 +4781,18 @@ static const ExprEvalResult *evaluateExpr(Expr *expr, CXCursor C) {

StringLiteral *S = getCFSTR_value(callExpr);
if (S) {
std::string strLiteral(S->getString().str());
result->EvalType = CXEval_CFStr;
result->EvalData.stringVal = new char[strLiteral.size() + 1];
strncpy((char *)result->EvalData.stringVal, strLiteral.c_str(),
strLiteral.size());
result->EvalData.stringVal[strLiteral.size()] = '\0';
result->EvalData.stringVal = cxstring::createDup(S->getString());
return result.release();
}
}
} else if (expr->getStmtClass() == Stmt::DeclRefExprClass) {
DeclRefExpr *D = static_cast<DeclRefExpr *>(expr);
ValueDecl *V = D->getDecl();
if (V->getKind() == Decl::Function) {
std::string strName = V->getNameAsString();
result->EvalType = CXEval_Other;
result->EvalData.stringVal = new char[strName.size() + 1];
strncpy(result->EvalData.stringVal, strName.c_str(), strName.size());
result->EvalData.stringVal[strName.size()] = '\0';
result->EvalData.stringVal =
cxstring::createDup(StringRef(V->getNameAsString()));
return result.release();
}
}
Expand Down
57 changes: 42 additions & 15 deletions clang/tools/libclang/CXString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ enum CXStringFlag {
/// CXString contains a 'const char *' that it doesn't own.
CXS_Unmanaged,

/// CXString contains a 'const char *' that it allocated with malloc().
CXS_Malloc,
/// CXString contains a 'CStringImpl' that it allocated with malloc().
CXS_MallocWithSize,

/// CXString contains a CXStringBuf that needs to be returned to the
/// CXStringPool.
CXS_StringBuf
};

struct CStringImpl {
size_t length;
char buffer[sizeof(length)];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this a bit more? I would have expected this to be:

struct CStringImpl {
  size_t length;
  char buffer[];
};

with a flexible array member. This always allocates 4-8 bytes for the string length and runs into out-of-bounds array behavior because the final member is a fixed length array rather than a FAM.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot about oob here. I was trying to avoid compiler extensions (flexible array member), as I understand it's not standard C++. I can change it to flexible array member without problem.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This not being in C++ is a good point. Given that this is purely internal, maybe we want to just have the size field and then tail-allocate the buffer? We could probably even use llvm::TrailingObjects given that this all lives in a C++ file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look at that class when I get the time, but just allocating the data without making a struct seems straight forward enough. I also need to see why tests fail while I'm at it.

};

namespace clang {
namespace cxstring {

Expand Down Expand Up @@ -71,10 +76,7 @@ CXString createDup(const char *String) {
if (String[0] == '\0')
return createEmpty();

CXString Str;
Str.data = strdup(String);
Str.private_flags = CXS_Malloc;
return Str;
return createDup(StringRef(String));
}

CXString createRef(StringRef String) {
Expand All @@ -91,12 +93,18 @@ CXString createRef(StringRef String) {
}

CXString createDup(StringRef String) {
auto toAllocate =
sizeof(size_t) + std::max(sizeof(size_t), String.size() + 1);
assert(toAllocate >= sizeof(CStringImpl));
auto ptr = static_cast<CStringImpl *>(llvm::safe_malloc(toAllocate));

ptr->length = String.size();
memcpy(ptr->buffer, String.data(), String.size());
ptr->buffer[String.size()] = 0;

CXString Result;
char *Spelling = static_cast<char *>(llvm::safe_malloc(String.size() + 1));
memmove(Spelling, String.data(), String.size());
Spelling[String.size()] = 0;
Result.data = Spelling;
Result.private_flags = (unsigned) CXS_Malloc;
Result.data = ptr;
Result.private_flags = (unsigned)CXS_MallocWithSize;
return Result;
}

Expand Down Expand Up @@ -164,19 +172,38 @@ const char *clang_getCString(CXString string) {
return static_cast<const char *>(string.data);
}

CStringInfo clang_getCStringInfo(CXString string) {
switch ((CXStringFlag)string.private_flags) {
case CXS_Unmanaged: {
auto ptr = static_cast<const char *>(string.data);
return {ptr, strlen(ptr)};
}
case CXS_MallocWithSize: {
auto ptr = static_cast<const CStringImpl *>(string.data);
return {ptr->buffer, ptr->length};
}
case CXS_StringBuf: {
auto ptr = static_cast<const cxstring::CXStringBuf *>(string.data);
return {ptr->Data.data(), ptr->Data.size()};
}
}
llvm_unreachable("Invalid CXString::private_flags");
}

void clang_disposeString(CXString string) {
switch ((CXStringFlag) string.private_flags) {
case CXS_Unmanaged:
break;
case CXS_Malloc:
return;
case CXS_MallocWithSize:
if (string.data)
free(const_cast<void *>(string.data));
break;
return;
case CXS_StringBuf:
static_cast<cxstring::CXStringBuf *>(
const_cast<void *>(string.data))->dispose();
break;
return;
}
llvm_unreachable("Invalid CXString::private_flags");
}

void clang_disposeStringSet(CXStringSet *set) {
Expand Down
2 changes: 2 additions & 0 deletions clang/tools/libclang/libclang.map
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ LLVM_21 {
clang_Cursor_getGCCAssemblyNumClobbers;
clang_Cursor_getGCCAssemblyClobber;
clang_Cursor_isGCCAssemblyVolatile;
clang_getCStringInfo;
clang_EvalResult_getAsCXString;
};

# Example of how to add a new symbol version entry. If you do add a new symbol
Expand Down
40 changes: 40 additions & 0 deletions clang/unittests/libclang/LibclangTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,46 @@ TEST_F(LibclangParseTest, EvaluateChildExpression) {
nullptr);
}

TEST_F(LibclangParseTest, StringLiteralWithZeros) {
const char testSource[] = R"cpp(
const char str[] = "pika\0chu";
)cpp";
std::string fileName = "main.cpp";
WriteFile(fileName, testSource);

const char *Args[] = {"-xc++"};
ClangTU = clang_parseTranslationUnit(Index, fileName.c_str(), Args, 1,
nullptr, 0, TUFlags);

int nodes = 0;

Traverse([&nodes](CXCursor cursor, CXCursor parent) -> CXChildVisitResult {
if (cursor.kind == CXCursor_StringLiteral) {
CXEvalResult RE = clang_Cursor_Evaluate(cursor);
EXPECT_NE(RE, nullptr);
EXPECT_EQ(clang_EvalResult_getKind(RE), CXEval_StrLiteral);

const char expected[] = "pika\0chu";
size_t expected_size = sizeof(expected) - 1;

auto lit = clang_EvalResult_getAsCXString(RE);
auto str = clang_getCStringInfo(lit);

EXPECT_TRUE(str.length == expected_size &&
memcmp(str.string, expected, str.length) == 0);

clang_disposeString(lit);
clang_EvalResult_dispose(RE);

nodes++;
return CXChildVisit_Continue;
}
return CXChildVisit_Recurse;
});

EXPECT_EQ(nodes, 1);
}

class LibclangReparseTest : public LibclangParseTest {
public:
void DisplayDiagnostics() {
Expand Down
Loading