754 m_warningMessagePreamble = ErrorMessagePreamble;
757 m_versionScriptGeneratorState =
764 enum class HeaderOrigin { SourceTree, Generated };
765 const auto scanHeaderFile = [
this, &error](
const std::filesystem::path &path,
766 bool isRegularFile, HeaderOrigin origin) {
767 const std::string filePath = path.generic_string();
770 const bool isGenerated = origin == HeaderOrigin::Generated;
772 if (isRegularFile && isHeaderFlag && !isDocFileHeuristicFlag) {
773 scannerDebug() <<
"Processing header: " << filePath
774 <<
" isGenerated: " << isGenerated <<
std::endl;
779 <<
"Skipping processing header: " << filePath
780 <<
" isRegularFile: " << isRegularFile
781 <<
" isHeaderFlag: " << isHeaderFlag
782 <<
" isDocFileHeuristicFlag: " << isDocFileHeuristicFlag
783 <<
" isGenerated: " << isGenerated
788 for (
auto const &entry :
789 std::filesystem::recursive_directory_iterator(m_commandLineArgs->sourceDir())) {
790 scanHeaderFile(entry.path(), entry.is_regular_file(),
791 HeaderOrigin::SourceTree);
796 for (
const auto &header : m_commandLineArgs->generatedHeaders()) {
799 const auto headerPath = makeHeaderAbsolute(header);
800 scanHeaderFile(headerPath, std::filesystem::is_regular_file(headerPath),
801 HeaderOrigin::Generated);
806 std::set<std::string> rspHeaders;
807 const auto &headers = m_commandLineArgs->headers();
808 for (
auto it = headers.begin(); it != headers.end(); ++it) {
809 const auto &header = *it;
810 scannerDebug() <<
"Processing header: " << header <<
std::endl;
815 for (
const auto &header : rspHeaders) {
816 scannerDebug() <<
"Processing header: " << header << std::endl;
817 if (!processHeader(makeHeaderAbsolute(header)))
827 for (
auto it = m_symbols.begin(); it != m_symbols.end(); ++it) {
828 const std::string &filename = it->second.file();
829 if (!filename.empty()) {
830 const auto camelCaseFile = m_commandLineArgs
->includeDir() +
'/' + it->first;
832 m_producedHeaders.insert(it->first);
833 m_moduleMapContents.insert({camelCaseFile, filename});
843 std::string versionHeaderFilename(moduleNameLower +
"version.h");
845 std::string versionFile = m_commandLineArgs
->includeDir() +
'/' + versionHeaderFilename;
848 FileStamp originalStamp = std::filesystem::last_write_time(versionFile, ec);
850 originalStamp = FileStamp::clock::now();
853 const auto camelCaseFile =
855 if (!generateAliasedHeaderFileIfTimestampChanged(camelCaseFile,
856 versionFile, originalStamp)) {
859 m_masterHeaderContents[versionHeaderFilename] = {};
860 m_producedHeaders.insert(versionHeaderFilename);
861 m_producedHeaders.insert(versionHeaderCamel);
862 m_moduleMapContents.insert({versionFile, {}});
863 m_moduleMapContents.insert({camelCaseFile, versionFile});
907 for (
const auto &header : m_publicHeaders) {
908 if (!generateForwardingHeader(m_commandLineArgs->stagingDir() +
'/' + header,
921 bool skipCleanup =
false)
924 bool outDirExists =
false;
925 if (!utils::createDirectories(outputDirectory,
"Unable to create staging directory",
929 if (outDirExists && !skipCleanup) {
931 for (
const auto &entry :
932 std::filesystem::recursive_directory_iterator(outputDirectory)) {
933 if (m_producedHeaders.find(entry.path().filename().generic_string())
934 == m_producedHeaders.end()) {
937 std::string firstLine;
939 std::ifstream input(entry.path(), std::ifstream::in);
940 if (input.is_open()) {
941 std::getline(input, firstLine);
945 if (firstLine.find(
"#ifndef DEPRECATED_HEADER_"
946 + m_commandLineArgs->moduleName())
948 || firstLine.find(
"#ifndef DEPRECATED_HEADER_") != 0)
949 std::filesystem::remove(entry.path());
952 }
catch (
const std::filesystem::filesystem_error &fserr) {
958 for (
const auto &header : m_producedHeaders) {
959 std::filesystem::path src(m_commandLineArgs->includeDir() +
'/' + header);
960 std::filesystem::path dst(outputDirectory +
'/' + header);
961 if (m_commandLineArgs->showOnly())
966 if (header == m_commandLineArgs->moduleName())
967 result &= writeIfDifferent(dst.string(), m_masterHeaderBuffer);
969 result &= updateOrCopy(src, dst);
1032 static const std::regex ThirdPartyFolderRegex(
"(^|.+/)3rdparty/.+");
1035 static const std::regex ConfigHeaderRegex(
"^(q|.+-)config(_p)?\\.h");
1039 bool isPrivate = m_currentFileType & PrivateHeader;
1040 bool isQpa = m_currentFileType & QpaHeader;
1041 bool isRhi = m_currentFileType & RhiHeader;
1042 bool isSsg = m_currentFileType & SsgHeader;
1043 bool isSpi = m_currentFileType & SpiHeader;
1044 bool isExport = m_currentFileType & ExportHeader;
1045 bool isPublic = !isPrivate && !isQpa && !isRhi && !isSsg && !isSpi;
1049 if (!m_currentFileInSourceDir
1050 && m_currentFileString.find(m_commandLineArgs
->binaryDir()) != 0) {
1051 scannerDebug() <<
"Header file: " << headerFile
1052 <<
" is outside the sync directories. Skipping." <<
std::endl;
1053 m_headerCheckExceptions.push_back(m_currentFileString);
1060 if (m_currentFileString.find(m_commandLineArgs
->includeDir()) == 0) {
1061 m_moduleMapContents.insert({m_currentFileString, {}});
1063 if (m_commandLineArgs->isFramework() && isPublic)
1064 m_publicHeaders.insert(m_currentFilename);
1071 if (m_currentFilename.empty()) {
1072 std::cerr <<
"Header file name of " << m_currentFileString <<
"is empty" <<
std::endl;
1077 FileStamp originalStamp = std::filesystem::last_write_time(headerFile, ec);
1079 originalStamp = FileStamp::clock::now();
1083 <<
"processHeader:start: " << headerFile
1084 <<
" m_currentFilename: " << m_currentFilename
1085 <<
" isPrivate: " << isPrivate
1086 <<
" isQpa: " << isQpa
1087 <<
" isRhi: " << isRhi
1088 <<
" isSsg: " << isSsg
1089 <<
" isSpi: " << isSpi
1090 <<
" isExport: " << isExport
1096 if (!utils::createDirectories(outputDir,
"Unable to create output directory"))
1099 bool headerFileExists =
std::filesystem::exists(headerFile);
1101 std::string aliasedFilepath = headerFile.generic_string();
1103 std::string aliasPath = outputDir +
'/' + m_currentFilename;
1108 if (m_commandLineArgs
->copy() && headerFileExists) {
1112 if (!generateAliasedHeaderFileIfTimestampChanged(aliasPath, aliasedFilepath,
1121 m_publicHeaders.insert(m_currentFilename);
1132 if (!headerFileExists) {
1133 scannerDebug() <<
"Header file: " << headerFile
1134 <<
" doesn't exist, but is added to syncqt scanning. Skipping.";
1142 bool is3rdParty =
std::regex_match(
1145 ThirdPartyFolderRegex);
1148 if (!
std::regex_match(m_currentFilename, ConfigHeaderRegex)) {
1153 || isSpi || !m_currentFileInSourceDir || isGenerated) {
1171 ParsingResult parsingResult;
1172 parsingResult.masterInclude = m_currentFileInSourceDir && !isExport && !is3rdParty
1173 && isPublic && !isGenerated;
1175 scannerDebug() <<
"parseHeader failed: " << headerFile <<
std::endl;
1181 && !parsingResult.versionScriptContent.empty()) {
1182 m_versionScriptContents.insert(m_versionScriptContents.end(),
1183 parsingResult.versionScriptContent.begin(),
1184 parsingResult.versionScriptContent.end());
1189 bool willBeInModuleMasterHeader =
false;
1191 if (m_currentFilename.find(
'_') == std::string::npos
1192 && parsingResult.masterInclude) {
1193 m_masterHeaderContents[m_currentFilename] = parsingResult.requireConfig;
1194 willBeInModuleMasterHeader =
true;
1199 <<
"processHeader:end: " << headerFile
1200 <<
" is3rdParty: " << is3rdParty
1201 <<
" isGenerated: " << isGenerated
1202 <<
" m_currentFileInSourceDir: " << m_currentFileInSourceDir
1203 <<
" willBeInModuleMasterHeader: " << willBeInModuleMasterHeader
1205 }
else if (m_currentFilename ==
"qconfig.h") {
1210 m_moduleMapContents.insert({outputDir +
"/" + m_currentFilename, {}});
1218 static const std::regex VersionScriptSymbolRegex(
1219 "^(?:struct|class)(?:\\s+Q_\\w*_EXPORT)?\\s+([\\w:]+)[^;]*(;$)?");
1222 static const std::regex VersionScriptNamespaceRegex(
1223 "^namespace\\s+Q_\\w+_EXPORT\\s+([\\w:]+).*");
1226 static const std::regex TrailingColonRegex(
"([\\w]+):$");
1228 switch (m_versionScriptGeneratorState) {
1230 scannerDebug() <<
"line ignored: " << buffer <<
std::endl;
1231 m_versionScriptGeneratorState = Active;
1236 m_versionScriptGeneratorState = Ignore;
1247 if (std::regex_match(buffer, match, VersionScriptSymbolRegex) && match[2].str().empty())
1248 symbol = match[1].str();
1249 else if (
std::regex_match(buffer, match, VersionScriptNamespaceRegex))
1250 symbol = match[1].str();
1252 if (
std::regex_match(symbol, match, TrailingColonRegex))
1253 symbol = match[1].str();
1256 if (!symbol.empty() && symbol[symbol.size() - 1] !=
';') {
1257 std::string relPath = m_currentFileInSourceDir
1263 std::string versionStringRecord =
" *";
1264 size_t startPos = 0;
1266 while (endPos != std::string::npos) {
1267 endPos = symbol.find(
"::", startPos);
1268 size_t length = endPos != std::string::npos ? (endPos - startPos)
1269 : (symbol.size() - startPos);
1271 std::string symbolPart = symbol.substr(startPos, length);
1272 versionStringRecord +=
std::to_string(symbolPart.size());
1273 versionStringRecord += symbolPart;
1275 startPos = endPos + 2;
1277 versionStringRecord +=
"*;";
1279 versionStringRecord +=
1281 versionStringRecord +=
" # ";
1282 versionStringRecord += relPath;
1283 versionStringRecord +=
":";
1284 versionStringRecord +=
std::to_string(m_currentFileLineNumber);
1285 versionStringRecord +=
"\n";
1286 result.versionScriptContent.push_back(versionStringRecord);
1295 ParsingResult &result,
1296 unsigned int skipChecks)
1301 static const std::regex MacroRegex(
"^\\s*#.*");
1334 static const std::regex OnceRegex(R"(^#\s*pragma\s+once$)");
1335 static const std::regex SkipHeaderCheckRegex(
"^#\\s*pragma qt_sync_skip_header_check$");
1336 static const std::regex StopProcessingRegex(
"^#\\s*pragma qt_sync_stop_processing$");
1337 static const std::regex SuspendProcessingRegex(
"^#\\s*pragma qt_sync_suspend_processing$");
1338 static const std::regex ResumeProcessingRegex(
"^#\\s*pragma qt_sync_resume_processing$");
1339 static const std::regex ExplixitClassPragmaRegex(
"^#\\s*pragma qt_class\\(([^\\)]+)\\)$");
1340 static const std::regex DeprecatesPragmaRegex(
"^#\\s*pragma qt_deprecates\\(([^\\)]+)\\)$");
1341 static const std::regex NoMasterIncludePragmaRegex(
"^#\\s*pragma qt_no_master_include$");
1345 static const std::string_view WeMeantItString(
"We mean it.");
1348 static const std::regex BeginNamespaceRegex(
"^QT_BEGIN_NAMESPACE(_[A-Z_]+)?$");
1349 static const std::regex EndNamespaceRegex(
"^QT_END_NAMESPACE(_[A-Z_]+)?$");
1355 static const std::regex IncludeRegex(
"^#\\s*include\\s*[<\"](.+)[>\"]");
1358 static const std::regex NamespaceRegex(
"\\s*namespace ([^ ]*)\\s+");
1362 static const std::regex DeclareIteratorRegex(
"^ *Q_DECLARE_\\w*ITERATOR\\((\\w+)\\);?$");
1367 static const std::regex RequireConfigRegex(
"^ *QT_REQUIRE_CONFIG\\((\\w+)\\);?$");
1375 static const std::regex ElfVersionTagRegex(
".*ELFVERSION:(stop|ignore-next|ignore).*");
1377 std::ifstream input(headerFile, std::ifstream::in);
1378 if (!input.is_open()) {
1379 std::cerr <<
"Unable to open " << headerFile <<
std::endl;
1383 bool hasQtBeginNamespace =
false;
1384 std::string qtBeginNamespace;
1385 std::string qtEndNamespace;
1386 bool hasWeMeantIt =
false;
1387 bool isSuspended =
false;
1388 bool isMultiLineComment =
false;
1389 std::size_t bracesDepth = 0;
1390 std::size_t namespaceCount = 0;
1391 std::string namespaceString;
1397 std::string tmpLine;
1398 std::size_t linesProcessed = 0;
1401 const auto error = [&] () ->
decltype(
auto) {
1402 return std::cerr << ErrorMessagePreamble << m_currentFileString
1403 <<
":" << m_currentFileLineNumber <<
" ";
1407 while (
std::getline(input, tmpLine)) {
1408 ++m_currentFileLineNumber;
1409 line.append(tmpLine);
1410 if (line.empty() || line.at(line.size() - 1) ==
'\\') {
1414 buffer.reserve(line.size());
1419 for (
std::size_t i = 0; i < line.size(); ++i) {
1420 if (line[i] ==
'\r')
1422 if (bracesDepth == namespaceCount) {
1423 if (line[i] ==
'/') {
1424 if ((i + 1) < line.size()) {
1425 if (line[i + 1] ==
'*') {
1426 isMultiLineComment =
true;
1428 }
else if (line[i + 1] ==
'/') {
1429 if (!(skipChecks & WeMeantItChecks)
1430 && line.find(WeMeantItString) != std::string::npos) {
1431 hasWeMeantIt =
true;
1434 if (m_versionScriptGeneratorState != Stopped
1435 &&
std::regex_match(line, match, ElfVersionTagRegex)) {
1436 if (match[1].str() ==
"ignore")
1437 m_versionScriptGeneratorState = Ignore;
1438 else if (match[1].str() ==
"ignore-next")
1439 m_versionScriptGeneratorState = IgnoreNext;
1440 else if (match[1].str() ==
"stop")
1441 m_versionScriptGeneratorState = Stopped;
1446 }
else if (line[i] ==
'*' && (i + 1) < line.size() && line[i + 1] ==
'/') {
1448 isMultiLineComment =
false;
1453 if (isMultiLineComment) {
1454 if (!(skipChecks & WeMeantItChecks) &&
1455 line.find(WeMeantItString) != std::string::npos) {
1456 hasWeMeantIt =
true;
1462 if (line[i] ==
'{') {
1463 if (
std::regex_match(buffer, match, NamespaceRegex)) {
1465 namespaceString +=
"::";
1466 namespaceString += match[1].str();
1470 }
else if (line[i] ==
'}') {
1471 if (namespaceCount > 0 && bracesDepth == namespaceCount) {
1472 namespaceString.resize(namespaceString.rfind(
"::"));
1476 }
else if (bracesDepth == namespaceCount) {
1482 scannerDebug() << m_currentFilename <<
": " << buffer <<
std::endl;
1484 if (m_currentFileType & PrivateHeader) {
1494 (m_currentFileType & PrivateHeader) || (m_currentFileType & QpaHeader) || (m_currentFileType & RhiHeader)
1495 || (m_currentFileType & SsgHeader) || (m_currentFileType & SpiHeader);
1498 if (
std::regex_match(buffer, MacroRegex)) {
1499 if (
std::regex_match(buffer, SkipHeaderCheckRegex)) {
1502 }
else if (
std::regex_match(buffer, StopProcessingRegex)) {
1503 if (skipChecks == AllChecks)
1504 m_headerCheckExceptions.push_back(m_currentFileString);
1506 }
else if (
std::regex_match(buffer, SuspendProcessingRegex)) {
1508 }
else if (
std::regex_match(buffer, ResumeProcessingRegex)) {
1509 isSuspended =
false;
1510 }
else if (
std::regex_match(buffer, match, ExplixitClassPragmaRegex)) {
1513 SymbolDescriptor::Pragma);
1517 }
else if (
std::regex_match(buffer, NoMasterIncludePragmaRegex)) {
1518 result.masterInclude =
false;
1519 }
else if (
std::regex_match(buffer, match, DeprecatesPragmaRegex)) {
1520 m_deprecatedHeaders[match[1].str()] = currentFileIncludePath();
1521 }
else if (
std::regex_match(buffer, OnceRegex)) {
1524 error() <<
"\"#pragma once\" is not allowed in installed header files: "
1525 "https://lists.qt-project.org/pipermail/development/2022-October/043121.html"
1528 }
else if (
std::regex_match(buffer, match, IncludeRegex) && !isSuspended) {
1530 std::string includedHeader = match[1].str();
1534 .generic_string()
)) {
1536 error() <<
"includes private header " << includedHeader <<
std::endl;
1538 for (
const auto &module : m_commandLineArgs->knownModules()) {
1539 std::string suggestedHeader =
"Qt" + module +
'/' + includedHeader;
1540 const std::string suggestedHeaderReversePath =
"/../" + suggestedHeader;
1541 if (std::filesystem::exists(m_commandLineArgs->includeDir()
1542 + suggestedHeaderReversePath)
1543 || std::filesystem::exists(m_commandLineArgs->installIncludeDir()
1544 +
'/' + suggestedHeader)) {
1545 faults |= IncludeChecks;
1546 std::cerr << m_warningMessagePreamble << m_currentFileString
1547 <<
":" << m_currentFileLineNumber
1548 <<
" includes " << includedHeader
1549 <<
" when it should include "
1550 << suggestedHeader << std::endl;
1567 if (namespaceCount == 0
1568 ||
std::regex_match(namespaceString,
1571 SymbolDescriptor::Declaration
);
1574 }
else if (
std::regex_match(buffer, match, DeclareIteratorRegex)) {
1575 std::string iteratorSymbol = match[1].str() +
"Iterator";
1577 SymbolDescriptor::Declaration
);
1579 m_currentFilename
, SymbolDescriptor::Declaration
);
1581 }
else if (
std::regex_match(buffer, match, RequireConfigRegex)) {
1582 result.requireConfig = match[1].str();
1590 if (
std::regex_match(buffer, match, BeginNamespaceRegex)) {
1591 qtBeginNamespace = match[1].str();
1592 hasQtBeginNamespace =
true;
1593 }
else if (
std::regex_match(buffer, match, EndNamespaceRegex)) {
1594 qtEndNamespace = match[1].str();
1602 if (hasQtBeginNamespace) {
1603 if (qtBeginNamespace != qtEndNamespace) {
1605 std::cerr << m_warningMessagePreamble << m_currentFileString
1606 <<
" the begin namespace macro QT_BEGIN_NAMESPACE" << qtBeginNamespace
1607 <<
" doesn't match the end namespace macro QT_END_NAMESPACE"
1608 << qtEndNamespace << std::endl;
1612 std::cerr << m_warningMessagePreamble << m_currentFileString
1613 <<
" does not include QT_BEGIN_NAMESPACE" << std::endl;
1619 std::cerr << m_warningMessagePreamble << m_currentFileString
1620 <<
" does not have the \"We mean it.\" warning"
1624 scannerDebug() <<
"linesTotal: " << m_currentFileLineNumber
1625 <<
" linesProcessed: " << linesProcessed << std::endl;
1627 if (skipChecks == AllChecks)
1628 m_headerCheckExceptions.push_back(m_currentFileString);
1631 return !(faults & m_criticalChecks);
1814 static std::regex cIdentifierSymbolsRegex(
"[^a-zA-Z0-9_]");
1815 const std::string guard_base =
"DEPRECATED_HEADER_" + m_commandLineArgs
->moduleName();
1817 for (
auto it = m_deprecatedHeaders.begin(); it != m_deprecatedHeaders.end(); ++it) {
1818 const std::string &descriptor = it->first;
1819 const std::string &replacement = it->second;
1821 const auto separatorPos = descriptor.find(
',');
1822 std::string headerPath = descriptor.substr(0, separatorPos);
1823 std::string versionDisclaimer;
1824 if (separatorPos != std::string::npos) {
1825 std::string version = descriptor.substr(separatorPos + 1);
1826 versionDisclaimer =
" and will be removed in Qt " + version;
1828 if (!parsed
.ok() || parsed.count != 2) {
1829 std::cerr << ErrorMessagePreamble
1830 <<
"Invalid version format specified for the deprecated header file "
1831 << headerPath <<
": '" << version
1832 <<
"'. Expected format: 'major.minor'.\n";
1836 const int major = parsed.parts[0];
1837 const int minor = parsed.parts[1];
1839 if (QT_VERSION_MAJOR > major
1840 || (QT_VERSION_MAJOR == major && QT_VERSION_MINOR >= minor)) {
1841 std::cerr << WarningMessagePreamble << headerPath
1842 <<
" is marked as deprecated and will not be generated in Qt "
1844 <<
". The respective qt_deprecates pragma needs to be removed.\n";
1849 const auto moduleSeparatorPos = headerPath.find(
'/');
1850 std::string headerName = moduleSeparatorPos != std::string::npos
1851 ? headerPath.substr(moduleSeparatorPos + 1)
1853 const std::string moduleName = moduleSeparatorPos != std::string::npos
1854 ? headerPath.substr(0, moduleSeparatorPos)
1855 : m_commandLineArgs->moduleName();
1858 std::cerr << ErrorMessagePreamble <<
"Unable to deprecate " << headerPath
1859 <<
": only public header files can be deprecated.\n";
1864 bool isCrossModuleDeprecation = moduleName != m_commandLineArgs
->moduleName();
1866 std::string qualifiedHeaderName =
1867 std::regex_replace(headerName, cIdentifierSymbolsRegex,
"_");
1868 std::string guard = guard_base +
"_" + qualifiedHeaderName;
1869 std::string warningText =
"Header <" + moduleName +
"/" + headerName +
"> is deprecated"
1870 + versionDisclaimer +
". Please include <" + replacement +
"> instead.";
1871 std::stringstream buffer;
1872 buffer <<
"#ifndef " << guard <<
"\n"
1873 <<
"#define " << guard <<
"\n"
1874 <<
"#if defined(__GNUC__)\n"
1875 <<
"# warning " << warningText <<
"\n"
1876 <<
"#elif defined(_MSC_VER)\n"
1877 <<
"# pragma message (\"" << warningText <<
"\")\n"
1879 <<
"#include <" << replacement <<
">\n"
1882 const std::string outputDir = isCrossModuleDeprecation
1888 if (isCrossModuleDeprecation) {
1889 const std::string stagingDir = outputDir +
"/.syncqt_staging/";
1892 m_producedHeaders.insert(headerName);