[ASTImporter] Prevent the ASTImporter from creating multiple main FileIDs.
authorRaphael Isemann <teemperor@gmail.com>
Mon, 17 Feb 2020 18:43:33 +0000 (19:43 +0100)
committerRaphael Isemann <teemperor@gmail.com>
Tue, 18 Feb 2020 07:37:26 +0000 (08:37 +0100)
commita37734f643fbac04ce6d7f2f6b1c8dcd3c4d5e0e
tree63125bef11bdd44cf673e1c988689b6fd08d2fbc
parent69906fe0b1812bb5be8028b21dfba4b78af351ba
[ASTImporter] Prevent the ASTImporter from creating multiple main FileIDs.

Summary:
When importing the main FileID the ASTImporter currently gives it no include location. This means
that any SourceLocations produced for this FileID look to Clang as if they are coming from the
main FileID (as the main FileID has no include location).

Clang seems to expect that there is only one main FileID in one translation unit (which makes sense
during normal compilation), so this behavior leads to several problems when producing diagnostics,
one being that when calling `SourceManager::isBeforeInTranslationUnit` on two SourceLocations
that come from two different ASTContext instances, Clang fails to sort the SourceLocations as
the include chains of the FileIDs don't end up in a single FileID. This causes that Clang crashes
with "Unsortable locations found" in this function.

This patch gives any imported main FileIDs the main FileID of the To ASTContext as its include
location. This allows Clang to sort all imported SourceLocations as now all include chains point
to the main FileID of the To ASTContext. The exact include location is currently set to the start
of the To main file (just because that should always be a valid SourceLocation).

Reviewers: martong, a_sidorin, a.sidorin, shafik, balazske

Reviewed By: martong, a_sidorin, shafik

Subscribers: balazske, rnkovacs, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D74542
clang/lib/AST/ASTImporter.cpp
clang/unittests/AST/ASTImporterTest.cpp