]> granicus.if.org Git - clang/commitdiff
Fix the location of the fixit for -Wnewline-eof.
authorJordan Rose <jordan_rose@apple.com>
Tue, 19 Jun 2012 03:09:38 +0000 (03:09 +0000)
committerJordan Rose <jordan_rose@apple.com>
Tue, 19 Jun 2012 03:09:38 +0000 (03:09 +0000)
It turns out SourceManager treating the "one-past-the-end" location as invalid,
but then failing to set the invalid flag properly.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@158699 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Basic/SourceManager.cpp
test/Lexer/newline-eof.c
unittests/Basic/SourceManagerTest.cpp

index 66b06ef6ab1bdfdd90a107c03b446e2984c15d9b..24bd51928c9e87807f92cd377eae664e58f9bcf8 100644 (file)
@@ -1015,9 +1015,10 @@ unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos,
   if (MyInvalid)
     return 1;
 
-  if (FilePos >= MemBuf->getBufferSize()) {
+  // It is okay to request a position just past the end of the buffer.
+  if (FilePos > MemBuf->getBufferSize()) {
     if (Invalid)
-      *Invalid = MyInvalid;
+      *Invalid = true;
     return 1;
   }
 
index 825a266a0a36445308a76eb2d837f476ee3941b4..a4a18835cf5fc1781bd7091af954aaf62c5111a9 100644 (file)
@@ -1,5 +1,9 @@
 // RUN: %clang_cc1 -fsyntax-only -Wnewline-eof -verify %s
+// RUN: %clang_cc1 -fsyntax-only -Wnewline-eof %s 2>&1 | FileCheck %s
 // rdar://9133072
 
+// Make sure the diagnostic shows up properly at the end of the last line.
+// CHECK: newline-eof.c:9:63
+
 // The following line isn't terminated, don't fix it.
 void foo() {} // expected-warning{{no newline at end of file}}
\ No newline at end of file
index 429b58d7ea4523ae427419da037e980e09d50336..de3b72318ccfbca35be0faaf78583a1659913c4e 100644 (file)
@@ -107,6 +107,54 @@ TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
   EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, macroExpEndLoc));
 }
 
+TEST_F(SourceManagerTest, getColumnNumber) {
+  const char *Source =
+    "int x;\n"
+    "int y;";
+
+  MemoryBuffer *Buf = MemoryBuffer::getMemBuffer(Source);
+  FileID MainFileID = SourceMgr.createMainFileIDForMemBuffer(Buf);
+
+  bool Invalid;
+
+  Invalid = false;
+  EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, &Invalid));
+  EXPECT_TRUE(!Invalid);
+
+  Invalid = false;
+  EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 4, &Invalid));
+  EXPECT_TRUE(!Invalid);
+
+  Invalid = false;
+  EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 7, &Invalid));
+  EXPECT_TRUE(!Invalid);
+
+  Invalid = false;
+  EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 11, &Invalid));
+  EXPECT_TRUE(!Invalid);
+
+  Invalid = false;
+  EXPECT_EQ(7U, SourceMgr.getColumnNumber(MainFileID, strlen(Source),
+                                         &Invalid));
+  EXPECT_TRUE(!Invalid);
+
+  Invalid = false;
+  SourceMgr.getColumnNumber(MainFileID, strlen(Source)+1, &Invalid);
+  EXPECT_TRUE(Invalid);
+
+  // Test invalid files
+  Invalid = false;
+  SourceMgr.getColumnNumber(FileID(), 0, &Invalid);
+  EXPECT_TRUE(Invalid);
+
+  Invalid = false;
+  SourceMgr.getColumnNumber(FileID(), 1, &Invalid);
+  EXPECT_TRUE(Invalid);
+
+  // Test with no invalid flag.
+  EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, NULL));
+}
+
 #if defined(LLVM_ON_UNIX)
 
 TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {