zixuw updated this revision to Diff 424959.
zixuw added a comment.
- Use first match in `getRelativeIncludeName`
- Try to get Preprocessor and reverse lookup headermaps in
`getRelativeIncludeName`
- Use `getRelativeIncludeName` to check for known files in `LocationFileChecker`
- Misc fixes & adjustments
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D123831/new/
https://reviews.llvm.org/D123831
Files:
clang/lib/ExtractAPI/ExtractAPIConsumer.cpp
clang/test/ExtractAPI/relative_include.m
Index: clang/test/ExtractAPI/relative_include.m
===================================================================
--- /dev/null
+++ clang/test/ExtractAPI/relative_include.m
@@ -0,0 +1,130 @@
+// RUN: rm -rf %t
+// RUN: split-file %s %t
+
+// Setup framework root
+// RUN: mkdir -p %t/Frameworks/MyFramework.framework/Headers
+// RUN: cp %t/MyFramework.h %t/Frameworks/MyFramework.framework/Headers/
+// RUN: cp %t/MyHeader.h %t/Frameworks/MyFramework.framework/Headers/
+
+// RUN: sed -e "s@SRCROOT@%{/t:regex_replacement}@g" \
+// RUN: %t/reference.output.json.in >> %t/reference.output.json
+
+// Headermap maps headers to the source root SRCROOT
+// RUN: sed -e "s@SRCROOT@%{/t:regex_replacement}@g" \
+// RUN: %t/headermap.hmap.json.in >> %t/headermap.hmap.json
+// RUN: %hmaptool write %t/headermap.hmap.json %t/headermap.hmap
+
+// Input headers use paths to the framework root/DSTROOT
+// RUN: %clang -extract-api --product-name=MyFramework -target arm64-apple-macosx \
+// RUN: -I%t/headermap.hmap -F%t/Frameworks \
+// RUN: %t/Frameworks/MyFramework.framework/Headers/MyFramework.h \
+// RUN: %t/Frameworks/MyFramework.framework/Headers/MyHeader.h \
+// RUN: -o %t/output.json | FileCheck -allow-empty %s
+
+// Generator version is not consistent across test runs, normalize it.
+// RUN: sed -e "s@\"generator\": \".*\"@\"generator\": \"?\"@g" \
+// RUN: %t/output.json >> %t/output-normalized.json
+// RUN: diff %t/reference.output.json %t/output-normalized.json
+
+// CHECK-NOT: error:
+// CHECK-NOT: warning:
+
+//--- headermap.hmap.json.in
+{
+ "mappings" :
+ {
+ "MyFramework/MyHeader.h" : "SRCROOT/MyHeader.h"
+ }
+}
+
+//--- MyFramework.h
+// Umbrella for MyFramework
+#import <MyFramework/MyHeader.h>
+
+//--- MyHeader.h
+#import <OtherFramework/OtherHeader.h>
+int MyInt;
+
+//--- Frameworks/OtherFramework.framework/Headers/OtherHeader.h
+int OtherInt;
+
+//--- reference.output.json.in
+{
+ "metadata": {
+ "formatVersion": {
+ "major": 0,
+ "minor": 5,
+ "patch": 3
+ },
+ "generator": "?"
+ },
+ "module": {
+ "name": "MyFramework",
+ "platform": {
+ "architecture": "arm64",
+ "operatingSystem": {
+ "minimumVersion": {
+ "major": 11,
+ "minor": 0,
+ "patch": 0
+ },
+ "name": "macosx"
+ },
+ "vendor": "apple"
+ }
+ },
+ "relationships": [],
+ "symbols": [
+ {
+ "accessLevel": "public",
+ "declarationFragments": [
+ {
+ "kind": "typeIdentifier",
+ "preciseIdentifier": "c:I",
+ "spelling": "int"
+ },
+ {
+ "kind": "text",
+ "spelling": " "
+ },
+ {
+ "kind": "identifier",
+ "spelling": "MyInt"
+ }
+ ],
+ "identifier": {
+ "interfaceLanguage": "c",
+ "precise": "c:@MyInt"
+ },
+ "kind": {
+ "displayName": "Global Variable",
+ "identifier": "c.var"
+ },
+ "location": {
+ "position": {
+ "character": 5,
+ "line": 2
+ },
+ "uri": "file://SRCROOT/MyHeader.h"
+ },
+ "names": {
+ "navigator": [
+ {
+ "kind": "identifier",
+ "spelling": "MyInt"
+ }
+ ],
+ "subHeading": [
+ {
+ "kind": "identifier",
+ "spelling": "MyInt"
+ }
+ ],
+ "title": "MyInt"
+ },
+ "pathComponents": [
+ "MyInt"
+ ]
+ }
+ ]
+}
Index: clang/lib/ExtractAPI/ExtractAPIConsumer.cpp
===================================================================
--- clang/lib/ExtractAPI/ExtractAPIConsumer.cpp
+++ clang/lib/ExtractAPI/ExtractAPIConsumer.cpp
@@ -38,7 +38,10 @@
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Regex.h"
#include "llvm/Support/raw_ostream.h"
#include <memory>
#include <utility>
@@ -55,10 +58,128 @@
return {};
}
+Optional<std::string> getRelativeIncludeName(const CompilerInstance &CI,
+ StringRef File) {
+ using namespace llvm::sys;
+ // Matches framework include patterns
+ const llvm::Regex Rule("/(.+)\\.framework/(.+)?Headers/(.+)");
+
+ // FIXME: WorkingDir might not be set at this point.
+ StringRef WorkingDir = CI.getFileSystemOpts().WorkingDir;
+
+ SmallString<128> FilePath(File.begin(), File.end());
+ if (!WorkingDir.empty() && !path::is_absolute(FilePath))
+ fs::make_absolute(WorkingDir, FilePath);
+ path::remove_dots(FilePath, true);
+ File = FilePath;
+
+ // Checks whether `Dir` is a strict path prefix of `File`. If so returns
+ // the prefix length. Otherwise return 0.
+ auto CheckDir = [&](llvm::StringRef Dir) -> unsigned {
+ llvm::SmallString<32> DirPath(Dir.begin(), Dir.end());
+ if (!WorkingDir.empty() && !path::is_absolute(DirPath))
+ fs::make_absolute(WorkingDir, DirPath);
+ path::remove_dots(DirPath, true);
+ Dir = DirPath;
+ for (auto NI = path::begin(File), NE = path::end(File),
+ DI = path::begin(Dir), DE = path::end(Dir);
+ /*termination condition in loop*/; ++NI, ++DI) {
+ // '.' components in File are ignored.
+ while (NI != NE && *NI == ".")
+ ++NI;
+ if (NI == NE)
+ break;
+
+ // '.' components in Dir are ignored.
+ while (DI != DE && *DI == ".")
+ ++DI;
+
+ // Dir is a prefix of File, up to '.' components and choice of path
+ // separators.
+ if (DI == DE)
+ return NI - path::begin(File);
+
+ // Consider all path separators equal.
+ if (NI->size() == 1 && DI->size() == 1 &&
+ path::is_separator(NI->front()) && path::is_separator(DI->front()))
+ continue;
+
+ // Special case Apple .sdk folders since the search path is typically a
+ // symlink like `iPhoneSimulator14.5.sdk` while the file is instead
+ // located in `iPhoneSimulator.sdk` (the real folder).
+ if (NI->endswith(".sdk") && DI->endswith(".sdk")) {
+ StringRef NBasename = path::stem(*NI);
+ StringRef DBasename = path::stem(*DI);
+ if (DBasename.startswith(NBasename))
+ continue;
+ }
+
+ if (*NI != *DI)
+ break;
+ }
+ return 0;
+ };
+
+ unsigned PrefixLength = 0;
+
+ // Go through the search paths and find the first one that is a prefix of
+ // the header.
+ for (const auto &Entry : CI.getHeaderSearchOpts().UserEntries) {
+ PrefixLength = CheckDir(Entry.Path);
+ if (PrefixLength > 0) {
+ // The header is found in a framework path.
+ if (Entry.IsFramework) {
+ SmallVector<StringRef, 4> Matches;
+ Rule.match(File, &Matches);
+ // Returned matches are always in stable order.
+ if (Matches.size() != 4)
+ return None;
+
+ return path::convert_to_slash(
+ (Matches[1].drop_front(Matches[1].rfind('/') + 1) + "/" +
+ Matches[3])
+ .str());
+
+ }
+
+ // The header is found in a normal search path.
+ return path::convert_to_slash(File.drop_front(PrefixLength));
+ }
+ }
+
+ // We didn't find any entry that leads to this header, try the current
+ // working directory.
+ // FIXME: WorkingDir might not be set at this point.
+ PrefixLength = CheckDir(CI.getFileSystemOpts().WorkingDir);
+ if (PrefixLength > 0)
+ return path::convert_to_slash(File.drop_front(PrefixLength));
+
+ // FIXME: reverse lookup headermaps to see if we can find a match there.
+ // But headermaps info is available through Preprocessor, which we might
+ // not have yet at this point when called from PrepareToExecuteAction.
+ if (CI.hasPreprocessor()) {
+ // Try to reverse lookup headermaps to see if the path has been remapped.
+ for (const DirectoryLookup &DL :
+ CI.getPreprocessor().getHeaderSearchInfo().search_dir_range()) {
+ if (!DL.isHeaderMap())
+ continue;
+
+ StringRef SpelledFilename =
+ DL.getHeaderMap()->reverseLookupFilename(File);
+ if (!SpelledFilename.empty())
+ return SpelledFilename.str();
+ }
+ }
+
+ // Couldn't determine a include name, use full path instead.
+ return None;
+}
+
struct LocationFileChecker {
bool isLocationInKnownFile(SourceLocation Loc) {
// If the loc refers to a macro expansion we need to first get the file
// location of the expansion.
+ auto &SM = CI.getSourceManager();
auto FileLoc = SM.getFileLoc(Loc);
FileID FID = SM.getFileID(FileLoc);
if (FID.isInvalid())
@@ -71,20 +192,39 @@
if (KnownFileEntries.count(File))
return true;
+ if (ExternalFileEntries.count(File))
+ return false;
+
+ StringRef FileName = File->tryGetRealPathName().empty()
+ ? File->getName()
+ : File->tryGetRealPathName();
+
+ // Try to reduce the include name the same way we tried to include it.
+ if (auto IncludeName = getRelativeIncludeName(CI, FileName))
+ if (llvm::find(KnownFiles, *IncludeName) != KnownFiles.end()) {
+ KnownFileEntries.insert(File);
+ return true;
+ }
+
+ // Record that the file was not found to avoid future reverse lookup for
+ // the same file.
+ ExternalFileEntries.insert(File);
return false;
}
- LocationFileChecker(const SourceManager &SM,
- const std::vector<std::string> &KnownFiles)
- : SM(SM) {
+ LocationFileChecker(const CompilerInstance &CI,
+ std::vector<std::string> &KnownFiles)
+ : CI(CI), KnownFiles(KnownFiles), ExternalFileEntries() {
for (const auto &KnownFilePath : KnownFiles)
- if (auto FileEntry = SM.getFileManager().getFile(KnownFilePath))
+ if (auto FileEntry = CI.getFileManager().getFile(KnownFilePath))
KnownFileEntries.insert(*FileEntry);
}
private:
- const SourceManager &SM;
+ const CompilerInstance &CI;
+ std::vector<std::string> &KnownFiles;
llvm::DenseSet<const FileEntry *> KnownFileEntries;
+ llvm::DenseSet<const FileEntry *> ExternalFileEntries;
};
/// The RecursiveASTVisitor to traverse symbol declarations and collect API
@@ -743,8 +883,7 @@
CI.getTarget().getTriple(),
CI.getFrontendOpts().Inputs.back().getKind().getLanguage());
- auto LCF = std::make_unique<LocationFileChecker>(CI.getSourceManager(),
- KnownInputFiles);
+ auto LCF = std::make_unique<LocationFileChecker>(CI, KnownInputFiles);
CI.getPreprocessor().addPPCallbacks(std::make_unique<MacroCallback>(
CI.getSourceManager(), *LCF, *API, CI.getPreprocessor()));
@@ -767,11 +906,19 @@
HeaderContents += "#import";
else
HeaderContents += "#include";
- HeaderContents += " \"";
- HeaderContents += FIF.getFile();
- HeaderContents += "\"\n";
- KnownInputFiles.emplace_back(FIF.getFile());
+ StringRef FilePath = FIF.getFile();
+ if (auto RelativeName = getRelativeIncludeName(CI, FilePath)) {
+ HeaderContents += " <";
+ HeaderContents += *RelativeName;
+ HeaderContents += ">\n";
+ KnownInputFiles.emplace_back(*RelativeName);
+ } else {
+ HeaderContents += " \"";
+ HeaderContents += FilePath;
+ HeaderContents += "\"\n";
+ KnownInputFiles.emplace_back(FilePath);
+ }
}
Buffer = llvm::MemoryBuffer::getMemBufferCopy(HeaderContents,
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits