[cfe-commits] r169666 - in /cfe/trunk: include/clang/Lex/PreprocessingRecord.h lib/Lex/PreprocessingRecord.cpp test/Index/c-index-getCursor-pp.c
Argyrios Kyrtzidis
akyrtzi at gmail.com
Fri Dec 7 18:21:17 PST 2012
Author: akirtzidis
Date: Fri Dec 7 20:21:17 2012
New Revision: 169666
URL: http://llvm.org/viewvc/llvm-project?rev=169666&view=rev
Log:
[libclang] Resolve a cursor that points to a macro name inside a #ifdef/#ifndef
directive as a macro expansion.
This is more of a "macro reference" than a macro expansion but it's close enough
for libclang's purposes. If it causes issues we can revisit and introduce a new
kind of cursor.
Modified:
cfe/trunk/include/clang/Lex/PreprocessingRecord.h
cfe/trunk/lib/Lex/PreprocessingRecord.cpp
cfe/trunk/test/Index/c-index-getCursor-pp.c
Modified: cfe/trunk/include/clang/Lex/PreprocessingRecord.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/PreprocessingRecord.h?rev=169666&r1=169665&r2=169666&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/PreprocessingRecord.h (original)
+++ cfe/trunk/include/clang/Lex/PreprocessingRecord.h Fri Dec 7 20:21:17 2012
@@ -570,6 +570,15 @@
StringRef SearchPath,
StringRef RelativePath,
const Module *Imported);
+ virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
+ const MacroInfo *MI);
+ virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
+ const MacroInfo *MI);
+ /// \brief Hook called whenever the 'defined' operator is seen.
+ virtual void Defined(const Token &MacroNameTok, const MacroInfo *MI);
+
+ void addMacroExpansion(const Token &Id, const MacroInfo *MI,
+ SourceRange Range);
/// \brief Cached result of the last \see getPreprocessedEntitiesInRange
/// query.
Modified: cfe/trunk/lib/Lex/PreprocessingRecord.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PreprocessingRecord.cpp?rev=169666&r1=169665&r2=169666&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PreprocessingRecord.cpp (original)
+++ cfe/trunk/lib/Lex/PreprocessingRecord.cpp Fri Dec 7 20:21:17 2012
@@ -357,8 +357,9 @@
return cast<MacroDefinition>(Entity);
}
-void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI,
- SourceRange Range) {
+void PreprocessingRecord::addMacroExpansion(const Token &Id,
+ const MacroInfo *MI,
+ SourceRange Range) {
// We don't record nested macro expansions.
if (Id.getLocation().isMacroID())
return;
@@ -371,6 +372,32 @@
new (*this) MacroExpansion(Def, Range));
}
+void PreprocessingRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok,
+ const MacroInfo *MI) {
+ // This is not actually a macro expansion but record it as a macro reference.
+ if (MI)
+ addMacroExpansion(MacroNameTok, MI, MacroNameTok.getLocation());
+}
+
+void PreprocessingRecord::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
+ const MacroInfo *MI) {
+ // This is not actually a macro expansion but record it as a macro reference.
+ if (MI)
+ addMacroExpansion(MacroNameTok, MI, MacroNameTok.getLocation());
+}
+
+void PreprocessingRecord::Defined(const Token &MacroNameTok,
+ const MacroInfo *MI) {
+ // This is not actually a macro expansion but record it as a macro reference.
+ if (MI)
+ addMacroExpansion(MacroNameTok, MI, MacroNameTok.getLocation());
+}
+
+void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI,
+ SourceRange Range) {
+ addMacroExpansion(Id, MI, Range);
+}
+
void PreprocessingRecord::MacroDefined(const Token &Id,
const MacroInfo *MI) {
SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
Modified: cfe/trunk/test/Index/c-index-getCursor-pp.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/c-index-getCursor-pp.c?rev=169666&r1=169665&r2=169666&view=diff
==============================================================================
--- cfe/trunk/test/Index/c-index-getCursor-pp.c (original)
+++ cfe/trunk/test/Index/c-index-getCursor-pp.c Fri Dec 7 20:21:17 2012
@@ -17,6 +17,12 @@
#include <a.h>
+#ifdef OBSCURE
+#endif
+
+#if defined(OBSCURE)
+#endif
+
// RUN: c-index-test -cursor-at=%s:1:11 -I%S/Inputs %s | FileCheck -check-prefix=CHECK-1 %s
// CHECK-1: macro definition=OBSCURE
// RUN: c-index-test -cursor-at=%s:2:14 -I%S/Inputs %s | FileCheck -check-prefix=CHECK-2 %s
@@ -35,6 +41,9 @@
// CHECK-8: macro expansion=__FILE__
// RUN: c-index-test -cursor-at=%s:18:12 -I%S/Inputs %s | FileCheck -check-prefix=CHECK-9 %s
// CHECK-9: inclusion directive=a.h
+// RUN: c-index-test -cursor-at=%s:20:10 -cursor-at=%s:23:15 -I%S/Inputs %s | FileCheck -check-prefix=CHECK-10 %s
+// CHECK-10: 20:8 macro expansion=OBSCURE
+// CHECK-10: 23:13 macro expansion=OBSCURE
// Same tests, but with "editing" optimizations
// RUN: env CINDEXTEST_EDITING=1 c-index-test -cursor-at=%s:1:11 -I%S/Inputs %s | FileCheck -check-prefix=CHECK-1 %s
More information about the cfe-commits
mailing list