1 //===- unittests/Basic/SourceManagerTest.cpp ------ SourceManager tests ---===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "clang/Basic/SourceManager.h" 11 #include "clang/Basic/FileManager.h" 12 #include "clang/Basic/Diagnostic.h" 13 #include "clang/Basic/LangOptions.h" 14 #include "clang/Basic/TargetOptions.h" 15 #include "clang/Basic/TargetInfo.h" 16 #include "clang/Lex/ModuleLoader.h" 17 #include "clang/Lex/HeaderSearch.h" 18 #include "clang/Lex/Preprocessor.h" 19 #include "llvm/ADT/SmallString.h" 20 #include "llvm/Config/config.h" 21 22 #include "gtest/gtest.h" 23 24 using namespace llvm; 25 using namespace clang; 26 27 namespace { 28 29 // The test fixture. 30 class SourceManagerTest : public ::testing::Test { 31 protected: 32 SourceManagerTest() 33 : FileMgr(FileMgrOpts), 34 DiagID(new DiagnosticIDs()), 35 Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()), 36 SourceMgr(Diags, FileMgr), 37 TargetOpts(new TargetOptions) { 38 TargetOpts->Triple = "x86_64-apple-darwin11.1.0"; 39 Target = TargetInfo::CreateTargetInfo(Diags, *TargetOpts); 40 } 41 42 FileSystemOptions FileMgrOpts; 43 FileManager FileMgr; 44 IntrusiveRefCntPtr<DiagnosticIDs> DiagID; 45 DiagnosticsEngine Diags; 46 SourceManager SourceMgr; 47 LangOptions LangOpts; 48 IntrusiveRefCntPtr<TargetOptions> TargetOpts; 49 IntrusiveRefCntPtr<TargetInfo> Target; 50 }; 51 52 class VoidModuleLoader : public ModuleLoader { 53 virtual Module *loadModule(SourceLocation ImportLoc, ModuleIdPath Path, 54 Module::NameVisibilityKind Visibility, 55 bool IsInclusionDirective) { 56 return 0; 57 } 58 }; 59 60 TEST_F(SourceManagerTest, isBeforeInTranslationUnit) { 61 const char *source = 62 "#define M(x) [x]\n" 63 "M(foo)"; 64 MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source); 65 FileID mainFileID = SourceMgr.createMainFileIDForMemBuffer(buf); 66 67 VoidModuleLoader ModLoader; 68 HeaderSearch HeaderInfo(FileMgr, Diags, LangOpts, &*Target); 69 Preprocessor PP(Diags, LangOpts, 70 Target.getPtr(), 71 SourceMgr, HeaderInfo, ModLoader, 72 /*IILookup =*/ 0, 73 /*OwnsHeaderSearch =*/false, 74 /*DelayInitialization =*/ false); 75 PP.EnterMainSourceFile(); 76 77 std::vector<Token> toks; 78 while (1) { 79 Token tok; 80 PP.Lex(tok); 81 if (tok.is(tok::eof)) 82 break; 83 toks.push_back(tok); 84 } 85 86 // Make sure we got the tokens that we expected. 87 ASSERT_EQ(3U, toks.size()); 88 ASSERT_EQ(tok::l_square, toks[0].getKind()); 89 ASSERT_EQ(tok::identifier, toks[1].getKind()); 90 ASSERT_EQ(tok::r_square, toks[2].getKind()); 91 92 SourceLocation lsqrLoc = toks[0].getLocation(); 93 SourceLocation idLoc = toks[1].getLocation(); 94 SourceLocation rsqrLoc = toks[2].getLocation(); 95 96 SourceLocation macroExpStartLoc = SourceMgr.translateLineCol(mainFileID, 2, 1); 97 SourceLocation macroExpEndLoc = SourceMgr.translateLineCol(mainFileID, 2, 6); 98 ASSERT_TRUE(macroExpStartLoc.isFileID()); 99 ASSERT_TRUE(macroExpEndLoc.isFileID()); 100 101 SmallString<32> str; 102 ASSERT_EQ("M", PP.getSpelling(macroExpStartLoc, str)); 103 ASSERT_EQ(")", PP.getSpelling(macroExpEndLoc, str)); 104 105 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(lsqrLoc, idLoc)); 106 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, rsqrLoc)); 107 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(macroExpStartLoc, idLoc)); 108 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, macroExpEndLoc)); 109 } 110 111 TEST_F(SourceManagerTest, getColumnNumber) { 112 const char *Source = 113 "int x;\n" 114 "int y;"; 115 116 MemoryBuffer *Buf = MemoryBuffer::getMemBuffer(Source); 117 FileID MainFileID = SourceMgr.createMainFileIDForMemBuffer(Buf); 118 119 bool Invalid; 120 121 Invalid = false; 122 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, &Invalid)); 123 EXPECT_TRUE(!Invalid); 124 125 Invalid = false; 126 EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 4, &Invalid)); 127 EXPECT_TRUE(!Invalid); 128 129 Invalid = false; 130 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 7, &Invalid)); 131 EXPECT_TRUE(!Invalid); 132 133 Invalid = false; 134 EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 11, &Invalid)); 135 EXPECT_TRUE(!Invalid); 136 137 Invalid = false; 138 EXPECT_EQ(7U, SourceMgr.getColumnNumber(MainFileID, strlen(Source), 139 &Invalid)); 140 EXPECT_TRUE(!Invalid); 141 142 Invalid = false; 143 SourceMgr.getColumnNumber(MainFileID, strlen(Source)+1, &Invalid); 144 EXPECT_TRUE(Invalid); 145 146 // Test invalid files 147 Invalid = false; 148 SourceMgr.getColumnNumber(FileID(), 0, &Invalid); 149 EXPECT_TRUE(Invalid); 150 151 Invalid = false; 152 SourceMgr.getColumnNumber(FileID(), 1, &Invalid); 153 EXPECT_TRUE(Invalid); 154 155 // Test with no invalid flag. 156 EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, NULL)); 157 } 158 159 #if defined(LLVM_ON_UNIX) 160 161 TEST_F(SourceManagerTest, getMacroArgExpandedLocation) { 162 const char *header = 163 "#define FM(x,y) x\n"; 164 165 const char *main = 166 "#include \"/test-header.h\"\n" 167 "#define VAL 0\n" 168 "FM(VAL,0)\n" 169 "FM(0,VAL)\n" 170 "FM(FM(0,VAL),0)\n" 171 "#define CONCAT(X, Y) X##Y\n" 172 "CONCAT(1,1)\n"; 173 174 MemoryBuffer *headerBuf = MemoryBuffer::getMemBuffer(header); 175 MemoryBuffer *mainBuf = MemoryBuffer::getMemBuffer(main); 176 FileID mainFileID = SourceMgr.createMainFileIDForMemBuffer(mainBuf); 177 178 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h", 179 headerBuf->getBufferSize(), 0); 180 SourceMgr.overrideFileContents(headerFile, headerBuf); 181 182 VoidModuleLoader ModLoader; 183 HeaderSearch HeaderInfo(FileMgr, Diags, LangOpts, &*Target); 184 Preprocessor PP(Diags, LangOpts, 185 Target.getPtr(), 186 SourceMgr, HeaderInfo, ModLoader, 187 /*IILookup =*/ 0, 188 /*OwnsHeaderSearch =*/false, 189 /*DelayInitialization =*/ false); 190 PP.EnterMainSourceFile(); 191 192 std::vector<Token> toks; 193 while (1) { 194 Token tok; 195 PP.Lex(tok); 196 if (tok.is(tok::eof)) 197 break; 198 toks.push_back(tok); 199 } 200 201 // Make sure we got the tokens that we expected. 202 ASSERT_EQ(4U, toks.size()); 203 ASSERT_EQ(tok::numeric_constant, toks[0].getKind()); 204 ASSERT_EQ(tok::numeric_constant, toks[1].getKind()); 205 ASSERT_EQ(tok::numeric_constant, toks[2].getKind()); 206 ASSERT_EQ(tok::numeric_constant, toks[3].getKind()); 207 208 SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13); 209 SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8); 210 SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4); 211 SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7); 212 SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22); 213 defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc); 214 loc1 = SourceMgr.getMacroArgExpandedLocation(loc1); 215 loc2 = SourceMgr.getMacroArgExpandedLocation(loc2); 216 loc3 = SourceMgr.getMacroArgExpandedLocation(loc3); 217 defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2); 218 219 EXPECT_TRUE(defLoc.isFileID()); 220 EXPECT_TRUE(loc1.isFileID()); 221 EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2)); 222 EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3)); 223 EXPECT_EQ(loc2, toks[1].getLocation()); 224 EXPECT_EQ(loc3, toks[2].getLocation()); 225 EXPECT_TRUE(defLoc2.isFileID()); 226 } 227 228 namespace { 229 230 struct MacroAction { 231 SourceLocation Loc; 232 std::string Name; 233 bool isDefinition; // if false, it is expansion. 234 235 MacroAction(SourceLocation Loc, StringRef Name, bool isDefinition) 236 : Loc(Loc), Name(Name), isDefinition(isDefinition) { } 237 }; 238 239 class MacroTracker : public PPCallbacks { 240 std::vector<MacroAction> &Macros; 241 242 public: 243 explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { } 244 245 virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) { 246 Macros.push_back(MacroAction(MI->getDefinitionLoc(), 247 MacroNameTok.getIdentifierInfo()->getName(), 248 true)); 249 } 250 virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI, 251 SourceRange Range) { 252 Macros.push_back(MacroAction(MacroNameTok.getLocation(), 253 MacroNameTok.getIdentifierInfo()->getName(), 254 false)); 255 } 256 }; 257 258 } 259 260 TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) { 261 const char *header = 262 "#define MACRO_IN_INCLUDE 0\n"; 263 264 const char *main = 265 "#define M(x) x\n" 266 "#define INC \"/test-header.h\"\n" 267 "#include M(INC)\n" 268 "#define INC2 </test-header.h>\n" 269 "#include M(INC2)\n"; 270 271 MemoryBuffer *headerBuf = MemoryBuffer::getMemBuffer(header); 272 MemoryBuffer *mainBuf = MemoryBuffer::getMemBuffer(main); 273 SourceMgr.createMainFileIDForMemBuffer(mainBuf); 274 275 const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h", 276 headerBuf->getBufferSize(), 0); 277 SourceMgr.overrideFileContents(headerFile, headerBuf); 278 279 VoidModuleLoader ModLoader; 280 HeaderSearch HeaderInfo(FileMgr, Diags, LangOpts, &*Target); 281 Preprocessor PP(Diags, LangOpts, 282 Target.getPtr(), 283 SourceMgr, HeaderInfo, ModLoader, 284 /*IILookup =*/ 0, 285 /*OwnsHeaderSearch =*/false, 286 /*DelayInitialization =*/ false); 287 288 std::vector<MacroAction> Macros; 289 PP.addPPCallbacks(new MacroTracker(Macros)); 290 291 PP.EnterMainSourceFile(); 292 293 std::vector<Token> toks; 294 while (1) { 295 Token tok; 296 PP.Lex(tok); 297 if (tok.is(tok::eof)) 298 break; 299 toks.push_back(tok); 300 } 301 302 // Make sure we got the tokens that we expected. 303 ASSERT_EQ(0U, toks.size()); 304 305 ASSERT_EQ(9U, Macros.size()); 306 // #define M(x) x 307 ASSERT_TRUE(Macros[0].isDefinition); 308 ASSERT_EQ("M", Macros[0].Name); 309 // #define INC "/test-header.h" 310 ASSERT_TRUE(Macros[1].isDefinition); 311 ASSERT_EQ("INC", Macros[1].Name); 312 // M expansion in #include M(INC) 313 ASSERT_FALSE(Macros[2].isDefinition); 314 ASSERT_EQ("M", Macros[2].Name); 315 // INC expansion in #include M(INC) 316 ASSERT_FALSE(Macros[3].isDefinition); 317 ASSERT_EQ("INC", Macros[3].Name); 318 // #define MACRO_IN_INCLUDE 0 319 ASSERT_TRUE(Macros[4].isDefinition); 320 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name); 321 // #define INC2 </test-header.h> 322 ASSERT_TRUE(Macros[5].isDefinition); 323 ASSERT_EQ("INC2", Macros[5].Name); 324 // M expansion in #include M(INC2) 325 ASSERT_FALSE(Macros[6].isDefinition); 326 ASSERT_EQ("M", Macros[6].Name); 327 // INC2 expansion in #include M(INC2) 328 ASSERT_FALSE(Macros[7].isDefinition); 329 ASSERT_EQ("INC2", Macros[7].Name); 330 // #define MACRO_IN_INCLUDE 0 331 ASSERT_TRUE(Macros[8].isDefinition); 332 ASSERT_EQ("MACRO_IN_INCLUDE", Macros[8].Name); 333 334 // The INC expansion in #include M(INC) comes before the first 335 // MACRO_IN_INCLUDE definition of the included file. 336 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[3].Loc, Macros[4].Loc)); 337 338 // The INC2 expansion in #include M(INC2) comes before the second 339 // MACRO_IN_INCLUDE definition of the included file. 340 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[7].Loc, Macros[8].Loc)); 341 } 342 343 #endif 344 345 } // anonymous namespace 346