Skip to content

Commit 4ca6732

Browse files
committed
[Frontend] NFC: Move dumpAPI up a bit
1 parent 958bf8e commit 4ca6732

File tree

1 file changed

+62
-62
lines changed

1 file changed

+62
-62
lines changed

lib/FrontendTool/FrontendTool.cpp

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,68 @@ static bool emitAnyWholeModulePostTypeCheckSupplementaryOutputs(
12241224
return hadAnyError;
12251225
}
12261226

1227+
/// Returns true if an error occurred.
1228+
static bool dumpAPI(ModuleDecl *Mod, StringRef OutDir) {
1229+
using namespace llvm::sys;
1230+
1231+
auto getOutPath = [&](SourceFile *SF) -> std::string {
1232+
SmallString<256> Path = OutDir;
1233+
StringRef Filename = SF->getFilename();
1234+
path::append(Path, path::filename(Filename));
1235+
return std::string(Path.str());
1236+
};
1237+
1238+
std::unordered_set<std::string> Filenames;
1239+
1240+
auto dumpFile = [&](SourceFile *SF) -> bool {
1241+
SmallString<512> TempBuf;
1242+
llvm::raw_svector_ostream TempOS(TempBuf);
1243+
1244+
PrintOptions PO = PrintOptions::printInterface();
1245+
PO.PrintOriginalSourceText = true;
1246+
PO.Indent = 2;
1247+
PO.PrintAccess = false;
1248+
PO.SkipUnderscoredStdlibProtocols = true;
1249+
SF->print(TempOS, PO);
1250+
if (TempOS.str().trim().empty())
1251+
return false; // nothing to show.
1252+
1253+
std::string OutPath = getOutPath(SF);
1254+
bool WasInserted = Filenames.insert(OutPath).second;
1255+
if (!WasInserted) {
1256+
llvm::errs() << "multiple source files ended up with the same dump API "
1257+
"filename to write to: " << OutPath << '\n';
1258+
return true;
1259+
}
1260+
1261+
std::error_code EC;
1262+
llvm::raw_fd_ostream OS(OutPath, EC, fs::FA_Read | fs::FA_Write);
1263+
if (EC) {
1264+
llvm::errs() << "error opening file '" << OutPath << "': "
1265+
<< EC.message() << '\n';
1266+
return true;
1267+
}
1268+
1269+
OS << TempOS.str();
1270+
return false;
1271+
};
1272+
1273+
std::error_code EC = fs::create_directories(OutDir);
1274+
if (EC) {
1275+
llvm::errs() << "error creating directory '" << OutDir << "': "
1276+
<< EC.message() << '\n';
1277+
return true;
1278+
}
1279+
1280+
for (auto *FU : Mod->getFiles()) {
1281+
if (auto *SF = dyn_cast<SourceFile>(FU))
1282+
if (dumpFile(SF))
1283+
return true;
1284+
}
1285+
1286+
return false;
1287+
}
1288+
12271289
/// Perform any actions that must have access to the ASTContext, and need to be
12281290
/// delayed until the Swift compile pipeline has finished. This may be called
12291291
/// before or after LLVM depending on when the ASTContext gets freed.
@@ -1796,68 +1858,6 @@ static void emitIndexDataForSourceFile(SourceFile *PrimarySourceFile,
17961858
}
17971859
}
17981860

1799-
/// Returns true if an error occurred.
1800-
static bool dumpAPI(ModuleDecl *Mod, StringRef OutDir) {
1801-
using namespace llvm::sys;
1802-
1803-
auto getOutPath = [&](SourceFile *SF) -> std::string {
1804-
SmallString<256> Path = OutDir;
1805-
StringRef Filename = SF->getFilename();
1806-
path::append(Path, path::filename(Filename));
1807-
return std::string(Path.str());
1808-
};
1809-
1810-
std::unordered_set<std::string> Filenames;
1811-
1812-
auto dumpFile = [&](SourceFile *SF) -> bool {
1813-
SmallString<512> TempBuf;
1814-
llvm::raw_svector_ostream TempOS(TempBuf);
1815-
1816-
PrintOptions PO = PrintOptions::printInterface();
1817-
PO.PrintOriginalSourceText = true;
1818-
PO.Indent = 2;
1819-
PO.PrintAccess = false;
1820-
PO.SkipUnderscoredStdlibProtocols = true;
1821-
SF->print(TempOS, PO);
1822-
if (TempOS.str().trim().empty())
1823-
return false; // nothing to show.
1824-
1825-
std::string OutPath = getOutPath(SF);
1826-
bool WasInserted = Filenames.insert(OutPath).second;
1827-
if (!WasInserted) {
1828-
llvm::errs() << "multiple source files ended up with the same dump API "
1829-
"filename to write to: " << OutPath << '\n';
1830-
return true;
1831-
}
1832-
1833-
std::error_code EC;
1834-
llvm::raw_fd_ostream OS(OutPath, EC, fs::FA_Read | fs::FA_Write);
1835-
if (EC) {
1836-
llvm::errs() << "error opening file '" << OutPath << "': "
1837-
<< EC.message() << '\n';
1838-
return true;
1839-
}
1840-
1841-
OS << TempOS.str();
1842-
return false;
1843-
};
1844-
1845-
std::error_code EC = fs::create_directories(OutDir);
1846-
if (EC) {
1847-
llvm::errs() << "error creating directory '" << OutDir << "': "
1848-
<< EC.message() << '\n';
1849-
return true;
1850-
}
1851-
1852-
for (auto *FU : Mod->getFiles()) {
1853-
if (auto *SF = dyn_cast<SourceFile>(FU))
1854-
if (dumpFile(SF))
1855-
return true;
1856-
}
1857-
1858-
return false;
1859-
}
1860-
18611861
/// Creates a diagnostic consumer that handles dispatching diagnostics to
18621862
/// multiple output files, based on the supplementary output paths specified by
18631863
/// \p inputsAndOutputs.

0 commit comments

Comments
 (0)