|
12 | 12 |
|
13 | 13 | #include "clang/ASTMatchers/ASTMatchers.h"
|
14 | 14 | #include "llvm/ADT/StringMap.h"
|
| 15 | +#include "llvm/Support/SmallVectorMemoryBuffer.h" |
15 | 16 |
|
16 | 17 | #include "clang/AST/DeclContextInternals.h"
|
17 | 18 | #include "gtest/gtest.h"
|
@@ -5896,6 +5897,61 @@ TEST_P(ImportSourceLocations, PreserveFileIDTreeStructure) {
|
5896 | 5897 | EXPECT_FALSE(ToSM.isBeforeInTranslationUnit(Location2, Location1));
|
5897 | 5898 | }
|
5898 | 5899 |
|
| 5900 | +TEST_P(ImportSourceLocations, NormalFileBuffer) { |
| 5901 | + // Test importing normal file buffers. |
| 5902 | + |
| 5903 | + std::string Path = "input0.c"; |
| 5904 | + std::string Source = "int X;"; |
| 5905 | + TranslationUnitDecl *FromTU = getTuDecl(Source, Lang_C, Path); |
| 5906 | + |
| 5907 | + SourceLocation ImportedLoc; |
| 5908 | + { |
| 5909 | + // Import the VarDecl to trigger the importing of the FileID. |
| 5910 | + auto Pattern = varDecl(hasName("X")); |
| 5911 | + VarDecl *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern); |
| 5912 | + ImportedLoc = Import(FromD, Lang_C)->getLocation(); |
| 5913 | + } |
| 5914 | + |
| 5915 | + // Make sure the imported buffer has the original contents. |
| 5916 | + SourceManager &ToSM = ToAST->getSourceManager(); |
| 5917 | + FileID ImportedID = ToSM.getFileID(ImportedLoc); |
| 5918 | + EXPECT_EQ(Source, ToSM.getBuffer(ImportedID, SourceLocation())->getBuffer()); |
| 5919 | +} |
| 5920 | + |
| 5921 | +TEST_P(ImportSourceLocations, OverwrittenFileBuffer) { |
| 5922 | + // Test importing overwritten file buffers. |
| 5923 | + |
| 5924 | + std::string Path = "input0.c"; |
| 5925 | + TranslationUnitDecl *FromTU = getTuDecl("int X;", Lang_C, Path); |
| 5926 | + |
| 5927 | + // Overwrite the file buffer for our input file with new content. |
| 5928 | + const std::string Contents = "overwritten contents"; |
| 5929 | + SourceLocation ImportedLoc; |
| 5930 | + { |
| 5931 | + SourceManager &FromSM = FromTU->getASTContext().getSourceManager(); |
| 5932 | + clang::FileManager &FM = FromSM.getFileManager(); |
| 5933 | + const clang::FileEntry &FE = |
| 5934 | + *FM.getVirtualFile(Path, static_cast<off_t>(Contents.size()), 0); |
| 5935 | + |
| 5936 | + llvm::SmallVector<char, 64> Buffer; |
| 5937 | + Buffer.append(Contents.begin(), Contents.end()); |
| 5938 | + auto FileContents = |
| 5939 | + std::make_unique<llvm::SmallVectorMemoryBuffer>(std::move(Buffer), Path); |
| 5940 | + FromSM.overrideFileContents(&FE, std::move(FileContents)); |
| 5941 | + |
| 5942 | + // Import the VarDecl to trigger the importing of the FileID. |
| 5943 | + auto Pattern = varDecl(hasName("X")); |
| 5944 | + VarDecl *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern); |
| 5945 | + ImportedLoc = Import(FromD, Lang_C)->getLocation(); |
| 5946 | + } |
| 5947 | + |
| 5948 | + // Make sure the imported buffer has the overwritten contents. |
| 5949 | + SourceManager &ToSM = ToAST->getSourceManager(); |
| 5950 | + FileID ImportedID = ToSM.getFileID(ImportedLoc); |
| 5951 | + EXPECT_EQ(Contents, |
| 5952 | + ToSM.getBuffer(ImportedID, SourceLocation())->getBuffer()); |
| 5953 | +} |
| 5954 | + |
5899 | 5955 | TEST_P(ASTImporterOptionSpecificTestBase, ImportExprOfAlignmentAttr) {
|
5900 | 5956 | // Test if import of these packed and aligned attributes does not trigger an
|
5901 | 5957 | // error situation where source location from 'From' context is referenced in
|
|
0 commit comments