5

I have this cmake project that I would like to modernize using clang-tidy. In order not to have too many things happening at once, I only activated the modernize-use-override option. However, when I apply this:

$> run-clang-tidy  -header-filter='.*'  -checks='-*, modernize-use-override' -fix

to the project, clang-tidy inserts multiple instances of the override specifier, for example:

void update_sizes() override override override etc.

I tried to follow the advice given here and used cmake to create a json compile command data base:

$>cmake ../../ -DCMAKE_BUILD_TYPE=debug -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

I made sure cmake is actually picking up the clang compiler by setting it to the system-wide default (using update-alternative). In this case, cmake generates make files that invoke the clang compiler.

I'm using Ubuntu 18.10 and clang 7.

I also tried this using clang-6 and setting cmake to generate ninja build scripts instead of make files, but the result is always the same.

The whole project compiles fine both with gcc as well as clang, before the fix is applied.

Please note that there is a similar discussion here, however the advice given there is to use run-clang-tidy.py, which is exactly what I'm doing. Therefore, I don't consider this a duplicate.

ThorOdinsson
  • 252
  • 1
  • 2
  • 7

2 Answers2

3

This is the same problem referenced in the discussion you linked to and it looks like a bug to my eyes. You have several options:

  1. Report this as a bug to clang. Wait for a long time.
  2. Fix the bug yourself. Invest a lot of your time.
  3. Use any tool with a capability to do text replacement over multiple files/directories (e.g. Notepad++). Find and replace all "override override" occurrences with "override". Repeat until there are no more occurrences. Shiver at the ugliness of the solution you used. :)
pablo285
  • 2,460
  • 4
  • 14
  • 38
  • 1
    Thank you for your answer. Fixing this by editing the files afterwards is probably not the best of options, because as soon as I turn on other checks, I will have to figure out how to deal with that. Therefore I was hoping for something that prevents this from happening in the first place. Since I have only spent about 1 hour using this tool, I didn't consider it appropriate of me to start submitting a ticket. I thought it more likely that I'm not doing things right... – ThorOdinsson Nov 10 '18 at 09:47
2

This is pretty old question, but problem still exist in clang 11. The problem is with the way clang stores the paths to the header files. You can easy see this in yaml files e.g

In the first yaml file:
FilePath: 'C:/SOURCES/APP/COMMON/CORE/../../../Libs/Sdk/Public/File.h'

In the secod yaml file:
FilePath: 'C:/SOURCES/APP/COMMON/APPCORE/VIEWS/../../../../Libs/Sdk/Public/File.h'

Above paths are the same, but not for 'clang apply replacements'.
The easiest way to fix this problem is modify script 'run-clang-tidy.py' to fix all paths in the yaml files before run 'clang apply replacements'

def FixPath(line):
    value = line.find("'")
    if value != -1:
        left = line[:value]
        right = line[value+1:len(line)-2]
        right = "'" + os.path.normpath(right) + "'"
        line = left + right.lower() + "\n"
    return line

def FixYamlContent(tmpdir):
    for replacefile in glob.iglob(os.path.join(tmpdir, '*.yaml')):
        with open(replacefile, "r+") as file_rw:
            lines = []
            for line in file_rw:
                if line.find("FilePath:") != -1:
                    line = FixPath(line)
                    
                lines.append(line)

            file_rw.seek(0)
            file_rw.writelines(lines)
            file_rw.truncate()

To use above code you need to call function FixYamlContent(tmpdir) just before function apply_fixes. I'm not a python developer and above code is not fully tested, but you get the idea.

unknown
  • 21
  • 2