[cfe-commits] r95602 - in /cfe/trunk: include/clang-c/Index.h tools/CIndex/CIndex.exports tools/CIndex/CIndexDiagnostic.cpp tools/c-index-test/c-index-test.c

Douglas Gregor dgregor at apple.com
Mon Feb 8 15:11:56 PST 2010


Author: dgregor
Date: Mon Feb  8 17:11:56 2010
New Revision: 95602

URL: http://llvm.org/viewvc/llvm-project?rev=95602&view=rev
Log:
Clean up the CIndex interface to diagnostic ranges. Thanks, Daniel!

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/tools/CIndex/CIndex.exports
    cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp
    cfe/trunk/tools/c-index-test/c-index-test.c

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=95602&r1=95601&r2=95602&view=diff

==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Mon Feb  8 17:11:56 2010
@@ -446,32 +446,28 @@
  * \brief Retrieve the text of the given diagnostic.
  */
 CINDEX_LINKAGE CXString clang_getDiagnosticSpelling(CXDiagnostic);
+
+/**
+ * \brief Determine the number of source ranges associated with the given
+ * diagnostic.
+ */
+CINDEX_LINKAGE unsigned clang_getDiagnosticNumRanges(CXDiagnostic);
   
 /**
- * \brief Retrieve the source ranges associated with the diagnostic.
+ * \brief Retrieve a source range associated with the diagnostic.
  *
- * These source ranges highlight important elements in the source
+ * A diagnostic's source ranges highlight important elements in the source
  * code. On the command line, Clang displays source ranges by
  * underlining them with '~' characters. 
  *
- * \param Diagnostic the diagnostic whose ranges are being extracted.
+ * \param Diagnostic the diagnostic whose range is being extracted.
  *
- * \param Ranges [out] will be set to a newly-allocated array
- * containing the source ranges of this diagnostic. These ranges must
- * be freed with \c clang_disposeDiagnosticRanges().
+ * \param Range the zero-based index specifying which range to 
  *
- * \param NumRanges [out] will be set to the number of source ranges
- * in the \p Ranges array.
- */
-CINDEX_LINKAGE void clang_getDiagnosticRanges(CXDiagnostic Diagnostic, 
-                                              CXSourceRange **Ranges, 
-                                              unsigned *NumRanges);
-
-/**
- * \brief Free the source ranges returned by \c clang_getDiagnosticRanges().
+ * \returns the requested source range.
  */
-CINDEX_LINKAGE void clang_disposeDiagnosticRanges(CXSourceRange *Ranges, 
-                                                  unsigned NumRanges);
+CINDEX_LINKAGE CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diagnostic, 
+                                                      unsigned Range);
 
 /**
  * \brief Determine the number of fix-it hints associated with the

Modified: cfe/trunk/tools/CIndex/CIndex.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndex.exports?rev=95602&r1=95601&r2=95602&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndex.exports (original)
+++ cfe/trunk/tools/CIndex/CIndex.exports Mon Feb  8 17:11:56 2010
@@ -4,7 +4,6 @@
 _clang_createTranslationUnit
 _clang_createTranslationUnitFromSourceFile
 _clang_disposeCodeCompleteResults
-_clang_disposeDiagnosticRanges
 _clang_disposeIndex
 _clang_disposeString
 _clang_disposeTokens
@@ -32,7 +31,8 @@
 _clang_getDiagnosticFixItReplacement
 _clang_getDiagnosticLocation
 _clang_getDiagnosticNumFixIts
-_clang_getDiagnosticRanges
+_clang_getDiagnosticNumRanges
+_clang_getDiagnosticRange
 _clang_getDiagnosticSeverity
 _clang_getDiagnosticSpelling
 _clang_getFile

Modified: cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp?rev=95602&r1=95601&r2=95602&view=diff

==============================================================================
--- cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp (original)
+++ cfe/trunk/tools/CIndex/CIndexDiagnostic.cpp Mon Feb  8 17:11:56 2010
@@ -106,33 +106,23 @@
   return CIndexer::createCXString(Spelling.str(), true);
 }
 
-void clang_getDiagnosticRanges(CXDiagnostic Diag, 
-                               CXSourceRange **Ranges, 
-                               unsigned *NumRanges) {
-  if (Ranges) 
-    *Ranges = 0;
-  if (NumRanges)
-    *NumRanges = 0;
+unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) {
+  CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
+  if (!StoredDiag || StoredDiag->Info.getLocation().isInvalid())
+    return 0;
+  
+  return StoredDiag->Info.getNumRanges();
+}
   
+CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diag, unsigned Range) {
   CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag);
-  if (!StoredDiag || !Ranges || !NumRanges || 
-      !StoredDiag->Info.getNumRanges() || 
+  if (!StoredDiag || Range >= StoredDiag->Info.getNumRanges() || 
       StoredDiag->Info.getLocation().isInvalid())
-    return;
+    return clang_getNullRange();
   
-  unsigned N = StoredDiag->Info.getNumRanges();
-  *Ranges = (CXSourceRange *)malloc(sizeof(CXSourceRange) * N);
-  *NumRanges = N;
-  for (unsigned I = 0; I != N; ++I)
-    (*Ranges)[I] = translateSourceRange(
-                                    StoredDiag->Info.getLocation().getManager(),
-                                        *StoredDiag->LangOptsPtr,
-                                        StoredDiag->Info.getRange(I));
-}
-
-void clang_disposeDiagnosticRanges(CXSourceRange *Ranges, 
-                                   unsigned NumRanges) {
-  free(Ranges);
+  return translateSourceRange(StoredDiag->Info.getLocation().getManager(),
+                              *StoredDiag->LangOptsPtr,
+                              StoredDiag->Info.getRange(Range));
 }
 
 unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag) {

Modified: cfe/trunk/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=95602&r1=95601&r2=95602&view=diff

==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Mon Feb  8 17:11:56 2010
@@ -196,20 +196,20 @@
   clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
                                  &file, &line, &column, 0);
   if (file) {
-    CXSourceRange *ranges = 0;
-    unsigned num_ranges;
-    unsigned i;
+    unsigned i, n;
     unsigned printed_any_ranges = 0;
     
     fprintf(out, "%s:%d:%d:", clang_getFileName(file), line, column);
   
-    clang_getDiagnosticRanges(Diagnostic, &ranges, &num_ranges);
-    for (i = 0; i != num_ranges; ++i) {
+    n = clang_getDiagnosticNumRanges(Diagnostic);
+    for (i = 0; i != n; ++i) {
       CXFile start_file, end_file;
+      CXSourceRange range = clang_getDiagnosticRange(Diagnostic, i);
+      
       unsigned start_line, start_column, end_line, end_column;
-      clang_getInstantiationLocation(clang_getRangeStart(ranges[i]),
+      clang_getInstantiationLocation(clang_getRangeStart(range),
                                      &start_file, &start_line, &start_column,0);
-      clang_getInstantiationLocation(clang_getRangeEnd(ranges[i]),
+      clang_getInstantiationLocation(clang_getRangeEnd(range),
                                      &end_file, &end_line, &end_column, 0);
       
       if (start_file != end_file || start_file != file)
@@ -219,7 +219,6 @@
               end_column+1);
       printed_any_ranges = 1;
     }
-    clang_disposeDiagnosticRanges(ranges, num_ranges);
     if (printed_any_ranges)
       fprintf(out, ":");
     





More information about the cfe-commits mailing list