r196487 - [libclang] Record ranges skipped by the preprocessor and expose them with libclang.

Argyrios Kyrtzidis akyrtzi at gmail.com
Thu Dec 5 00:19:32 PST 2013


Author: akirtzidis
Date: Thu Dec  5 02:19:32 2013
New Revision: 196487

URL: http://llvm.org/viewvc/llvm-project?rev=196487&view=rev
Log:
[libclang] Record ranges skipped by the preprocessor and expose them with libclang.

Patch by Erik Verbruggen!

Added:
    cfe/trunk/test/Index/skipped-ranges.c
Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/include/clang/Lex/PreprocessingRecord.h
    cfe/trunk/lib/Lex/PreprocessingRecord.cpp
    cfe/trunk/tools/c-index-test/c-index-test.c
    cfe/trunk/tools/libclang/CIndex.cpp
    cfe/trunk/tools/libclang/libclang.exports

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=196487&r1=196486&r2=196487&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Thu Dec  5 02:19:32 2013
@@ -30,7 +30,7 @@
  * compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.
  */
 #define CINDEX_VERSION_MAJOR 0
-#define CINDEX_VERSION_MINOR 20
+#define CINDEX_VERSION_MINOR 21
 
 #define CINDEX_VERSION_ENCODE(major, minor) ( \
       ((major) * 10000)                       \
@@ -599,6 +599,33 @@ CINDEX_LINKAGE CXSourceLocation clang_ge
 CINDEX_LINKAGE CXSourceLocation clang_getRangeEnd(CXSourceRange range);
 
 /**
+ * \brief Identifies an array of ranges that were skipped by the preprocessor.
+ *
+ * The preprocessor will skip lines when they are surrounded by an
+ * if/ifdef/ifndef directive whose condition does not evaluate to true.
+ */
+typedef struct {
+  /** \brief The number of ranges in the \c ranges array. */
+  unsigned count;
+  /**
+   * \brief An array of \c CXSourceRange, where each range starts at the
+   * preprocessor directive after the # token, and ends at the end of the
+   * corresponding endif.
+   */
+  CXSourceRange *ranges;
+} CXSkippedRanges;
+
+/**
+ * \brief Retrieve all ranges that were skipped by the preprocessor.
+ */
+CINDEX_LINKAGE CXSkippedRanges *clang_getSkippedRanges(CXTranslationUnit tu, CXFile file);
+
+/**
+ * \brief Destroy the given \c CXSkippedRanges.
+ */
+CINDEX_LINKAGE void clang_disposeSkippedRanges(CXSkippedRanges *skipped);
+
+/**
  * @}
  */
 

