diff options
Diffstat (limited to 'src/tools/moc/main.cpp')
-rw-r--r-- | src/tools/moc/main.cpp | 95 |
1 files changed, 49 insertions, 46 deletions
diff --git a/src/tools/moc/main.cpp b/src/tools/moc/main.cpp index 71b9757ebc..89fb367ca7 100644 --- a/src/tools/moc/main.cpp +++ b/src/tools/moc/main.cpp @@ -19,7 +19,8 @@ #include <qcoreapplication.h> #include <qcommandlineoption.h> #include <qcommandlineparser.h> -#include <qscopedpointer.h> + +#include <memory> QT_BEGIN_NAMESPACE @@ -58,10 +59,21 @@ void error(const char *msg = "Invalid argument") fprintf(stderr, "moc: %s\n", msg); } -struct ScopedPointerFileCloser +static auto openFileForWriting(const QString &name) { - static inline void cleanup(FILE *handle) { if (handle) fclose(handle); } -}; + struct Closer { void operator()(FILE *handle) const { fclose(handle); } }; + using R = std::unique_ptr<FILE, Closer>; + +#ifdef _MSC_VER + FILE *file; + if (_wfopen_s(&file, reinterpret_cast<const wchar_t *>(name.utf16()), L"w") != 0) + return R{}; + return R{file}; +#else + return R{fopen(QFile::encodeName(name).constData(), "w")}; +#endif +} +using File = decltype(openFileForWriting({})); static inline bool hasNext(const Symbols &symbols, int i) { return (i < symbols.size()); } @@ -178,7 +190,7 @@ int runMoc(int argc, char **argv) QString filename; QString output; QFile in; - FILE *out = nullptr; + File out; // Note that moc isn't translated. // If you use this code as an example for a translated app, make sure to translate the strings. @@ -330,7 +342,7 @@ int runMoc(int argc, char **argv) if (parser.isSet(collectOption)) return collectJson(files, output, hasOptionFiles); - if (files.count() > 1) { + if (files.size() > 1) { error(qPrintable("Too many input files specified: '"_L1 + files.join("' '"_L1) + u'\'')); parser.showHelp(1); } else if (!files.isEmpty()) { @@ -399,7 +411,7 @@ int runMoc(int argc, char **argv) for (const QString &arg : defines) { QByteArray name = arg.toLocal8Bit(); QByteArray value("1"); - int eq = name.indexOf('='); + const qsizetype eq = name.indexOf('='); if (eq >= 0) { value = name.mid(eq + 1); name = name.left(eq); @@ -449,11 +461,14 @@ int runMoc(int argc, char **argv) if (filename.isEmpty()) { filename = QStringLiteral("standard input"); - in.open(stdin, QIODevice::ReadOnly); + if (!in.open(stdin, QIODevice::ReadOnly)) { + fprintf(stderr, "moc: cannot open standard input: %s\n", qPrintable(in.errorString())); + return 1; + } } else { in.setFileName(filename); if (!in.open(QIODevice::ReadOnly)) { - fprintf(stderr, "moc: %s: No such file\n", qPrintable(filename)); + fprintf(stderr, "moc: cannot open %s: %s\n", qPrintable(filename), qPrintable(in.errorString())); return 1; } moc.filename = filename.toLocal8Bit(); @@ -525,51 +540,45 @@ int runMoc(int argc, char **argv) // 3. and output meta object code - QScopedPointer<FILE, ScopedPointerFileCloser> jsonOutput; + File jsonOutput; bool outputToFile = true; if (output.size()) { // output file specified -#if defined(_MSC_VER) - if (_wfopen_s(&out, reinterpret_cast<const wchar_t *>(output.utf16()), L"w") != 0) -#else - out = fopen(QFile::encodeName(output).constData(), "w"); // create output file + out = openFileForWriting(output); if (!out) -#endif { - fprintf(stderr, "moc: Cannot create %s\n", QFile::encodeName(output).constData()); + const auto fopen_errno = errno; + fprintf(stderr, "moc: Cannot create %s. Error: %s\n", + QFile::encodeName(output).constData(), + strerror(fopen_errno)); return 1; } if (parser.isSet(jsonOption)) { const QString jsonOutputFileName = output + ".json"_L1; - FILE *f; -#if defined(_MSC_VER) - if (_wfopen_s(&f, reinterpret_cast<const wchar_t *>(jsonOutputFileName.utf16()), L"w") != 0) -#else - f = fopen(QFile::encodeName(jsonOutputFileName).constData(), "w"); - if (!f) -#endif - fprintf(stderr, "moc: Cannot create JSON output file %s. %s\n", + jsonOutput = openFileForWriting(jsonOutputFileName); + if (!jsonOutput) { + const auto fopen_errno = errno; + fprintf(stderr, "moc: Cannot create JSON output file %s. Error: %s\n", QFile::encodeName(jsonOutputFileName).constData(), - strerror(errno)); - jsonOutput.reset(f); + strerror(fopen_errno)); + } } } else { // use stdout - out = stdout; + out.reset(stdout); outputToFile = false; } if (pp.preprocessOnly) { - fprintf(out, "%s\n", composePreprocessorOutput(moc.symbols).constData()); + fprintf(out.get(), "%s\n", composePreprocessorOutput(moc.symbols).constData()); } else { if (moc.classList.isEmpty()) moc.note("No relevant classes found. No output generated."); else - moc.generate(out, jsonOutput.data()); + moc.generate(out.get(), jsonOutput.get()); } - if (output.size()) - fclose(out); + out.reset(); if (parser.isSet(depFileOption)) { // 4. write a Make-style dependency file (can also be consumed by Ninja). @@ -587,23 +596,17 @@ int runMoc(int argc, char **argv) fprintf(stderr, "moc: Writing to stdout, but no depfile path specified.\n"); } - QScopedPointer<FILE, ScopedPointerFileCloser> depFileHandle; - FILE *depFileHandleRaw; -#if defined(_MSC_VER) - if (_wfopen_s(&depFileHandleRaw, - reinterpret_cast<const wchar_t *>(depOutputFileName.utf16()), L"w") != 0) -#else - depFileHandleRaw = fopen(QFile::encodeName(depOutputFileName).constData(), "w"); - if (!depFileHandleRaw) -#endif - fprintf(stderr, "moc: Cannot create dep output file '%s'. %s\n", + File depFileHandle = openFileForWriting(depOutputFileName); + if (!depFileHandle) { + const auto fopen_errno = errno; + fprintf(stderr, "moc: Cannot create dep output file '%s'. Error: %s\n", QFile::encodeName(depOutputFileName).constData(), - strerror(errno)); - depFileHandle.reset(depFileHandleRaw); + strerror(fopen_errno)); + } - if (!depFileHandle.isNull()) { + if (depFileHandle) { // First line is the path to the generated file. - fprintf(depFileHandle.data(), "%s: ", + fprintf(depFileHandle.get(), "%s: ", escapeAndEncodeDependencyPath(depRuleName).constData()); QByteArrayList dependencies; @@ -635,7 +638,7 @@ int runMoc(int argc, char **argv) // Join dependencies, output them, and output a final new line. const auto dependenciesJoined = dependencies.join(QByteArrayLiteral(" \\\n ")); - fprintf(depFileHandle.data(), "%s\n", dependenciesJoined.constData()); + fprintf(depFileHandle.get(), "%s\n", dependenciesJoined.constData()); } } |