Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matdbg: fix two bugs with formatting #8388

Merged
merged 1 commit into from
Jan 27, 2025
Merged
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
40 changes: 26 additions & 14 deletions libs/matdbg/src/SourceFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,32 @@ namespace filament::matdbg {

#if defined(__linux__) || defined(__APPLE__)
std::string SourceFormatter::format(char const* source) {
std::string const TMP_FILENAME = "/tmp/matdbg-tmp-src.cpp";
char output[1024];

std::string original(source);
FILE* fp;
fp = fopen(TMP_FILENAME.c_str(), "w");
// First check if clang-format is available.
FILE* fp = popen("clang-format -version", "r");

// Need to drain the output
while (fgets(output, 1024, fp) != NULL) {}

int status = pclose(fp);
if (!fp || !WEXITSTATUS(status)) {
std::call_once(mClangWarningFlag, []() {
utils::slog.w << "[matdbg] unable to run clang-format to format shader file. "
<< "Please make sure it's installed." << utils::io::endl;
});
return source;
}

// Write the source to a temp file for formatting
char const* TMP_FILENAME = "/tmp/matdbg-tmp-src.cpp";
fp = fopen(TMP_FILENAME, "w");
if (!fp) {
return original;
return source;
}

// Unknown if the input is 0-terminated so we put it in a string first.
std::string original(source);
fputs(original.c_str(), fp);
fflush(fp);
pclose(fp);
Expand All @@ -45,22 +63,16 @@ std::string SourceFormatter::format(char const* source) {
"}'";

fp = popen(("clang-format " + CLANG_FORMAT_OPTIONS + "< " + TMP_FILENAME).c_str(), "r");

if (!fp) {
std::call_once(mClangWarningFlag, []() {
utils::slog.w << "[matdbg] unable to run clang-format to format shader file. "
<< "Please make sure it's installed.";
});
return original;
}

char output[1024];
std::stringstream outStream;
while (fgets(output, 1024, fp) != NULL) {
outStream << output;
}

int status = pclose(fp);
status = pclose(fp);
if (WEXITSTATUS(status)) {
utils::slog.w << "[matdbg] clang-format failed with code=" << WEXITSTATUS(status)
<< utils::io::endl;
Expand All @@ -69,11 +81,11 @@ std::string SourceFormatter::format(char const* source) {
}
#else
std::string SourceFormatter::format(char const* source) {
std::call_once(mClangWarningFlag, []() {
std::call_once(mClangWarningFlag, []() {
utils::slog.w <<"[matdbg]: source formatting is not available on this platform" <<
utils::io::endl;
});
return "";
return source;
}
#endif

Expand Down
Loading