Modified: cfe/trunk/include/clang/Lex/PreprocessingRecord.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/PreprocessingRecord.h?rev=196487&r1=196486&r2=196487&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/PreprocessingRecord.h (original)
+++ cfe/trunk/include/clang/Lex/PreprocessingRecord.h Thu Dec  5 02:19:32 2013
@@ -304,6 +304,9 @@ namespace clang {
     /// and are referenced by the iterator using negative indices.
     std::vector<PreprocessedEntity *> LoadedPreprocessedEntities;
 
+    /// \brief The set of ranges that were skipped by the preprocessor,
+    std::vector<SourceRange> SkippedRanges;
+
     /// \brief Global (loaded or local) ID for a preprocessed entity.
     /// Negative values are used to indicate preprocessed entities
     /// loaded from the external source while non-negative values are used to
@@ -556,6 +559,11 @@ namespace clang {
     /// \brief Retrieve the macro definition that corresponds to the given
     /// \c MacroInfo.
     MacroDefinition *findMacroDefinition(const MacroInfo *MI);
+
+    /// \brief Retrieve all ranges that got skipped while preprocessing.
+    const std::vector<SourceRange> &getSkippedRanges() const {
+      return SkippedRanges;
+    }
         
   private:
     virtual void MacroExpands(const Token &Id, const MacroDirective *MD,
@@ -579,6 +587,8 @@ namespace clang {
     virtual void Defined(const Token &MacroNameTok, const MacroDirective *MD,
                          SourceRange Range);
 
+    virtual void SourceRangeSkipped(SourceRange Range);
+
     void addMacroExpansion(const Token &Id, const MacroInfo *MI,
                            SourceRange Range);
 

Modified: cfe/trunk/lib/Lex/PreprocessingRecord.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PreprocessingRecord.cpp?rev=196487&r1=196486&r2=196487&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PreprocessingRecord.cpp (original)
+++ cfe/trunk/lib/Lex/PreprocessingRecord.cpp Thu Dec  5 02:19:32 2013
@@ -406,6 +406,10 @@ void PreprocessingRecord::Defined(const
                       MacroNameTok.getLocation());
 }
 
+void PreprocessingRecord::SourceRangeSkipped(SourceRange Range) {
+  SkippedRanges.push_back(Range);
+}
+
 void PreprocessingRecord::MacroExpands(const Token &Id,const MacroDirective *MD,
                                        SourceRange Range,
                                        const MacroArgs *Args) {

Added: cfe/trunk/test/Index/skipped-ranges.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/skipped-ranges.c?rev=196487&view=auto
==============================================================================
--- cfe/trunk/test/Index/skipped-ranges.c (added)
+++ cfe/trunk/test/Index/skipped-ranges.c Thu Dec  5 02:19:32 2013
@@ -0,0 +1,25 @@
+#define cool
+
+#if defined(cool)
+
+#if defined(really_cool)
+#endif // really_cool
+
+#elif defined(hot)
+// hot
+
+
+#endif // trailing comment
+
+#ifndef cool
+#ifndef uncool
+
+int probably_hot = 1;
+
+#endif // uncool
+#endif // cool
+
+// RUN: env CINDEXTEST_SHOW_SKIPPED_RANGES=1 c-index-test -test-annotate-tokens=%s:1:1:16:1 %s | FileCheck %s
+// CHECK: Skipping: [5:2 - 6:7]
+// CHECK: Skipping: [8:2 - 12:7]
+// CHECK: Skipping: [14:2 - 20:7]

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=196487&r1=196486&r2=196487&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Thu Dec  5 02:19:32 2013
@@ -3183,6 +3183,7 @@ int perform_token_annotation(int argc, c
   CXSourceLocation startLoc, endLoc;
   CXFile file = 0;
   CXCursor *cursors = 0;
+  CXSkippedRanges *skipped_ranges = 0;
   unsigned i;
 
   input += strlen("-test-annotate-tokens=");
@@ -3271,6 +3272,19 @@ int perform_token_annotation(int argc, c
     goto teardown;
   }
 
+  skipped_ranges = clang_getSkippedRanges(TU, file);
+  for (i = 0; i != skipped_ranges->count; ++i) {
+    unsigned start_line, start_column, end_line, end_column;
+    clang_getSpellingLocation(clang_getRangeStart(skipped_ranges->ranges[i]),
+                              0, &start_line, &start_column, 0);
+    clang_getSpellingLocation(clang_getRangeEnd(skipped_ranges->ranges[i]),
+                              0, &end_line, &end_column, 0);
+    printf("Skipping: ");
+    PrintExtent(stdout, start_line, start_column, end_line, end_column);
+    printf("\n");
+  }
+  clang_disposeSkippedRanges(skipped_ranges);
+
   for (i = 0; i != num_tokens; ++i) {
     const char *kind = "<unknown>";
     CXString spelling = clang_getTokenSpelling(TU, tokens[i]);

Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=196487&r1=196486&r2=196487&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Thu Dec  5 02:19:32 2013
@@ -6494,6 +6494,47 @@ void clang_disposeCXTUResourceUsage(CXTU
     delete (MemUsageEntries*) usage.data;
 }
 
+CXSkippedRanges *clang_getSkippedRanges(CXTranslationUnit TU, CXFile file) {
+  CXSkippedRanges *skipped = new CXSkippedRanges;
+  skipped->count = 0;
+  skipped->ranges = 0;
+
+  if (!file)
+    return skipped;
+
+  ASTUnit *astUnit = cxtu::getASTUnit(TU);
+  PreprocessingRecord *ppRec = astUnit->getPreprocessor().getPreprocessingRecord();
+  if (!ppRec)
+    return skipped;
+
+  ASTContext &Ctx = astUnit->getASTContext();
+  SourceManager &sm = Ctx.getSourceManager();
+  FileEntry *fileEntry = static_cast<FileEntry *>(file);
+  FileID wantedFileID = sm.translateFile(fileEntry);
+
+  const std::vector<SourceRange> &SkippedRanges = ppRec->getSkippedRanges();
+  std::vector<SourceRange> wantedRanges;
+  for (std::vector<SourceRange>::const_iterator i = SkippedRanges.begin(), ei = SkippedRanges.end();
+       i != ei; ++i) {
+    if (sm.getFileID(i->getBegin()) == wantedFileID || sm.getFileID(i->getEnd()) == wantedFileID)
+      wantedRanges.push_back(*i);
+  }
+
+  skipped->count = wantedRanges.size();
+  skipped->ranges = new CXSourceRange[skipped->count];
+  for (unsigned i = 0, ei = skipped->count; i != ei; ++i)
+    skipped->ranges[i] = cxloc::translateSourceRange(Ctx, wantedRanges[i]);
+
+  return skipped;
+}
+
+void clang_disposeSkippedRanges(CXSkippedRanges *skipped) {
+  if (skipped) {
+    delete[] skipped->ranges;
+    delete skipped;
+  }
+}
+
 } // end extern "C"
 
 void clang::PrintLibclangResourceUsage(CXTranslationUnit TU) {

Modified: cfe/trunk/tools/libclang/libclang.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=196487&r1=196486&r2=196487&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.exports (original)
+++ cfe/trunk/tools/libclang/libclang.exports Thu Dec  5 02:19:32 2013
@@ -101,6 +101,7 @@ clang_disposeDiagnosticSet
 clang_disposeIndex
 clang_disposeOverriddenCursors
 clang_disposeCXPlatformAvailability
+clang_disposeSkippedRanges
 clang_disposeString
 clang_disposeTokens
 clang_disposeTranslationUnit
@@ -207,6 +208,7 @@ clang_getRangeStart
 clang_getRemappings
 clang_getRemappingsFromFileList
 clang_getResultType
+clang_getSkippedRanges
 clang_getSpecializedCursorTemplate
 clang_getSpellingLocation
 clang_getTUResourceUsageName





More information about the cfe-commits mailing list