2
votes

I have problem with finding main include for my source files. But step by step.

File z.h

#include "test"
#include "azd"
#include <vector> 
#include <boost/something>
#include <map>

File z.C

#include "test"
#include "azd"
#include <vector>
#include <boost/something>
#include <map>
#include "z.h"

File (part) .clang-format located together with z.h and z.C

BasedOnStyle: LLVM
Language: Cpp
IncludeIsMainRegex: '(_test)?$'
IncludeCategories:
- Regex:    '^(<boost)'
  Priority: 3
- Regex:    '^<'
  Priority: 4
- Regex:    '.*'
  Priority: 1

Now, testing:

$ clang-format z.h

#include "azd"
#include "test"
#include <boost/something>
#include <map>
#include <vector>

Here, everything looks great, includes sorted as expected, but

$ clang-format z.C

#include "azd"
#include "test"
#include "z.h"
#include <boost/something>
#include <map>
#include <vector>

Here, "a.z" is on the middle, although is should be detected by '(_test)?$' as main include. The funny part is that when I rename z.C -> z.cc and then:

$ clang-format z.cc

#include "z.h"
#include "azd"
#include "test"
#include <boost/something>
#include <map>
#include <vector>

works OK...

It seems that clang-format does not recognize *.C files as C++ language. I am working on C++ project where I do not have luxury to rename all *.C files to *.cc, so please can someone tell me how to force clang-format to use C++ style formatting on *.C files? Or to solve this problem with finding main include with another approach?

And second issue/question. We sometime split templates declarations and definitions into template.h and templateImpl.h files. How to force clang-format to treat templateImpl.h as source and put #include "template.h" as main include?

Regards

1

1 Answers

0
votes

Looks like the only thing which doesn't work in your case is IncludeIsMainRegex option when a source file has *.C extension.

If I am not mistaken, then this option currently works only with source files which have *.c, *.cc, *.cpp, *.c++, *.cxx extensions.

My assumptions is based on this snippet of code from clang (v7.0.0), file clang/lib/Tooling/Inclusions/HeaderIncludes.cpp:

IncludeCategoryManager::IncludeCategoryManager(const IncludeStyle &Style,
                                               StringRef FileName)
    : Style(Style), FileName(FileName) {
  FileStem = llvm::sys::path::stem(FileName);
  for (const auto &Category : Style.IncludeCategories)
    CategoryRegexs.emplace_back(Category.Regex, llvm::Regex::IgnoreCase);
  IsMainFile = FileName.endswith(".c") || FileName.endswith(".cc") ||
               FileName.endswith(".cpp") || FileName.endswith(".c++") ||
               FileName.endswith(".cxx") || FileName.endswith(".m") ||
               FileName.endswith(".mm");
}

int IncludeCategoryManager::getIncludePriority(StringRef IncludeName,
                                               bool CheckMainHeader) const {
  int Ret = INT_MAX;
  for (unsigned i = 0, e = CategoryRegexs.size(); i != e; ++i)
    if (CategoryRegexs[i].match(IncludeName)) {
      Ret = Style.IncludeCategories[i].Priority;
      break;
    }
  if (CheckMainHeader && IsMainFile && Ret > 0 && isMainHeader(IncludeName))
    Ret = 0;
  return Ret;
}

bool IncludeCategoryManager::isMainHeader(StringRef IncludeName) const {
  if (!IncludeName.startswith("\""))
    return false;
  StringRef HeaderStem =
      llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1));
  if (FileStem.startswith(HeaderStem) ||
      FileStem.startswith_lower(HeaderStem)) {
    llvm::Regex MainIncludeRegex(HeaderStem.str() + Style.IncludeIsMainRegex,
                                 llvm::Regex::IgnoreCase);
    if (MainIncludeRegex.match(FileStem))
      return true;
  }
  return false;
}