<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Incorrect include paths applied from compile_commands.json"
   href="https://bugs.llvm.org/show_bug.cgi?id=38540">38540</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Incorrect include paths applied from compile_commands.json
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Frontend
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>pradelle@silexica.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=20686" name="attach_20686" title="Example reproduction for the issue">attachment 20686</a> <a href="attachment.cgi?id=20686&action=edit" title="Example reproduction for the issue">[details]</a></span>
Example reproduction for the issue

When the path to a particular include directory is passed to successive
compiler invocations using a compilation database (compile_commands.json file),
the first path is cached and reused for all invocations. When the first path in
the database is relative, it gets reused without fixing it according to the
location in which the different invocations are running. As a consequence, some
header files are reported as missing whereas they should be found.

# Reproduction

Create a source directory with source files in at least two directories of
different nesting such as:

main.c
subdirectory/src.c
include/header.h

The source files must include the header file.

Generate a compile_commands.json file such that:
 - the first command adds the "include" directory using a relative path (for
instance "-I include" for "main.c")
 - the second command adds the same "include" directory using its absolute path

Run "clang-check" with "-p path_to_compile_commands.json_directory". Notice how
the preprocessing of the second C file fails by reporting a missing header
file.

A similar example is provided as an attachment. Run it with "make" in the
directory where the Makefile is located (root directory).

# Suggested fix

It looks like the issued is caused by the caching of directory entries in
frontend/InitHeaderSearch.cpp (in AddUnmappedPath). Every directory is searched
for in a FileManager that caches all the paths seen so far, including in
previous invocations, and returns the first entry cached.

The problem is solved by making every path absolute before searching for them
in the FileManager.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>