forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request swiftlang#27924 from jrose-apple/attribute-attribu…
…tion [ClangImporter] Diagnose bad swift_name attributes better
- Loading branch information
Showing
15 changed files
with
259 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
//===--- ClangSourceBufferImporter.cpp - Map Clang buffers to Swift -------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#import "ClangSourceBufferImporter.h" | ||
#import "swift/Basic/SourceManager.h" | ||
#import "clang/Basic/SourceManager.h" | ||
#import "llvm/Support/MemoryBuffer.h" | ||
|
||
using namespace swift; | ||
using namespace swift::importer; | ||
|
||
static SourceLoc findEndOfLine(SourceManager &SM, SourceLoc loc, | ||
unsigned bufferID) { | ||
CharSourceRange entireBuffer = SM.getRangeForBuffer(bufferID); | ||
CharSourceRange rangeFromLoc{SM, loc, entireBuffer.getEnd()}; | ||
StringRef textFromLoc = SM.extractText(rangeFromLoc); | ||
size_t newlineOffset = textFromLoc.find_first_of({"\r\n\0", 3}); | ||
if (newlineOffset == StringRef::npos) | ||
return entireBuffer.getEnd(); | ||
return loc.getAdvancedLoc(newlineOffset); | ||
} | ||
|
||
SourceLoc ClangSourceBufferImporter::resolveSourceLocation( | ||
const clang::SourceManager &clangSrcMgr, | ||
clang::SourceLocation clangLoc) { | ||
SourceLoc loc; | ||
|
||
clangLoc = clangSrcMgr.getFileLoc(clangLoc); | ||
auto decomposedLoc = clangSrcMgr.getDecomposedLoc(clangLoc); | ||
if (decomposedLoc.first.isInvalid()) | ||
return loc; | ||
|
||
auto buffer = clangSrcMgr.getBuffer(decomposedLoc.first); | ||
unsigned mirrorID; | ||
|
||
auto mirrorIter = mirroredBuffers.find(buffer); | ||
if (mirrorIter != mirroredBuffers.end()) { | ||
mirrorID = mirrorIter->second; | ||
} else { | ||
std::unique_ptr<llvm::MemoryBuffer> mirrorBuffer{ | ||
llvm::MemoryBuffer::getMemBuffer(buffer->getBuffer(), | ||
buffer->getBufferIdentifier(), | ||
/*RequiresNullTerminator=*/true) | ||
}; | ||
mirrorID = swiftSourceManager.addNewSourceBuffer(std::move(mirrorBuffer)); | ||
mirroredBuffers[buffer] = mirrorID; | ||
} | ||
loc = swiftSourceManager.getLocForOffset(mirrorID, decomposedLoc.second); | ||
|
||
auto presumedLoc = clangSrcMgr.getPresumedLoc(clangLoc); | ||
if (!presumedLoc.getFilename()) | ||
return loc; | ||
if (presumedLoc.getLine() == 0) | ||
return SourceLoc(); | ||
|
||
unsigned bufferLineNumber = | ||
clangSrcMgr.getLineNumber(decomposedLoc.first, decomposedLoc.second); | ||
|
||
StringRef presumedFile = presumedLoc.getFilename(); | ||
SourceLoc startOfLine = loc.getAdvancedLoc(-presumedLoc.getColumn() + 1); | ||
bool isNewVirtualFile = swiftSourceManager.openVirtualFile( | ||
startOfLine, presumedFile, presumedLoc.getLine() - bufferLineNumber); | ||
if (isNewVirtualFile) { | ||
SourceLoc endOfLine = findEndOfLine(swiftSourceManager, loc, mirrorID); | ||
swiftSourceManager.closeVirtualFile(endOfLine); | ||
} | ||
|
||
using SourceManagerRef = llvm::IntrusiveRefCntPtr<const clang::SourceManager>; | ||
auto iter = std::lower_bound(sourceManagersWithDiagnostics.begin(), | ||
sourceManagersWithDiagnostics.end(), | ||
&clangSrcMgr, | ||
[](const SourceManagerRef &inArray, | ||
const clang::SourceManager *toInsert) { | ||
return std::less<const clang::SourceManager *>()(inArray.get(), toInsert); | ||
}); | ||
if (iter == sourceManagersWithDiagnostics.end() || | ||
iter->get() != &clangSrcMgr) { | ||
sourceManagersWithDiagnostics.insert(iter, &clangSrcMgr); | ||
} | ||
|
||
return loc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//===--- ClangSourceBufferImporter.h - Map Clang buffers over ---*- C++ -*-===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SWIFT_CLANGIMPORTER_CLANGSOURCEBUFFERIMPORTER_H | ||
#define SWIFT_CLANGIMPORTER_CLANGSOURCEBUFFERIMPORTER_H | ||
|
||
#include "swift/Basic/LLVM.h" | ||
#include "swift/Basic/SourceLoc.h" | ||
#include "clang/Basic/SourceLocation.h" | ||
#include "llvm/ADT/DenseMap.h" | ||
#include "llvm/ADT/IntrusiveRefCntPtr.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
|
||
namespace llvm { | ||
class MemoryBuffer; | ||
} | ||
|
||
namespace clang { | ||
class SourceManager; | ||
} | ||
|
||
namespace swift { | ||
class SourceManager; | ||
|
||
namespace importer { | ||
|
||
/// A helper class used to keep alive the Clang source managers where | ||
/// diagnostics have been reported. | ||
/// | ||
/// This is a bit of a hack, but LLVM's source manager (and by extension | ||
/// Swift's) does not support buffers going away, so if we want to report | ||
/// diagnostics in them we have to do it this way. | ||
class ClangSourceBufferImporter { | ||
// This is not using SmallPtrSet or similar because we need the | ||
// IntrusiveRefCntPtr to stay a ref-counting pointer. | ||
SmallVector<llvm::IntrusiveRefCntPtr<const clang::SourceManager>, 4> | ||
sourceManagersWithDiagnostics; | ||
llvm::DenseMap<const llvm::MemoryBuffer *, unsigned> mirroredBuffers; | ||
SourceManager &swiftSourceManager; | ||
|
||
public: | ||
explicit ClangSourceBufferImporter(SourceManager &sourceMgr) | ||
: swiftSourceManager(sourceMgr) {} | ||
|
||
/// Returns a Swift source location that points into a Clang buffer. | ||
/// | ||
/// This will keep the Clang buffer alive as long as this object. | ||
SourceLoc resolveSourceLocation(const clang::SourceManager &clangSrcMgr, | ||
clang::SourceLocation clangLoc); | ||
}; | ||
|
||
} // end namespace importer | ||
} // end namespace swift | ||
|
||
#endif |
Oops, something went wrong.