Skip to content

Instantly share code, notes, and snippets.

@RFullum
Created April 18, 2023 19:54
Show Gist options
  • Save RFullum/16134600c979fd18bdc4f1a85a87d7a0 to your computer and use it in GitHub Desktop.
Save RFullum/16134600c979fd18bdc4f1a85a87d7a0 to your computer and use it in GitHub Desktop.
Find header files in Juce app that don't have an #include anywhere other than their corresponding source file
/*
Put somewhere in Juce app (ie. ctor of MainComponent) and run app. Will
print to console the name of the header files that don't have an #include
in the code other than in their corresponding .cpp file.
*/
DBG(" ");
auto sourceDir = juce::File("~/Rode/RodeCentral/rode_central/RODE Central/Source");
std::vector<juce::File> codeFiles; // .h and .cpp
std::vector<juce::File> headerFiles; // just .h
std::vector<juce::File> unusedHeaderFiles;
for (juce::DirectoryEntry entry : juce::RangedDirectoryIterator(sourceDir, true))
{
auto file = entry.getFile();
auto headerExt = juce::String(".h");
auto sourceExt = juce::String(".cpp");
if (file.getFileExtension() == headerExt || file.getFileExtension() == sourceExt)
{
codeFiles.emplace_back(file);
if (file.getFileExtension() == headerExt)
headerFiles.emplace_back(file);
}
}
unusedHeaderFiles = headerFiles;
for (auto header : headerFiles)
{
for (auto codeFile : codeFiles)
{
const auto headerNameNoExt = header.getFileNameWithoutExtension();
const auto codeNameNoExt = codeFile.getFileNameWithoutExtension();
if (headerNameNoExt != codeNameNoExt)
{
auto fileText = codeFile.loadFileAsString();
auto headerName = header.getFileName();
if (fileText.contains(headerName))
{
for (int i = 0; i < unusedHeaderFiles.size(); ++i)
{
if (unusedHeaderFiles[i] == header)
unusedHeaderFiles.erase(unusedHeaderFiles.begin() + i);
}
}
}
}
}
for (auto unused : unusedHeaderFiles)
DBG(unused.getFileName());
DBG(" ");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment