Skip to content

Commit

Permalink
Ensure presumed source file paths are canonical when using a compilat…
Browse files Browse the repository at this point in the history
…ion database. (#532)

Fixes #529.
  • Loading branch information
mattmccutchen-cci authored Apr 7, 2021
1 parent 4a6ae1d commit e012f43
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
33 changes: 33 additions & 0 deletions clang/lib/3C/3C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,31 @@ ArgumentsAdjuster addVerifyAdjuster() {
return AdjustedArgs;
};
}
ArgumentsAdjuster canonicalizeSourceFilePathAdjuster() {
return [](const CommandLineArguments &Args, StringRef FileName) {
std::string AbsoluteFp;
std::error_code EC = tryGetCanonicalFilePath(std::string(FileName), AbsoluteFp);
assert(!EC && "We canonicalized the source file path during 3C "
"initialization; doing it again in "
"canonicalizeSourceFilePathAdjuster shouldn't fail.");
CommandLineArguments AdjustedArgs;
for (auto Arg : Args) {
if (Arg == FileName) {
// XXX We are assuming that any occurrence of the source file path as an
// argument should be replaced with the canonical file path. It might be
// safer to only match the argument that represents the input file.
// Unfortunately, it's hard to do here short of reimplementing compiler
// command line parsing. It would be better to adjust the parsed option
// data structures instead
// (https://github.com/correctcomputation/checkedc-clang/issues/536).
AdjustedArgs.push_back(AbsoluteFp);
} else {
AdjustedArgs.push_back(Arg);
}
}
return AdjustedArgs;
};
}

void dumpConstraintOutputJson(const std::string &PostfixStr,
ProgramInfo &Info) {
Expand Down Expand Up @@ -302,6 +327,14 @@ bool _3CInterface::parseASTs() {
// for status.
if (VerifyDiagnosticOutput)
Tool->appendArgumentsAdjuster(addVerifyAdjuster());
// We canonicalize source file paths in the _3CInterface constructor, but the
// compilation database may override the path with a relative or otherwise
// non-canonical path, which may then affect the presumed filename, which is
// used in the PersistentSourceLoc. Since we need PersistentSourceLocs to be
// unambiguous, we canonicalize the source file path again here. We may still
// get relative presumed filenames from relative -I paths, though
// convert_project tries to change the -I options to avoid this.
Tool->appendArgumentsAdjuster(canonicalizeSourceFilePathAdjuster());

// load the ASTs
if (Tool->buildASTs(ASTs))
Expand Down
4 changes: 4 additions & 0 deletions clang/tools/3c/utils/port_tools/generate_ccommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ def run3C(checkedc_bin, extra_3c_args,
# 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")
Expand Down

0 comments on commit e012f43

Please sign in to comment.