Skip to content

Commit

Permalink
Add -I adjustment to _3CASTBuilderAction.
Browse files Browse the repository at this point in the history
Fixes #515.
  • Loading branch information
mattmccutchen-cci committed Jun 11, 2021
1 parent 78f3ef3 commit f996422
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
28 changes: 26 additions & 2 deletions clang/lib/3C/3C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,12 @@ class _3CASTBuilderAction : public ToolAction {
// fixed another way, then making the path absolute would be sufficient to
// fix https://github.com/correctcomputation/checkedc-clang/issues/515; the
// path wouldn't need to be canonical.
std::error_code EC;
SmallVectorImpl<FrontendInputFile> &Inputs = Invocation->getFrontendOpts().Inputs;
for (FrontendInputFile *Iter = Inputs.begin(); Iter < Inputs.end(); Iter++) {
FrontendInputFile &OldInput = *Iter;
// getFile will assert that the input is a file, which should be true for 3C.
std::string OldPath = std::string(OldInput.getFile()), NewPath;
EC = tryGetCanonicalFilePath(OldPath, NewPath);
std::error_code EC = tryGetCanonicalFilePath(OldPath, NewPath);
if (EC) {
errs() << "3C error: Failed to re-canonicalize source file path "
<< OldPath << " during compiler invocation "
Expand All @@ -119,6 +118,31 @@ class _3CASTBuilderAction : public ToolAction {
*Iter = FrontendInputFile(NewPath, OldInput.getKind(), OldInput.isSystem());
}

// Canonicalize -I paths to address the same two issues for `#include`d
// files. The situation is analogous except that this is not a complete fix
// for https://github.com/correctcomputation/checkedc-clang/issues/604
// because the portion of the path in the `#include` directive may be
// non-canonical.
HeaderSearchOptions &HeaderOpts = *Invocation->HeaderSearchOpts;
std::vector<HeaderSearchOptions::Entry> NewUserEntries;
for (auto It = HeaderOpts.UserEntries.begin(); It != HeaderOpts.UserEntries.end(); ) {
HeaderSearchOptions::Entry &Entry = *It;
std::string OldPath = Entry.Path, NewPath;
std::error_code EC = tryGetCanonicalFilePath(OldPath, NewPath);
if (EC) {
// Normally, if an -I directory isn't accessible, Clang seems to ignore
// it with no diagnostic. Hopefully, removing it from the list here will
// have the same effect and not break anything. If we kept the
// non-canonical entry, we might benefit from any diagnostic that Clang
// might issue in the future, but it's harder to reason about whether
// that might re-expose the original bugs.
It = HeaderOpts.UserEntries.erase(It);
} else {
Entry.Path = NewPath;
It++;
}
}

// Finally, actually build the AST. This part is the same as in
// ASTBuilderAction::runInvocation.

Expand Down
14 changes: 0 additions & 14 deletions clang/tools/3c/utils/port_tools/generate_ccommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ def run3C(checkedc_bin, extra_3c_args,

translation_units: List[TranslationUnitInfo] = []
all_files = []
absolute_include_dirs = set()
for i in cmds:
file_to_add = i['file']
compiler_path = None # XXX Clean this up
Expand Down Expand Up @@ -165,9 +164,6 @@ def run3C(checkedc_bin, extra_3c_args,
target_directory, file_to_add,
output_filename)
translation_units.append(tu)
for arg in compiler_x_args:
if arg.startswith('-I'):
absolute_include_dirs.add(tu.realpath(arg[len('-I'):]))

expandMacros(expand_macros_opts, compilation_base_dir, translation_units)

Expand Down Expand Up @@ -223,16 +219,6 @@ def run3C(checkedc_bin, extra_3c_args,
args.append('-p')
args.append(compile_commands_json)
args.append('-extra-arg=-w')
# This is a workaround for a bug in Clang LibTooling that affects the use of
# relative paths with -I when different translation units have different
# working directories. For details, see
# https://github.com/correctcomputation/checkedc-clang/issues/515 .
#
# Even if the above issue were fixed, we may still need this in some cases
# to ensure that PersistentSourceLoc filenames are unambiguous: see the
# comment in _3CInterface::parseASTs in clang/lib/3C/3C.cpp .
for dir in absolute_include_dirs:
args.append('-extra-arg-before=-I' + dir)
vcodewriter.addClangdArg("-log=verbose")
vcodewriter.addClangdArg(args[1:])
args.append('-base-dir="' + compilation_base_dir + '"')
Expand Down

0 comments on commit f996422

Please sign in to comment.